Merge "Replace nsaCore library with Spring"
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / controller / LoadDynamicConfig.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.controller;
22
23 import org.apache.commons.configuration.ConfigurationException;
24 import org.apache.commons.configuration.PropertiesConfiguration;
25 import org.json.JSONObject;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.io.BufferedReader;
30 import java.io.FileReader;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.util.Iterator;
34 import java.util.Map;
35
36 public class LoadDynamicConfig {
37
38     private static final Logger log = LoggerFactory.getLogger(LoadDynamicConfig.class);
39
40     public String propFile = "collector.properties";
41     public String configFile = "/opt/app/KV-Configuration.json";
42     public String dMaaPOutputFile = "./etc/DmaapConfig.json";
43
44     public LoadDynamicConfig() {
45
46     }
47
48     public static void main(String[] args) {
49         Map<String, String> env = System.getenv();
50
51         // Check again to ensure new controller deployment related config
52         if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE")
53                 && env.containsKey("HOSTNAME")) {
54
55             try {
56
57                 LoadDynamicConfig lc = new LoadDynamicConfig();
58                 String jsonData = readFile(lc.configFile);
59                 JSONObject jsonObject = new JSONObject(jsonData);
60                 lc.writeconfig(jsonObject);
61
62
63             } catch (Exception e) {
64                 log.error(e.getLocalizedMessage(), e);
65                 e.printStackTrace();
66
67             }
68
69         } else {
70             log.info(">>>Static configuration to be used");
71         }
72
73     }
74
75     public static String readFile(String filename) {
76         String result = "";
77         try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
78             StringBuilder sb = new StringBuilder();
79             String line = br.readLine();
80             while (line != null) {
81                 sb.append(line);
82                 line = br.readLine();
83             }
84             result = sb.toString();
85         } catch (Exception e) {
86             log.error(e.getLocalizedMessage(), e);
87             e.printStackTrace();
88         }
89         return result;
90     }
91
92     public void writeconfig(JSONObject jsonObject) {
93
94         PropertiesConfiguration conf;
95         try {
96             conf = new PropertiesConfiguration(propFile);
97
98             conf.setEncoding(null);
99
100             // update properties based on consul dynamic configuration
101             Iterator<?> keys = jsonObject.keys();
102
103             while (keys.hasNext()) {
104                 String key = (String) keys.next();
105                 // check if any configuration is related to dmaap
106                 // and write into dmaapconfig.json
107                 if (key.startsWith("streams_publishes")) {
108                     // VESCollector only have publish streams
109                     try (FileWriter file = new FileWriter(dMaaPOutputFile)) {
110                         String indentedretstring = (new JSONObject(jsonObject.get(key).toString())).toString(4);
111                         file.write(indentedretstring);
112                         log.info("Successfully written JSON Object to DmaapConfig.json");
113                     } catch (IOException e) {
114                         log.info("Error in writing dmaap configuration into DmaapConfig.json", e);
115                     }
116                 } else {
117                     conf.setProperty(key, jsonObject.get(key).toString());
118                 }
119
120             }
121             conf.save();
122         } catch (ConfigurationException e) {
123             log.error(e.getLocalizedMessage(), e);
124             e.printStackTrace();
125         }
126     }
127
128 }