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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.kpi;
23 import java.io.BufferedReader;
24 import java.io.FileReader;
25 import java.time.Duration;
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;
38 import com.google.gson.Gson;
39 import com.google.gson.JsonObject;
42 * Entry point for the kpi computation service application.
48 @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
49 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,
50 DataSourceTransactionManagerAutoConfiguration.class })
51 public class Application {
53 private static Logger log = LoggerFactory.getLogger(Application.class);
56 * Main method where initial configuration and context is set.
60 public static void main(String[] args) {
61 Boolean standalone = Boolean.parseBoolean(System.getenv("STANDALONE"));
64 String configFile = System.getenv("CONFIG_FILE");
65 getStandaloneConfig(configFile);
71 log.info("Starting spring boot application");
72 SpringApplication.run(Application.class, args);
76 * Get Configuration from config file.
78 * @param configFile : location of the config file.
80 public static void getStandaloneConfig(String configFile) {
82 log.info("Running in standalone mode");
84 String configAllJson = readFromFile(configFile);
86 JsonObject configAll = new Gson().fromJson(configAllJson, JsonObject.class);
88 JsonObject config = configAll.getAsJsonObject("config");
90 Configuration.getInstance().updateConfigurationFromJsonObject(config);
96 * Get config from cbs.
99 public static void getConfig() {
101 ConfigFetchFromCbs configFetchFromCbs = new ConfigFetchFromCbs(Duration.ofSeconds(60));
102 Thread configFetchThread = new Thread(configFetchFromCbs);
103 configFetchThread.start();
106 } catch (InterruptedException e) {
107 log.debug("InterruptedException : {}", e);
108 Thread.currentThread().interrupt();
110 log.info("after 10s sleep");
113 private static String readFromFile(String file) {
115 try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
116 content = bufferedReader.readLine();
118 while ((temp = bufferedReader.readLine()) != null) {
119 content = content.concat(temp);
121 content = content.trim();
122 } catch (Exception e) {