3e65c8c05bc31013ebd9b657a14c4d4fe72d3dce
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / Application.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
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
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
22 package org.onap.dcaegen2.services.sonhms;
23
24 import com.google.gson.Gson;
25 import com.google.gson.JsonObject;
26 import com.google.gson.reflect.TypeToken;
27
28 import java.io.BufferedReader;
29 import java.io.FileReader;
30 import java.lang.reflect.Type;
31 import java.time.Duration;
32 import java.util.Map;
33
34 import javax.sql.DataSource;
35
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;
43
44
45 @SpringBootApplication
46 public class Application {
47
48     private static Logger log = LoggerFactory.getLogger(Application.class);
49
50     /**
51      * Main method where the pci context is initially set.
52      */
53     public static void main(String[] args) {
54         getConfig();
55         log.info("Starting spring boot application");
56         SpringApplication.run(Application.class);
57
58     }
59
60     private static void getConfig() {
61
62         Boolean standalone = Boolean.parseBoolean(System.getenv("STANDALONE"));
63
64         if (standalone) {
65             log.info("Running in standalone mode");
66
67             String configFile = System.getenv("CONFIG_FILE");
68             String configAllJson = readFromFile(configFile);
69
70             JsonObject configAll = new Gson().fromJson(configAllJson, JsonObject.class);
71
72             JsonObject config = configAll.getAsJsonObject("config");
73
74             Configuration.getInstance().updateConfigurationFromJsonObject(config);
75
76             ConfigPolicy configPolicy = ConfigPolicy.getInstance();
77             Type mapType = new TypeToken<Map<String, Object>>() {
78             }.getType();
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);
85             }
86             return;
87         }
88
89         ConfigFetchFromCbs configFetchFromCbs = new ConfigFetchFromCbs(Duration.ofSeconds(60));
90         Thread configFetchThread = new Thread(configFetchFromCbs);
91         configFetchThread.start();
92         try {
93             Thread.sleep(10000);
94         } catch (InterruptedException e) {
95             log.debug("InterruptedException : {}", e);
96         }
97         log.info("after 10s sleep");
98     }
99
100     /**
101      * DataSource bean.
102      */
103     @Bean
104     public DataSource dataSource() {
105         Configuration configuration = Configuration.getInstance();
106
107         String url = "jdbc:postgresql://" + configuration.getPgHost() + ":" + configuration.getPgPort() + "/sonhms";
108
109         return DataSourceBuilder.create().url(url).username(configuration.getPgUsername())
110                 .password(configuration.getPgPassword()).build();
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 }