2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2018-2019 TechMahindra
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.universalvesadapter.utils;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
27 import java.net.URISyntaxException;
28 import java.nio.file.Files;
29 import java.util.HashMap;
31 import org.json.simple.JSONArray;
32 import org.json.simple.JSONObject;
33 import org.json.simple.parser.JSONParser;
34 import org.json.simple.parser.ParseException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
39 import org.springframework.util.ResourceUtils;
40 import com.fasterxml.jackson.databind.JsonNode;
41 import com.fasterxml.jackson.databind.ObjectMapper;
44 public class CollectorConfigPropertyRetrieval {
46 private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");
47 private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
48 private static JSONArray array;
50 private DmaapConfig dmaapConfig;
52 public static JSONArray collectorConfigArray(String configFile) {
54 JSONParser parser = new JSONParser();
55 String content = readFile(configFile);
56 JSONObject obj = (JSONObject) parser.parse(content);
57 JSONObject appobj = (JSONObject) obj.get("app_preferences");
58 array = (JSONArray) appobj.get("collectors");
60 debugLogger.info("Retrieved JsonArray from Collector Config File");
62 } catch (ParseException e) {
63 errorLogger.error("ParseException occured at position:", e);
70 public static String[] getProperyArray(String propertyName, String defaultConfigFilelocation) {
71 JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);
73 String[] propertyArray = new String[jsonArray.size()];
75 for (int k = 0; k < jsonArray.size(); k++) {
77 JSONObject collJson = (JSONObject) jsonArray.get(k);
79 propertyArray[k] = (String) collJson.get(propertyName);
81 debugLogger.info("returning {} array from Collector Config", propertyName);
86 public Map<String, String> getDmaapTopics(String subscriber, String publisher,
87 String defaultConfigFilelocation) {
88 JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);
90 Map<String, String> dmaapTopics = new HashMap<>();
92 for (int k = 0; k < jsonArray.size(); k++) {
94 JSONObject collJson = (JSONObject) jsonArray.get(k);
96 dmaapTopics.put(collJson.get(subscriber).toString(),
97 collJson.get(publisher).toString());
100 debugLogger.info("returning Dmaap topics from Collector Config");
105 public Map<String, String> getTopics(String subscriber, String publisher,
106 String defaultConfigFilelocation) {
107 Map<String, String> dmaapTopics = new HashMap<>();
111 ObjectMapper objectMapper = new ObjectMapper();
112 String content = readFile(defaultConfigFilelocation);
113 // read JSON like DOM Parser
114 JsonNode rootNode = objectMapper.readTree(content);
115 JsonNode subscriberUrl = rootNode.path("streams_subscribes").path(subscriber)
116 .path("dmaap_info").path("topic_url");
117 JsonNode publisherUrl = rootNode.path("streams_publishes").path(publisher)
118 .path("dmaap_info").path("topic_url");
120 dmaapTopics.put(getTopicName(subscriberUrl.asText()),
121 getTopicName(publisherUrl.asText()));
122 setDmaapConfig(subscriberUrl.asText());
123 } catch (IOException ex) {
124 errorLogger.error("IOException occured:", ex);
125 } catch (URISyntaxException e) {
126 errorLogger.error("Invalid URI :", e);
132 public String getTopicName(String url) throws URISyntaxException {
133 URI uri = new URI(url);
134 String path = uri.getPath();
135 String idStr = path.substring(path.lastIndexOf('/') + 1);
139 public void setDmaapConfig(String url) throws URISyntaxException {
140 URI uri = new URI(url);
141 dmaapConfig.setDmaaphost(uri.getHost());
142 dmaapConfig.setDEFAULT_PORT_NUMBER(uri.getPort());
145 public static String readFile(String configFileName) {
146 String content = null;
150 file = ResourceUtils.getFile("classpath:" + configFileName);
151 content = new String(Files.readAllBytes(file.toPath()));
152 } catch (FileNotFoundException e) {
153 errorLogger.error("colud not find file :{}", configFileName);
155 } catch (IOException e) {
156 errorLogger.error("unable to read the file , reason:", e);