summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/NokiaSvnfmApplication.java
blob: 9124010389c7a9b1a7bd5d788a8e7f57dbf7693a (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * 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 com.google.common.collect.Sets;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManagerForSo;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManagerForVfc;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManagerForSo;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManagerForVfc;
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.ConfigurableApplicationContext;
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 com.google.common.collect.Sets.newHashSet;
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 SelfRegistrationManagerForSo selfRegistrationManagerForSo;
        private final SelfRegistrationManagerForVfc selfRegistrationManagerForVfc;
        private final JobManagerForSo jobManagerForSo;
        private final JobManagerForVfc jobManagerForVfc;

        /**
         * Runs the registration process
         */
        private ExecutorService executorService = newCachedThreadPool();

        @Autowired
        SelfRegistrationTrigger(SelfRegistrationManagerForVfc selfRegistrationManagerForVfc, SelfRegistrationManagerForSo selfRegistrationManagerForSo, JobManagerForSo jobManagerForSo, JobManagerForVfc jobManagerForVfc) {
            this.jobManagerForSo = jobManagerForSo;
            this.jobManagerForVfc = jobManagerForVfc;
            this.selfRegistrationManagerForVfc = selfRegistrationManagerForVfc;
            this.selfRegistrationManagerForSo = selfRegistrationManagerForSo;
        }

        private static boolean isDirect(ConfigurableApplicationContext applicationContext) {
            return newHashSet(applicationContext.getEnvironment().getActiveProfiles()).contains("direct");
        }

        @Override
        public void onApplicationEvent(ApplicationReadyEvent contextRefreshedEvent) {
            Callable<Boolean> singleRegistration = () -> {
                logger.info("Self registration started");
                try {
                    if (isDirect(contextRefreshedEvent.getApplicationContext())) {
                        selfRegistrationManagerForSo.register();
                    } else {
                        selfRegistrationManagerForVfc.register();
                    }
                    logger.info("Self registration finished");
                } catch (RuntimeException e) {
                    logger.error("Self registration failed", e);
                    throw e;
                }
                return true;
            };
            executorService.submit(() -> {
                boolean notPrepareForShutDown = !jobManagerForVfc.isPreparingForShutDown() && !jobManagerForSo.isPreparingForShutDown();
                while (notPrepareForShutDown) {
                    try {
                        executorService.submit(singleRegistration).get();
                        //registration successful
                        return;
                    } catch (Exception e) {
                        logger.warn("Unable to execute self registration process", e);
                    }

                    systemFunctions().sleep(5000);
                }
                logger.warn("Component is preparing for shutdown giving up component start");
            });
        }

    }

    /**
     * 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 JobManagerForSo jobManagerForSo;
        private final JobManagerForVfc jobManagerForVfc;
        private final SelfRegistrationManagerForVfc selfRegistrationManagerForVfc;
        private final SelfRegistrationManagerForSo selfRegistrationManagerForSo;


        @Autowired
        SelfDeRegistrationTrigger(SelfRegistrationManagerForVfc selfRegistrationManager, SelfRegistrationManagerForSo selfRegistrationManagerForSo, JobManagerForSo jobManager, JobManagerForVfc jobManagerForVfc) {
            this.jobManagerForSo = jobManager;
            this.jobManagerForVfc = jobManagerForVfc;
            this.selfRegistrationManagerForVfc = selfRegistrationManager;
            this.selfRegistrationManagerForSo = selfRegistrationManagerForSo;
        }

        @Override
        public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
            logger.info("Self de-registration started");
            try {
                jobManagerForVfc.prepareForShutdown();
                jobManagerForSo.prepareForShutdown();
                if (Sets.newHashSet(contextClosedEvent.getApplicationContext().getEnvironment().getActiveProfiles()).contains("direct")) {
                    selfRegistrationManagerForSo.deRegister();
                } else {
                    selfRegistrationManagerForVfc.deRegister();
                }
            } catch (RuntimeException e) {
                logger.error("Self de-registration failed", e);
                throw e;
            }
            logger.info("Self de-registration finished");
        }
    }
}