f1c2a6f082bad0a9858608f08d729f42a691118a
[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 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;
35
36 import static java.util.concurrent.Executors.newCachedThreadPool;
37
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;
41
42 /**
43  * Represents the spring boot application
44  */
45 @SpringBootApplication
46 public class NokiaSvnfmApplication {
47     private static Logger logger = getLogger(NokiaSvnfmApplication.class);
48
49     /**
50      * Entry point for the Spring boot application
51      *
52      * @param args arguments for the application (not used)
53      */
54     public static void main(String[] args) {
55         systemFunctions().newSpringApplication(NokiaSvnfmApplication.class).run(args);
56     }
57
58     /**
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)
63      */
64     @Component
65     @Profile("!test")
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;
71
72         /**
73          * Runs the registration process
74          */
75         private ExecutorService executorService = newCachedThreadPool();
76
77         @Autowired
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;
83         }
84
85         @Override
86         public void onApplicationEvent(ApplicationReadyEvent contextRefreshedEvent) {
87             Callable<Boolean> singleRegistration = () -> {
88                 logger.info("Self registration started");
89                 try {
90                     if (isDirect(contextRefreshedEvent.getApplicationContext())) {
91                         selfRegistrationManagerForSo.register();
92                     } else {
93                         selfRegistrationManagerForVfc.register();
94                     }
95                     logger.info("Self registration finished");
96                 } catch (RuntimeException e) {
97                     logger.error("Self registration failed", e);
98                     throw e;
99                 }
100                 return true;
101             };
102             executorService.submit(() -> {
103                 boolean notPrepareForShutDown = !jobManagerForVfc.isPreparingForShutDown() && !jobManagerForSo.isPreparingForShutDown();
104                 while (notPrepareForShutDown) {
105                     try {
106                         executorService.submit(singleRegistration).get();
107                         //registration successful
108                         return;
109                     } catch (Exception e) {
110                         logger.warn("Unable to execute self registration process", e);
111                     }
112
113                     systemFunctions().sleep(5000);
114                 }
115                 logger.warn("Component is preparing for shutdown giving up component start");
116             });
117         }
118
119         private static boolean isDirect(ConfigurableApplicationContext applicationContext) {
120             return newHashSet(applicationContext.getEnvironment().getActiveProfiles()).contains("direct");
121         }
122
123     }
124
125     /**
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)
129      */
130     @Component
131     @Profile("!test")
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;
137
138
139         @Autowired
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;
145         }
146
147         @Override
148         public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
149             logger.info("Self de-registration started");
150             try {
151                 jobManagerForVfc.prepareForShutdown();
152                 jobManagerForSo.prepareForShutdown();
153                 if (Sets.newHashSet(contextClosedEvent.getApplicationContext().getEnvironment().getActiveProfiles()).contains("direct")) {
154                     selfRegistrationManagerForSo.deRegister();
155                 } else {
156                     selfRegistrationManagerForVfc.deRegister();
157                 }
158             } catch (RuntimeException e) {
159                 logger.error("Self de-registration failed", e);
160                 throw e;
161             }
162             logger.info("Self de-registration finished");
163         }
164     }
165 }