org.openbaton.autoscaling.Application.java Source code

Java tutorial

Introduction

Here is the source code for org.openbaton.autoscaling.Application.java

Source

/*
 *
 *  *
 *  *  * Copyright (c) 2016 Open Baton (http://www.openbaton.org)
 *  *  *
 *  *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  *  * you may not use this file except in compliance with the License.
 *  *  * You may obtain a copy of the License at
 *  *  *
 *  *  *     http://www.apache.org/licenses/LICENSE-2.0
 *  *  *
 *  *  * Unless required by applicable law or agreed to in writing, software
 *  *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *  * See the License for the specific language governing permissions and
 *  *  * limitations under the License.
 *  *
 *
 */

package org.openbaton.autoscaling;

import org.openbaton.autoscaling.configuration.AutoScalingProperties;
import org.openbaton.autoscaling.configuration.NfvoProperties;
import org.openbaton.autoscaling.configuration.PropertiesConfiguration;
import org.openbaton.autoscaling.configuration.SpringProperties;
import org.openbaton.autoscaling.core.management.ASBeanConfiguration;
import org.openbaton.autoscaling.core.management.ElasticityManagement;
import org.openbaton.autoscaling.utils.Utils;
import org.openbaton.catalogue.mano.record.NetworkServiceRecord;
import org.openbaton.catalogue.mano.record.Status;
import org.openbaton.catalogue.mano.record.VirtualNetworkFunctionRecord;
import org.openbaton.catalogue.nfvo.Action;
import org.openbaton.catalogue.nfvo.EndpointType;
import org.openbaton.catalogue.nfvo.EventEndpoint;
import org.openbaton.catalogue.security.Project;
import org.openbaton.exceptions.NotFoundException;
import org.openbaton.exceptions.VimException;
import org.openbaton.plugin.utils.PluginStartup;
import org.openbaton.sdk.NFVORequestor;
import org.openbaton.sdk.api.exception.SDKException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Created by mpa on 27.10.15.
 */
@SpringBootApplication
@ComponentScan({ "org.openbaton.autoscaling.api", "org.openbaton.autoscaling", "org.openbaton" })
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { ASBeanConfiguration.class,
        PropertiesConfiguration.class })
public class Application implements CommandLineRunner, ApplicationListener<ContextClosedEvent> {

    protected static Logger log = LoggerFactory.getLogger(Application.class);

    @Autowired
    private ConfigurableApplicationContext context;

    private NFVORequestor nfvoRequestor;

    private List<String> subscriptionIds;

    @Autowired
    private AutoScalingProperties autoScalingProperties;

    @Autowired
    private SpringProperties springProperties;

    @Autowired
    private NfvoProperties nfvoProperties;

    private ElasticityManagement elasticityManagement;

    private void init() throws SDKException, ClassNotFoundException {
        //start all the plugins needed
        startPlugins();
        //waiting until the NFVO is available
        waitForNfvo();
        this.elasticityManagement = context.getBean(ElasticityManagement.class);
        this.nfvoRequestor = new NFVORequestor(nfvoProperties.getUsername(), nfvoProperties.getPassword(), "*",
                false, nfvoProperties.getIp(), nfvoProperties.getPort(), "1");
        for (Project project : nfvoRequestor.getProjectAgent().findAll()) {
            if (project.getName().equals("default")) {
                nfvoRequestor.setProjectId(project.getId());
            }
        }
        subscriptionIds = new ArrayList<>();
        subscriptionIds.add(subscribe(Action.INSTANTIATE_FINISH));
        subscriptionIds.add(subscribe(Action.RELEASE_RESOURCES_FINISH));
        subscriptionIds.add(subscribe(Action.ERROR));

        fetchNSRsFromNFVO();
    }

    private void exit() throws SDKException {
        unsubscribe();
        destroyPlugins();
        List<NetworkServiceRecord> nsrs = new ArrayList<>();
        try {
            for (Project project : nfvoRequestor.getProjectAgent().findAll()) {
                nfvoRequestor.setProjectId(project.getId());
                nsrs.addAll(nfvoRequestor.getNetworkServiceRecordAgent().findAll());
            }
        } catch (SDKException e) {
            log.warn(
                    "Problem while fetching exisiting NSRs from the Orchestrator to start Autoscaling. Elasticity for previously deployed NSRs will not start",
                    e);
        } catch (ClassNotFoundException e) {
            log.error(e.getMessage(), e);
        }
        Set<Future<Boolean>> pendingTasks = new HashSet<>();
        for (NetworkServiceRecord nsr : nsrs) {
            for (VirtualNetworkFunctionRecord vnfr : nsr.getVnfr()) {
                pendingTasks.add(elasticityManagement.deactivate(nsr.getProjectId(), nsr.getId(), vnfr.getId()));
            }
        }
        for (Future<Boolean> pendingTask : pendingTasks) {
            try {
                pendingTask.get(60, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                if (log.isDebugEnabled()) {
                    log.error(e.getMessage(), e);
                }
            } catch (ExecutionException e) {
                if (log.isDebugEnabled()) {
                    log.error(e.getMessage(), e);
                }
            } catch (TimeoutException e) {
                if (log.isDebugEnabled()) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

    private String subscribe(Action action) throws SDKException {
        log.debug("Subscribing to all NSR Events with Action " + action);
        EventEndpoint eventEndpoint = new EventEndpoint();
        eventEndpoint.setName("Subscription:" + action);
        eventEndpoint.setEndpoint("http://" + autoScalingProperties.getServer().getIp() + ":"
                + autoScalingProperties.getServer().getPort() + "/elasticity-management/" + action);
        eventEndpoint.setEvent(action);
        eventEndpoint.setType(EndpointType.REST);
        return nfvoRequestor.getEventAgent().create(eventEndpoint).getId();
    }

    private void unsubscribe() throws SDKException {
        for (String subscriptionId : subscriptionIds) {
            nfvoRequestor.getEventAgent().delete(subscriptionId);
        }
    }

    private void startPlugins() {
        try {
            PluginStartup.startPluginRecursive("./plugins", true, autoScalingProperties.getRabbitmq().getBrokerIp(),
                    String.valueOf(springProperties.getRabbitmq().getPort()), 15,
                    springProperties.getRabbitmq().getUsername(), springProperties.getRabbitmq().getPassword(),
                    autoScalingProperties.getRabbitmq().getManagement().getPort(), "/tmp/openbaton/plugin-log/");
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    private void destroyPlugins() {
        PluginStartup.destroy();
    }

    private void waitForNfvo() {
        if (!Utils.isNfvoStarted(nfvoProperties.getIp(), nfvoProperties.getPort())) {
            log.error("After 150 sec the Nfvo is not started yet. Is there an error?");
            System.exit(1); // 1 stands for the error in running nfvo TODO define error codes (doing)
        }
    }

    private void fetchNSRsFromNFVO() {
        log.debug("Fetching previously deployed NSRs from NFVO to start the autoscaling for them.");
        List<NetworkServiceRecord> nsrs = new ArrayList<>();
        try {
            for (Project project : nfvoRequestor.getProjectAgent().findAll()) {
                nfvoRequestor.setProjectId(project.getId());
                nsrs.addAll(nfvoRequestor.getNetworkServiceRecordAgent().findAll());
            }
        } catch (SDKException e) {
            log.warn(
                    "Problem while fetching exisiting NSRs from the Orchestrator to start Autoscaling. Elasticity for previously deployed NSRs will not start",
                    e);
        } catch (ClassNotFoundException e) {
            log.error(e.getMessage(), e);
        }
        for (NetworkServiceRecord nsr : nsrs) {
            try {
                if (nsr.getStatus().ordinal() == Status.ACTIVE.ordinal()
                        || nsr.getStatus().ordinal() == Status.SCALING.ordinal()) {
                    log.debug("Adding previously deployed NSR with id: " + nsr.getId() + " to autoscaling");
                    try {
                        elasticityManagement.activate(nsr);
                    } catch (VimException e) {
                        log.error(e.getMessage(), e);
                    }
                } else {
                    log.warn("Cannot add NSR with id: " + nsr.getId() + " to autoscaling because it is in state: "
                            + nsr.getStatus() + " and not in state " + Status.ACTIVE + " or " + Status.ERROR
                            + ". ");
                }
            } catch (NotFoundException e) {
                log.warn("Not found NSR with id: " + nsr.getId());
            }
        }
    }

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        try {
            exit();
        } catch (SDKException e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void run(String... args) throws Exception {
        init();
    }
}