1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 Wipro Limited.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 *******************************************************************************/
22 package org.onap.dcaegen2.services.sonhms;
24 import com.google.gson.Gson;
25 import com.google.gson.JsonObject;
26 import com.google.gson.reflect.TypeToken;
28 import java.io.BufferedReader;
29 import java.io.FileReader;
30 import java.lang.reflect.Type;
31 import java.time.Duration;
34 import javax.sql.DataSource;
36 import org.onap.dcaegen2.services.sonhms.controller.ConfigFetchFromCbs;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.boot.SpringApplication;
40 import org.springframework.boot.autoconfigure.SpringBootApplication;
41 import org.springframework.boot.jdbc.DataSourceBuilder;
42 import org.springframework.context.annotation.Bean;
45 @SpringBootApplication
46 public class Application {
48 private static Logger log = LoggerFactory.getLogger(Application.class);
51 * Main method where the pci context is initially set.
53 public static void main(String[] args) {
55 log.info("Starting spring boot application");
56 SpringApplication.run(Application.class);
60 private static void getConfig() {
62 Boolean standalone = Boolean.parseBoolean(System.getenv("STANDALONE"));
65 log.info("Running in standalone mode");
67 String configFile = System.getenv("CONFIG_FILE");
68 String configAllJson = readFromFile(configFile);
70 JsonObject configAll = new Gson().fromJson(configAllJson, JsonObject.class);
72 JsonObject config = configAll.getAsJsonObject("config");
74 Configuration.getInstance().updateConfigurationFromJsonObject(config);
76 ConfigPolicy configPolicy = ConfigPolicy.getInstance();
77 Type mapType = new TypeToken<Map<String, Object>>() {
79 if (configAll.getAsJsonObject("policies") != null) {
80 JsonObject policyJson = configAll.getAsJsonObject("policies").getAsJsonArray("items").get(0)
81 .getAsJsonObject().getAsJsonObject("config");
82 Map<String, Object> policy = new Gson().fromJson(policyJson, mapType);
83 configPolicy.setConfig(policy);
84 log.info("Config policy {}", configPolicy);
89 ConfigFetchFromCbs configFetchFromCbs = new ConfigFetchFromCbs(Duration.ofSeconds(60));
90 Thread configFetchThread = new Thread(configFetchFromCbs);
91 configFetchThread.start();
94 } catch (InterruptedException e) {
95 log.debug("InterruptedException : {}", e);
97 log.info("after 10s sleep");
104 public DataSource dataSource() {
105 Configuration configuration = Configuration.getInstance();
107 String url = "jdbc:postgresql://" + configuration.getPgHost() + ":" + configuration.getPgPort() + "/sonhms";
109 return DataSourceBuilder.create().url(url).username(configuration.getPgUsername())
110 .password(configuration.getPgPassword()).build();
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) {