Fix automatic package conversion
[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 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;
31
32 import static java.util.concurrent.Executors.newCachedThreadPool;
33
34 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
35 import static org.slf4j.LoggerFactory.getLogger;
36
37 /**
38  * Represents the spring boot application
39  */
40 @SpringBootApplication
41 public class NokiaSvnfmApplication {
42     private static Logger logger = getLogger(NokiaSvnfmApplication.class);
43
44     /**
45      * Entry point for the Spring boot application
46      *
47      * @param args arguments for the application (not used)
48      */
49     public static void main(String[] args) {
50         systemFunctions().newSpringApplication(NokiaSvnfmApplication.class).run(args);
51     }
52
53     /**
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)
58      */
59     @Component
60     @Profile("!test")
61     public static class SelfRegistrationTrigger implements ApplicationListener<ApplicationReadyEvent> {
62         private final SelfRegistrationManager selfRegistrationManager;
63         private final JobManager jobManager;
64         /**
65          * Runs the registration process
66          */
67         private ExecutorService executorService = newCachedThreadPool();
68
69         @Autowired
70         SelfRegistrationTrigger(SelfRegistrationManager selfRegistrationManager, JobManager jobManager) {
71             this.jobManager = jobManager;
72             this.selfRegistrationManager = selfRegistrationManager;
73         }
74
75         @Override
76         public void onApplicationEvent(ApplicationReadyEvent contextRefreshedEvent) {
77             Callable<Boolean> singleRegistration = () -> {
78                 logger.info("Self registration started");
79                 try {
80                     selfRegistrationManager.register();
81                     logger.info("Self registration finished");
82                 } catch (RuntimeException e) {
83                     logger.error("Self registration failed", e);
84                     throw e;
85                 }
86                 return true;
87             };
88             executorService.submit(() -> {
89                 while (!jobManager.isPreparingForShutDown()) {
90                     try {
91                         executorService.submit(singleRegistration).get();
92                         //registration successful
93                         return;
94                     } catch (Exception e) {
95                         logger.warn("Unable to execute self registration process", e);
96                     }
97                     systemFunctions().sleep(5000);
98                 }
99             });
100         }
101     }
102
103     /**
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)
107      */
108     @Component
109     @Profile("!test")
110     public static class SelfDeRegistrationTrigger implements ApplicationListener<ContextClosedEvent> {
111         private final SelfRegistrationManager selfRegistrationManager;
112         private final JobManager jobManager;
113
114         @Autowired
115         SelfDeRegistrationTrigger(SelfRegistrationManager selfRegistrationManager, JobManager jobManager) {
116             this.jobManager = jobManager;
117             this.selfRegistrationManager = selfRegistrationManager;
118         }
119
120         @Override
121         public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
122             logger.info("Self de-registration started");
123             try {
124                 jobManager.prepareForShutdown();
125                 selfRegistrationManager.deRegister();
126             } catch (RuntimeException e) {
127                 logger.error("Self de-registration failed", e);
128                 throw e;
129             }
130             logger.info("Self de-registration finished");
131         }
132     }
133 }