2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright 2018-2019 TechMahindra
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.onap.universalvesadapter.utils;
\r
23 import java.io.File;
\r
24 import java.io.FileNotFoundException;
\r
25 import java.io.IOException;
\r
26 import java.net.URI;
\r
27 import java.net.URISyntaxException;
\r
28 import java.nio.file.Files;
\r
29 import java.util.HashMap;
\r
30 import java.util.Map;
\r
32 import org.json.simple.JSONArray;
\r
33 import org.json.simple.JSONObject;
\r
34 import org.json.simple.parser.JSONParser;
\r
35 import org.json.simple.parser.ParseException;
\r
36 import org.slf4j.Logger;
\r
37 import org.slf4j.LoggerFactory;
\r
38 import org.springframework.beans.factory.annotation.Autowired;
\r
39 import org.springframework.stereotype.Component;
\r
40 import org.springframework.util.ResourceUtils;
\r
42 import com.fasterxml.jackson.databind.JsonNode;
\r
43 import com.fasterxml.jackson.databind.ObjectMapper;
\r
46 public class CollectorConfigPropertyRetrival {
\r
49 private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");
\r
50 private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
\r
51 private static JSONArray array;
\r
53 private DmaapConfig dmaapConfig;
\r
55 public static JSONArray collectorConfigArray(String configFile) {
\r
57 JSONParser parser = new JSONParser();
\r
58 String content = readFile(configFile);
\r
59 JSONObject obj = (JSONObject) parser.parse(content);
\r
60 JSONObject appobj = (JSONObject) obj.get("app_preferences");
\r
61 array = (JSONArray) appobj.get("collectors");
\r
63 debugLogger.info("Retrieved JsonArray from Collector Config File");
\r
65 } catch (ParseException e) {
\r
66 errorLogger.error("ParseException occured at position:", e.getPosition());
\r
74 public static String[] getProperyArray(String properyName, String defaultConfigFilelocation) {
\r
75 JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);
\r
77 String[] propertyArray = new String[jsonArray.size()];
\r
79 for (int k = 0; k < jsonArray.size(); k++) {
\r
81 JSONObject collJson = (JSONObject) jsonArray.get(k);
\r
83 propertyArray[k] = (String) collJson.get(properyName);
\r
85 debugLogger.info("returning " + properyName + " array from Collector Config");
\r
86 return propertyArray;
\r
90 public Map<String, String> getDmaapTopics(String subscriber, String publisher,
\r
91 String defaultConfigFilelocation) {
\r
92 JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);
\r
94 Map<String, String> dmaapTopics = new HashMap<>();
\r
96 for (int k = 0; k < jsonArray.size(); k++) {
\r
98 JSONObject collJson = (JSONObject) jsonArray.get(k);
\r
100 dmaapTopics.put(collJson.get(subscriber).toString(),
\r
101 collJson.get(publisher).toString());
\r
104 debugLogger.info("returning Dmaap topics from Collector Config");
\r
105 return dmaapTopics;
\r
109 public Map<String, String> getTopics(String subscriber, String publisher,
\r
110 String defaultConfigFilelocation) {
\r
111 Map<String, String> dmaapTopics = new HashMap<>();
\r
115 ObjectMapper objectMapper = new ObjectMapper();
\r
116 String content = readFile(defaultConfigFilelocation);
\r
117 // read JSON like DOM Parser
\r
118 JsonNode rootNode = objectMapper.readTree(content);
\r
119 JsonNode subscriberUrl = rootNode.path("streams_subscribes").path(subscriber)
\r
120 .path("dmaap_info").path("topic_url");
\r
121 JsonNode publisherUrl = rootNode.path("streams_publishes").path(publisher)
\r
122 .path("dmaap_info").path("topic_url");
\r
124 dmaapTopics.put(getTopicName(subscriberUrl.asText()),
\r
125 getTopicName(publisherUrl.asText()));
\r
126 setDmaapConfig(subscriberUrl.asText());
\r
127 } catch (IOException ex) {
\r
128 errorLogger.error("IOException occured:" + ex.getMessage());
\r
130 } catch (URISyntaxException e) {
\r
132 errorLogger.error("Invalid URI :" + e.getInput() + ": " + e.getReason());
\r
135 return dmaapTopics;
\r
139 public String getTopicName(String url) throws URISyntaxException {
\r
140 URI uri = new URI(url);
\r
141 String path = uri.getPath();
\r
142 String idStr = path.substring(path.lastIndexOf('/') + 1);
\r
147 public void setDmaapConfig(String url) throws URISyntaxException {
\r
148 URI uri = new URI(url);
\r
149 dmaapConfig.setDmaaphost(uri.getHost());
\r
150 dmaapConfig.setDEFAULT_PORT_NUMBER(uri.getPort());
\r
154 public static String readFile(String configFileName) {
\r
155 String content = null;
\r
159 file = ResourceUtils.getFile("classpath:" + configFileName);
\r
160 content = new String(Files.readAllBytes(file.toPath()));
\r
161 } catch (FileNotFoundException e) {
\r
162 errorLogger.error("colud not find file :", configFileName);
\r
164 } catch (IOException e) {
\r
165 errorLogger.error("unable to read the file , reason:", e.getCause());
\r