Switched Ubuntu 16.04 to recommended base image alpine:3.8.
[dcaegen2/services/mapper.git] / UniversalVesAdapter / src / main / java / org / onap / universalvesadapter / utils / CollectorConfigPropertyRetrieval.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : DCAE
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
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.universalvesadapter.utils;
22
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.nio.file.Files;
29 import java.util.HashMap;
30 import java.util.Map;
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;
42
43 @Component
44 public class CollectorConfigPropertyRetrieval {
45
46     private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");
47     private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
48     private static JSONArray array;
49     @Autowired
50     private DmaapConfig dmaapConfig;
51
52     public static JSONArray collectorConfigArray(String configFile) {
53         try {
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");
59
60             debugLogger.info("Retrieved JsonArray from Collector Config File");
61
62         } catch (ParseException e) {
63             errorLogger.error("ParseException occured at position:", e);
64         }
65
66         return array;
67
68     }
69
70     public static String[] getProperyArray(String propertyName, String defaultConfigFilelocation) {
71         JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);
72
73         String[] propertyArray = new String[jsonArray.size()];
74
75         for (int k = 0; k < jsonArray.size(); k++) {
76
77             JSONObject collJson = (JSONObject) jsonArray.get(k);
78
79             propertyArray[k] = (String) collJson.get(propertyName);
80         }
81         debugLogger.info("returning {} array from Collector Config", propertyName);
82         return propertyArray;
83
84     }
85
86     public Map<String, String> getDmaapTopics(String subscriber, String publisher,
87             String defaultConfigFilelocation) {
88         JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);
89
90         Map<String, String> dmaapTopics = new HashMap<>();
91
92         for (int k = 0; k < jsonArray.size(); k++) {
93
94             JSONObject collJson = (JSONObject) jsonArray.get(k);
95
96             dmaapTopics.put(collJson.get(subscriber).toString(),
97                     collJson.get(publisher).toString());
98
99         }
100         debugLogger.info("returning Dmaap topics from Collector Config");
101         return dmaapTopics;
102
103     }
104
105     public Map<String, String> getTopics(String subscriber, String publisher,
106             String defaultConfigFilelocation) {
107         Map<String, String> dmaapTopics = new HashMap<>();
108
109         try {
110
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");
119
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);
127         }
128
129         return dmaapTopics;
130     }
131
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);
136         return idStr;
137     }
138
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());
143     }
144
145     public static String readFile(String configFileName) {
146         String content = null;
147         File file = null;
148
149         try {
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);
154
155         } catch (IOException e) {
156             errorLogger.error("unable to read the file , reason:", e);
157         }
158
159         return content;
160     }
161 }