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