Config fetch for VESCollector through DCAE-SDK (CBS Client)
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / VesApplication.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2020 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.dcae;
23
24 import io.vavr.collection.Map;
25 import org.onap.dcae.common.EventSender;
26 import org.onap.dcae.common.validator.StndDefinedValidatorResolver;
27 import org.onap.dcae.common.publishing.DMaaPConfigurationParser;
28 import org.onap.dcae.common.publishing.DMaaPEventPublisher;
29 import org.onap.dcae.common.publishing.PublisherConfig;
30 import org.onap.dcae.configuration.ConfigLoader;
31 import org.onap.dcae.configuration.ConfigLoaderFactory;
32 import org.onap.dcaegen2.services.sdk.services.external.schema.manager.service.StndDefinedValidator;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Qualifier;
36 import org.springframework.boot.SpringApplication;
37 import org.springframework.boot.autoconfigure.SpringBootApplication;
38 import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
39 import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
40 import org.springframework.context.ConfigurableApplicationContext;
41 import org.springframework.context.annotation.Bean;
42 import org.springframework.context.annotation.Lazy;
43
44 import java.nio.file.Paths;
45 import java.util.concurrent.ScheduledFuture;
46 import java.util.concurrent.ScheduledThreadPoolExecutor;
47 import java.util.concurrent.TimeUnit;
48
49 @SpringBootApplication(exclude = {GsonAutoConfiguration.class, SecurityAutoConfiguration.class})
50 public class VesApplication {
51
52     private static final Logger incomingRequestsLogger = LoggerFactory.getLogger("org.onap.dcae.common.input");
53     private static final Logger errorLog = LoggerFactory.getLogger("org.onap.dcae.common.error");
54     private static ApplicationSettings applicationSettings;
55     private static ConfigurableApplicationContext context;
56     private static ConfigLoader configLoader;
57     private static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;
58     private static DMaaPEventPublisher eventPublisher;
59     private static ScheduledFuture<?> scheduleFeatures;
60
61     public static void main(String[] args) {
62         SpringApplication app = new SpringApplication(VesApplication.class);
63         applicationSettings = new ApplicationSettings(args, CLIUtils::processCmdLine);
64         scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
65         init();
66         app.setAddCommandLineProperties(true);
67         context = app.run();
68         configLoader.updateConfig();
69     }
70
71     public static void restartApplication() {
72         Thread thread = new Thread(() -> {
73             context.close();
74             applicationSettings.reloadProperties();
75             scheduleFeatures.cancel(true);
76             init();
77             context = SpringApplication.run(VesApplication.class);
78         });
79         thread.setDaemon(false);
80         thread.start();
81     }
82
83     private static void init() {
84         createConfigLoader();
85         createSchedulePoolExecutor();
86         createExecutors();
87     }
88
89     private static void createExecutors() {
90         eventPublisher = new DMaaPEventPublisher(getDmaapConfig());
91     }
92
93     private static void createSchedulePoolExecutor() {
94         scheduleFeatures = scheduledThreadPoolExecutor.scheduleAtFixedRate(configLoader::updateConfig,
95                 applicationSettings.configurationUpdateFrequency(),
96                 applicationSettings.configurationUpdateFrequency(),
97                 TimeUnit.MINUTES);
98     }
99
100     private static void createConfigLoader() {
101         configLoader = ConfigLoaderFactory.create(
102                 applicationSettings.configurationFileLocation(),
103                 Paths.get(applicationSettings.dMaaPConfigurationFileLocation()));
104     }
105
106     private static Map<String, PublisherConfig> getDmaapConfig() {
107         return DMaaPConfigurationParser
108                 .parseToDomainMapping(Paths.get(applicationSettings.dMaaPConfigurationFileLocation())).get();
109     }
110
111     @Bean
112     @Lazy
113     public ApplicationSettings applicationSettings() {
114         return applicationSettings;
115     }
116
117     @Bean
118     @Qualifier("incomingRequestsLogger")
119     public Logger incomingRequestsLogger() {
120         return incomingRequestsLogger;
121     }
122
123     @Bean
124     @Qualifier("errorLog")
125     public Logger errorLogger() {
126         return errorLog;
127     }
128
129     @Bean
130     @Qualifier("eventSender")
131     public EventSender eventSender() {
132         return new EventSender(eventPublisher, applicationSettings.getDmaapStreamIds());
133     }
134
135     @Bean
136     public StndDefinedValidator getStndDefinedValidator(StndDefinedValidatorResolver resolver) {
137         return resolver.resolve();
138     }
139
140 }