2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia;
19 import com.google.common.collect.Sets;
20 import java.util.concurrent.Callable;
21 import java.util.concurrent.ExecutorService;
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManagerForSo;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManagerForVfc;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManagerForSo;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManagerForVfc;
26 import org.slf4j.Logger;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.boot.autoconfigure.SpringBootApplication;
29 import org.springframework.boot.context.event.ApplicationReadyEvent;
30 import org.springframework.context.ApplicationListener;
31 import org.springframework.context.ConfigurableApplicationContext;
32 import org.springframework.context.annotation.Profile;
33 import org.springframework.context.event.ContextClosedEvent;
34 import org.springframework.stereotype.Component;
36 import static java.util.concurrent.Executors.newCachedThreadPool;
38 import static com.google.common.collect.Sets.newHashSet;
39 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
40 import static org.slf4j.LoggerFactory.getLogger;
43 * Represents the spring boot application
45 @SpringBootApplication
46 public class NokiaSvnfmApplication {
47 private static Logger logger = getLogger(NokiaSvnfmApplication.class);
50 * Entry point for the Spring boot application
52 * @param args arguments for the application (not used)
54 public static void main(String[] args) {
55 systemFunctions().newSpringApplication(NokiaSvnfmApplication.class).run(args);
59 * Responsible for starting the self registration process after the servlet has been started
60 * and is ready to answer REST request
61 * - has been disabled in the test because the application that provides the ONAP simulator
62 * has already not yet been started (can not answer REST requests)
66 public static class SelfRegistrationTrigger implements ApplicationListener<ApplicationReadyEvent> {
67 private final SelfRegistrationManagerForSo selfRegistrationManagerForSo;
68 private final SelfRegistrationManagerForVfc selfRegistrationManagerForVfc;
69 private final JobManagerForSo jobManagerForSo;
70 private final JobManagerForVfc jobManagerForVfc;
73 * Runs the registration process
75 private ExecutorService executorService = newCachedThreadPool();
78 SelfRegistrationTrigger(SelfRegistrationManagerForVfc selfRegistrationManagerForVfc, SelfRegistrationManagerForSo selfRegistrationManagerForSo, JobManagerForSo jobManagerForSo, JobManagerForVfc jobManagerForVfc) {
79 this.jobManagerForSo = jobManagerForSo;
80 this.jobManagerForVfc = jobManagerForVfc;
81 this.selfRegistrationManagerForVfc = selfRegistrationManagerForVfc;
82 this.selfRegistrationManagerForSo = selfRegistrationManagerForSo;
85 private static boolean isDirect(ConfigurableApplicationContext applicationContext) {
86 return newHashSet(applicationContext.getEnvironment().getActiveProfiles()).contains("direct");
90 public void onApplicationEvent(ApplicationReadyEvent contextRefreshedEvent) {
91 Callable<Boolean> singleRegistration = () -> {
92 logger.info("Self registration started");
94 if (isDirect(contextRefreshedEvent.getApplicationContext())) {
95 selfRegistrationManagerForSo.register();
97 selfRegistrationManagerForVfc.register();
99 logger.info("Self registration finished");
100 } catch (RuntimeException e) {
101 logger.error("Self registration failed", e);
106 executorService.submit(() -> {
107 boolean notPrepareForShutDown = !jobManagerForVfc.isPreparingForShutDown() && !jobManagerForSo.isPreparingForShutDown();
108 while (notPrepareForShutDown) {
110 executorService.submit(singleRegistration).get();
111 //registration successful
113 } catch (Exception e) {
114 logger.warn("Unable to execute self registration process", e);
117 systemFunctions().sleep(5000);
119 logger.warn("Component is preparing for shutdown giving up component start");
126 * Responsible for starting the un-registration process after the service has been ramped down
127 * - has been disabled in test because the same application that provides the ONAP simulator
128 * has already been ramped down (can not answer REST requests)
132 public static class SelfDeRegistrationTrigger implements ApplicationListener<ContextClosedEvent> {
133 private final JobManagerForSo jobManagerForSo;
134 private final JobManagerForVfc jobManagerForVfc;
135 private final SelfRegistrationManagerForVfc selfRegistrationManagerForVfc;
136 private final SelfRegistrationManagerForSo selfRegistrationManagerForSo;
140 SelfDeRegistrationTrigger(SelfRegistrationManagerForVfc selfRegistrationManager, SelfRegistrationManagerForSo selfRegistrationManagerForSo, JobManagerForSo jobManager, JobManagerForVfc jobManagerForVfc) {
141 this.jobManagerForSo = jobManager;
142 this.jobManagerForVfc = jobManagerForVfc;
143 this.selfRegistrationManagerForVfc = selfRegistrationManager;
144 this.selfRegistrationManagerForSo = selfRegistrationManagerForSo;
148 public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
149 logger.info("Self de-registration started");
151 jobManagerForVfc.prepareForShutdown();
152 jobManagerForSo.prepareForShutdown();
153 if (Sets.newHashSet(contextClosedEvent.getApplicationContext().getEnvironment().getActiveProfiles()).contains("direct")) {
154 selfRegistrationManagerForSo.deRegister();
156 selfRegistrationManagerForVfc.deRegister();
158 } catch (RuntimeException e) {
159 logger.error("Self de-registration failed", e);
162 logger.info("Self de-registration finished");