a7426be2522d2b55a68a95e443cc953209b76d1f
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcae;
22
23 import io.vavr.collection.Map;
24 import org.json.JSONObject;
25 import org.onap.dcae.commonFunction.EventProcessor;
26 import org.onap.dcae.commonFunction.event.publishing.DMaaPConfigurationParser;
27 import org.onap.dcae.commonFunction.event.publishing.EventPublisher;
28 import org.onap.dcae.commonFunction.event.publishing.PublisherConfig;
29 import org.onap.dcae.controller.ConfigLoader;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Qualifier;
33 import org.springframework.boot.Banner;
34 import org.springframework.boot.SpringApplication;
35 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
36 import org.springframework.boot.autoconfigure.SpringBootApplication;
37 import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
38 import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
39 import org.springframework.context.annotation.Bean;
40 import org.springframework.context.annotation.Lazy;
41
42 import java.nio.file.Paths;
43 import java.util.concurrent.*;
44
45 @SpringBootApplication(exclude = {GsonAutoConfiguration.class, SecurityAutoConfiguration.class})
46 public class VesApplication {
47
48     private static final Logger metriclog = LoggerFactory.getLogger("com.att.ecomp.metrics");
49     private static final Logger incomingRequestsLogger = LoggerFactory.getLogger("org.onap.dcae.commonFunction.input");
50     private static final Logger oplog = LoggerFactory.getLogger("org.onap.dcae.commonFunction.output");
51     private static final Logger errorLog = LoggerFactory.getLogger("org.onap.dcae.commonFunction.error");
52     private static final int MAX_THREADS = 20;
53     public static LinkedBlockingQueue<JSONObject> fProcessingInputQueue;
54     private static ApplicationSettings properties;
55
56     public static void main(String[] args) {
57         SpringApplication app = new SpringApplication(VesApplication.class);
58
59         properties = new ApplicationSettings(args, CLIUtils::processCmdLine);
60
61         fProcessingInputQueue = new LinkedBlockingQueue<>(properties.maximumAllowedQueuedEvents());
62
63         EventPublisher publisher = EventPublisher.createPublisher(oplog,
64                 DMaaPConfigurationParser
65                         .parseToDomainMapping(Paths.get(properties.dMaaPConfigurationFileLocation()))
66                         .get());
67         spawnDynamicConfigUpdateThread(publisher, properties);
68         EventProcessor ep = new EventProcessor(EventPublisher.createPublisher(oplog, getDmapConfig()), properties);
69
70         ExecutorService executor = Executors.newFixedThreadPool(MAX_THREADS);
71         for (int i = 0; i < MAX_THREADS; ++i) {
72             executor.execute(ep);
73         }
74
75         app.setBannerMode(Banner.Mode.OFF);
76         app.setAddCommandLineProperties(true);
77         app.run();
78     }
79
80     private static void spawnDynamicConfigUpdateThread(EventPublisher eventPublisher, ApplicationSettings properties) {
81         ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
82         ConfigLoader configLoader = ConfigLoader
83                 .create(eventPublisher::reconfigure,
84                         Paths.get(properties.dMaaPConfigurationFileLocation()),
85                         properties.configurationFileLocation());
86         scheduledThreadPoolExecutor
87                 .scheduleAtFixedRate(() -> configLoader.updateConfig(),
88                         properties.configurationUpdateFrequency(),
89                         properties.configurationUpdateFrequency(),
90                         TimeUnit.MINUTES);
91     }
92
93     private static Map<String, PublisherConfig> getDmapConfig() {
94         return DMaaPConfigurationParser.
95                 parseToDomainMapping(Paths.get(properties.dMaaPConfigurationFileLocation())).get();
96     }
97
98     @Bean
99     @Lazy
100     public ApplicationSettings applicationSettings() {
101         return properties;
102     }
103
104     @Bean
105     @Qualifier("incomingRequestsLogger")
106     public Logger incomingRequestsLogger() {
107         return incomingRequestsLogger;
108     }
109
110     @Bean
111     @Qualifier("metricsLog")
112     public Logger incomingRequestsMetricsLogger() {
113         return metriclog;
114     }
115
116     @Bean
117     @Qualifier("errorLog")
118     public Logger errorLogger() {
119         return errorLog;
120     }
121
122     @Bean
123     public LinkedBlockingQueue<JSONObject> inputQueue() {
124         return fProcessingInputQueue;
125     }
126
127 }