New KPI Compution MS
[dcaegen2/services.git] / components / kpi-computation-ms / src / main / java / org / onap / dcaegen2 / kpi / Application.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2021 China Mobile.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.kpi;
22
23 import java.io.BufferedReader;
24 import java.io.FileReader;
25 import java.time.Duration;
26
27 import org.onap.dcaegen2.kpi.controller.ConfigFetchFromCbs;
28 import org.onap.dcaegen2.kpi.models.Configuration;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.boot.SpringApplication;
32 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
33 import org.springframework.boot.autoconfigure.SpringBootApplication;
34 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
35 import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
36 import org.springframework.scheduling.annotation.EnableScheduling;
37
38 import com.google.gson.Gson;
39 import com.google.gson.JsonObject;
40
41 /**
42  * Entry point for the kpi computation service application.
43  *
44  * @author Kai Lu
45  *
46  */
47 @EnableScheduling
48 @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
49 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,
50         DataSourceTransactionManagerAutoConfiguration.class })
51 public class Application {
52
53     private static Logger log = LoggerFactory.getLogger(Application.class);
54
55     /**
56      * Main method where initial configuration and context is set.
57      * 
58      * @param args args
59      */
60     public static void main(String[] args) {
61         Boolean standalone = Boolean.parseBoolean(System.getenv("STANDALONE"));
62
63         if (standalone) {
64             String configFile = System.getenv("CONFIG_FILE");
65             getStandaloneConfig(configFile);
66
67         } else {
68             getConfig();
69         }
70
71         log.info("Starting spring boot application");
72         SpringApplication.run(Application.class, args);
73     }
74
75     /**
76      * Get Configuration from config file.
77      * 
78      * @param configFile : location of the config file.
79      */
80     public static void getStandaloneConfig(String configFile) {
81
82         log.info("Running in standalone mode");
83
84         String configAllJson = readFromFile(configFile);
85
86         JsonObject configAll = new Gson().fromJson(configAllJson, JsonObject.class);
87
88         JsonObject config = configAll.getAsJsonObject("config");
89
90         Configuration.getInstance().updateConfigurationFromJsonObject(config);
91
92         return;
93     }
94
95     /**
96      * Get config from cbs.
97      * 
98      */
99     public static void getConfig() {
100
101         ConfigFetchFromCbs configFetchFromCbs = new ConfigFetchFromCbs(Duration.ofSeconds(60));
102         Thread configFetchThread = new Thread(configFetchFromCbs);
103         configFetchThread.start();
104         try {
105             Thread.sleep(10000);
106         } catch (InterruptedException e) {
107             log.debug("InterruptedException : {}", e);
108             Thread.currentThread().interrupt();
109         }
110         log.info("after 10s sleep");
111     }
112
113     private static String readFromFile(String file) {
114         String content = "";
115         try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
116             content = bufferedReader.readLine();
117             String temp;
118             while ((temp = bufferedReader.readLine()) != null) {
119                 content = content.concat(temp);
120             }
121             content = content.trim();
122         } catch (Exception e) {
123             content = null;
124         }
125         return content;
126     }
127
128 }