Fix container startup
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / NokiaSvnfmApplication.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia;
18
19 import java.util.concurrent.Callable;
20 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.Future;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager;
26 import org.slf4j.Logger;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.boot.SpringApplication;
29 import org.springframework.boot.autoconfigure.SpringBootApplication;
30 import org.springframework.boot.context.event.ApplicationReadyEvent;
31 import org.springframework.context.ApplicationListener;
32 import org.springframework.context.annotation.Profile;
33 import org.springframework.context.event.ContextClosedEvent;
34 import org.springframework.stereotype.Component;
35
36 import static java.util.concurrent.Executors.newCachedThreadPool;
37
38 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
39 import static org.slf4j.LoggerFactory.getLogger;
40
41 /**
42  * Represents the spring boot application
43  */
44 @SpringBootApplication
45 public class NokiaSvnfmApplication {
46     private static Logger logger = getLogger(NokiaSvnfmApplication.class);
47
48     /**
49      * Entry point for the Spring boot application
50      *
51      * @param args arguments for the application (not used)
52      */
53     public static void main(String[] args) {
54         systemFunctions().newSpringApplication(NokiaSvnfmApplication.class).run(args);
55     }
56
57     /**
58      * Responsible for starting the self registration process after the servlet has been started
59      * and is ready to answer REST request
60      * - has been disabled in the test because the application that provides the ONAP simulator
61      * has already not yet been started (can not answer REST requests)
62      */
63     @Component
64     @Profile("!test")
65     public static class SelfRegistrationTrigger implements ApplicationListener<ApplicationReadyEvent> {
66         private final SelfRegistrationManager selfRegistrationManager;
67         private final JobManager jobManager;
68         /**
69          * Runs the registration process
70          */
71         private ExecutorService executorService = newCachedThreadPool();
72
73         @Autowired
74         SelfRegistrationTrigger(SelfRegistrationManager selfRegistrationManager, JobManager jobManager){
75             this.jobManager = jobManager;
76             this.selfRegistrationManager = selfRegistrationManager;
77         }
78
79         @Override
80         public void onApplicationEvent(ApplicationReadyEvent contextRefreshedEvent) {
81             Callable<Boolean> singleRegistration = () -> {
82                 logger.info("Self registration started");
83                 try {
84                     selfRegistrationManager.register();
85                     logger.info("Self registration finished");
86                 } catch (RuntimeException e) {
87                     logger.error("Self registration failed", e);
88                     throw e;
89                 }
90                 return true;
91             };
92             executorService.submit(() -> {
93                 while(!jobManager.isPreparingForShutDown()){
94                     try{
95                         executorService.submit(singleRegistration).get();
96                         //registration successful
97                         return;
98                     }
99                     catch (Exception e){
100                         logger.warn("Unable to execute self registration process", e);
101                     }
102                     systemFunctions().sleep(5000);
103                 }
104             });
105         }
106     }
107
108     /**
109      * Responsible for starting the un-registration process after the service has been ramped down
110      * - has been disabled in test because the same application that provides the ONAP simulator
111      * has already been ramped down (can not answer REST requests)
112      */
113     @Component
114     @Profile("!test")
115     public static class SelfDeRegistrationTrigger implements ApplicationListener<ContextClosedEvent> {
116         private final SelfRegistrationManager selfRegistrationManager;
117         private final JobManager jobManager;
118
119         @Autowired
120         SelfDeRegistrationTrigger(SelfRegistrationManager selfRegistrationManager, JobManager jobManager){
121             this.jobManager = jobManager;
122             this.selfRegistrationManager = selfRegistrationManager;
123         }
124
125         @Override
126         public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
127             logger.info("Self de-registration started");
128             try {
129                 jobManager.prepareForShutdown();
130                 selfRegistrationManager.deRegister();
131             } catch (RuntimeException e) {
132                 logger.error("Self de-registration failed", e);
133                 throw e;
134             }
135             logger.info("Self de-registration finished");
136         }
137     }
138 }