summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/NokiaSvnfmApplication.java
blob: 23c2e4bf6ff30a652009070539b676ca4a73ca08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright 2016-2017, Nokia Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import static java.util.concurrent.Executors.newCachedThreadPool;

import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
import static org.slf4j.LoggerFactory.getLogger;

/**
 * Represents the spring boot application
 */
@SpringBootApplication
public class NokiaSvnfmApplication {
    private static Logger logger = getLogger(NokiaSvnfmApplication.class);

    /**
     * Entry point for the Spring boot application
     *
     * @param args arguments for the application (not used)
     */
    public static void main(String[] args) {
        systemFunctions().newSpringApplication(NokiaSvnfmApplication.class).run(args);
    }

    /**
     * Responsible for starting the self registration process after the servlet has been started
     * and is ready to answer REST request
     * - has been disabled in the test because the application that provides the ONAP simulator
     * has already not yet been started (can not answer REST requests)
     */
    @Component
    @Profile("!test")
    public static class SelfRegistrationTrigger implements ApplicationListener<ApplicationReadyEvent> {
        private final SelfRegistrationManager selfRegistrationManager;
        private final JobManager jobManager;
        /**
         * Runs the registration process
         */
        private ExecutorService executorService = newCachedThreadPool();

        @Autowired
        SelfRegistrationTrigger(SelfRegistrationManager selfRegistrationManager, JobManager jobManager) {
            this.jobManager = jobManager;
            this.selfRegistrationManager = selfRegistrationManager;
        }

        @Override
        public void onApplicationEvent(ApplicationReadyEvent contextRefreshedEvent) {
            Callable<Boolean> singleRegistration = () -> {
                logger.info("Self registration started");
                try {
                    selfRegistrationManager.register();
                    logger.info("Self registration finished");
                } catch (RuntimeException e) {
                    logger.error("Self registration failed", e);
                    throw e;
                }
                return true;
            };
            executorService.submit(() -> {
                while (!jobManager.isPreparingForShutDown()) {
                    try {
                        executorService.submit(singleRegistration).get();
                        //registration successful
                        return;
                    } catch (Exception e) {
                        logger.warn("Unable to execute self registration process", e);
                    }
                    systemFunctions().sleep(5000);
                }
            });
        }
    }

    /**
     * Responsible for starting the un-registration process after the service has been ramped down
     * - has been disabled in test because the same application that provides the ONAP simulator
     * has already been ramped down (can not answer REST requests)
     */
    @Component
    @Profile("!test")
    public static class SelfDeRegistrationTrigger implements ApplicationListener<ContextClosedEvent> {
        private final SelfRegistrationManager selfRegistrationManager;
        private final JobManager jobManager;

        @Autowired
        SelfDeRegistrationTrigger(SelfRegistrationManager selfRegistrationManager, JobManager jobManager) {
            this.jobManager = jobManager;
            this.selfRegistrationManager = selfRegistrationManager;
        }

        @Override
        public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
            logger.info("Self de-registration started");
            try {
                jobManager.prepareForShutdown();
                selfRegistrationManager.deRegister();
            } catch (RuntimeException e) {
                logger.error("Self de-registration failed", e);
                throw e;
            }
            logger.info("Self de-registration finished");
        }
    }
}