6296d4b29a1a9fd01522bd9c1f9efe7f9d3152a3
[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 org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
20 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager;
21 import org.slf4j.Logger;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.boot.SpringApplication;
24 import org.springframework.boot.autoconfigure.SpringBootApplication;
25 import org.springframework.boot.context.event.ApplicationReadyEvent;
26 import org.springframework.context.ApplicationListener;
27 import org.springframework.context.annotation.Profile;
28 import org.springframework.context.event.ContextClosedEvent;
29 import org.springframework.stereotype.Component;
30
31 import static org.slf4j.LoggerFactory.getLogger;
32
33 /**
34  * Represents the spring boot application
35  */
36 @SpringBootApplication
37 public class NokiaSvnfmApplication {
38     private static Logger logger = getLogger(NokiaSvnfmApplication.class);
39
40     /**
41      * Entry point for the Spring boot application
42      *
43      * @param args arguments for the application (not used)
44      */
45     public static void main(String[] args) {
46         SpringApplication.run(NokiaSvnfmApplication.class, args);
47     }
48
49     /**
50      * Responsible for starting the self registration process after the servlet has been started
51      * and is ready to answer REST request
52      * - has been disabled in the test because the application that provides the ONAP simulator
53      * has already not yet been started (can not answer REST requests)
54      */
55     @Component
56     @Profile("!test")
57     public static class SelfRegistrationTrigger implements ApplicationListener<ApplicationReadyEvent> {
58         @Autowired
59         private SelfRegistrationManager selfRegistrationManager;
60
61         @Override
62         public void onApplicationEvent(ApplicationReadyEvent contextRefreshedEvent) {
63             logger.info("Self registration started");
64             try {
65                 selfRegistrationManager.register();
66                 logger.info("Self registration finished");
67             } catch (RuntimeException e) {
68                 logger.error("Self registration failed", e);
69                 throw e;
70             }
71         }
72     }
73
74     /**
75      * Responsible for starting the un-registration process after the service has been ramped down
76      * - has been disabled in test because the same application that provides the ONAP simulator
77      * has already been ramped down (can not answer REST requests)
78      */
79     @Component
80     @Profile("!test")
81     public static class SelfDeRegistrationTrigger implements ApplicationListener<ContextClosedEvent> {
82         @Autowired
83         private SelfRegistrationManager selfRegistrationManager;
84         @Autowired
85         private JobManager jobManager;
86
87         @Override
88         public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
89             logger.info("Self de-registration started");
90             try {
91                 jobManager.prepareForShutdown();
92                 selfRegistrationManager.deRegister();
93             } catch (RuntimeException e) {
94                 logger.error("Self de-registration failed", e);
95                 throw e;
96             }
97             logger.info("Self de-registration finished");
98         }
99     }
100 }