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 java.util.concurrent.Callable;
20 import java.util.concurrent.ExecutorService;
21 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager;
23 import org.slf4j.Logger;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.boot.autoconfigure.SpringBootApplication;
26 import org.springframework.boot.context.event.ApplicationReadyEvent;
27 import org.springframework.context.ApplicationListener;
28 import org.springframework.context.annotation.Profile;
29 import org.springframework.context.event.ContextClosedEvent;
30 import org.springframework.stereotype.Component;
32 import static java.util.concurrent.Executors.newCachedThreadPool;
34 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
35 import static org.slf4j.LoggerFactory.getLogger;
38 * Represents the spring boot application
40 @SpringBootApplication
41 public class NokiaSvnfmApplication {
42 private static Logger logger = getLogger(NokiaSvnfmApplication.class);
45 * Entry point for the Spring boot application
47 * @param args arguments for the application (not used)
49 public static void main(String[] args) {
50 systemFunctions().newSpringApplication(NokiaSvnfmApplication.class).run(args);
54 * Responsible for starting the self registration process after the servlet has been started
55 * and is ready to answer REST request
56 * - has been disabled in the test because the application that provides the ONAP simulator
57 * has already not yet been started (can not answer REST requests)
61 public static class SelfRegistrationTrigger implements ApplicationListener<ApplicationReadyEvent> {
62 private final SelfRegistrationManager selfRegistrationManager;
63 private final JobManager jobManager;
65 * Runs the registration process
67 private ExecutorService executorService = newCachedThreadPool();
70 SelfRegistrationTrigger(SelfRegistrationManager selfRegistrationManager, JobManager jobManager) {
71 this.jobManager = jobManager;
72 this.selfRegistrationManager = selfRegistrationManager;
76 public void onApplicationEvent(ApplicationReadyEvent contextRefreshedEvent) {
77 Callable<Boolean> singleRegistration = () -> {
78 logger.info("Self registration started");
80 selfRegistrationManager.register();
81 logger.info("Self registration finished");
82 } catch (RuntimeException e) {
83 logger.error("Self registration failed", e);
88 executorService.submit(() -> {
89 while (!jobManager.isPreparingForShutDown()) {
91 executorService.submit(singleRegistration).get();
92 //registration successful
94 } catch (Exception e) {
95 logger.warn("Unable to execute self registration process", e);
97 systemFunctions().sleep(5000);
104 * Responsible for starting the un-registration process after the service has been ramped down
105 * - has been disabled in test because the same application that provides the ONAP simulator
106 * has already been ramped down (can not answer REST requests)
110 public static class SelfDeRegistrationTrigger implements ApplicationListener<ContextClosedEvent> {
111 private final SelfRegistrationManager selfRegistrationManager;
112 private final JobManager jobManager;
115 SelfDeRegistrationTrigger(SelfRegistrationManager selfRegistrationManager, JobManager jobManager) {
116 this.jobManager = jobManager;
117 this.selfRegistrationManager = selfRegistrationManager;
121 public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
122 logger.info("Self de-registration started");
124 jobManager.prepareForShutdown();
125 selfRegistrationManager.deRegister();
126 } catch (RuntimeException e) {
127 logger.error("Self de-registration failed", e);
130 logger.info("Self de-registration finished");