2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.dcae.controller;
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;
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;
36 public class LoadDynamicConfig {
38 private static final Logger log = LoggerFactory.getLogger(LoadDynamicConfig.class);
40 public String propFile = "collector.properties";
41 public String configFile = "/opt/app/KV-Configuration.json";
42 public String dMaaPOutputFile = "./etc/DmaapConfig.json";
44 public LoadDynamicConfig() {
48 public static void main(String[] args) {
49 Map<String, String> env = System.getenv();
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")) {
57 LoadDynamicConfig lc = new LoadDynamicConfig();
58 String jsonData = readFile(lc.configFile);
59 JSONObject jsonObject = new JSONObject(jsonData);
60 lc.writeconfig(jsonObject);
63 } catch (Exception e) {
64 log.error(e.getLocalizedMessage(), e);
70 log.info(">>>Static configuration to be used");
75 public static String readFile(String filename) {
77 try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
78 StringBuilder sb = new StringBuilder();
79 String line = br.readLine();
80 while (line != null) {
84 result = sb.toString();
85 } catch (Exception e) {
86 log.error(e.getLocalizedMessage(), e);
92 public void writeconfig(JSONObject jsonObject) {
94 PropertiesConfiguration conf;
96 conf = new PropertiesConfiguration(propFile);
98 conf.setEncoding(null);
100 // update properties based on consul dynamic configuration
101 Iterator<?> keys = jsonObject.keys();
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);
117 conf.setProperty(key, jsonObject.get(key).toString());
122 } catch (ConfigurationException e) {
123 log.error(e.getLocalizedMessage(), e);