Switched Ubuntu 16.04 to recommended base image alpine:3.8.
[dcaegen2/services/mapper.git] / UniversalVesAdapter / src / main / java / org / onap / universalvesadapter / utils / CollectorConfigPropertyRetrival.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP : DCAE\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
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\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
19  */\r
20 \r
21 package org.onap.universalvesadapter.utils;\r
22 \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
31 \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
41 \r
42 import com.fasterxml.jackson.databind.JsonNode;\r
43 import com.fasterxml.jackson.databind.ObjectMapper;\r
44 \r
45 @Component\r
46 public class CollectorConfigPropertyRetrival {\r
47     \r
48     \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
52     @Autowired\r
53     private DmaapConfig dmaapConfig;\r
54     \r
55     public static JSONArray collectorConfigArray(String configFile) {\r
56         try {\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
62             \r
63             debugLogger.info("Retrieved JsonArray from Collector Config File");\r
64             \r
65         } catch (ParseException e) {\r
66             errorLogger.error("ParseException occured at position:", e.getPosition());\r
67         }\r
68         \r
69         \r
70         return array;\r
71         \r
72     }\r
73     \r
74     public static String[] getProperyArray(String properyName, String defaultConfigFilelocation) {\r
75         JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);\r
76         \r
77         String[] propertyArray = new String[jsonArray.size()];\r
78         \r
79         for (int k = 0; k < jsonArray.size(); k++) {\r
80             \r
81             JSONObject collJson = (JSONObject) jsonArray.get(k);\r
82             \r
83             propertyArray[k] = (String) collJson.get(properyName);\r
84         }\r
85         debugLogger.info("returning " + properyName + " array from Collector Config");\r
86         return propertyArray;\r
87         \r
88     }\r
89     \r
90     public Map<String, String> getDmaapTopics(String subscriber, String publisher,\r
91             String defaultConfigFilelocation) {\r
92         JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);\r
93         \r
94         Map<String, String> dmaapTopics = new HashMap<>();\r
95         \r
96         for (int k = 0; k < jsonArray.size(); k++) {\r
97             \r
98             JSONObject collJson = (JSONObject) jsonArray.get(k);\r
99             \r
100             dmaapTopics.put(collJson.get(subscriber).toString(),\r
101                     collJson.get(publisher).toString());\r
102             \r
103         }\r
104         debugLogger.info("returning Dmaap topics from Collector Config");\r
105         return dmaapTopics;\r
106         \r
107     }\r
108     \r
109     public Map<String, String> getTopics(String subscriber, String publisher,\r
110             String defaultConfigFilelocation) {\r
111         Map<String, String> dmaapTopics = new HashMap<>();\r
112         \r
113         try {\r
114             \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
123             \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
129             \r
130         } catch (URISyntaxException e) {\r
131             \r
132             errorLogger.error("Invalid URI :" + e.getInput() + ": " + e.getReason());\r
133         }\r
134         \r
135         return dmaapTopics;\r
136         \r
137     }\r
138     \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
143         return idStr;\r
144         \r
145     }\r
146     \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
151         \r
152     }\r
153     \r
154     public static String readFile(String configFileName) {\r
155         String content = null;\r
156         File file = null;\r
157         \r
158         try {\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
163             \r
164         } catch (IOException e) {\r
165             errorLogger.error("unable to read the file , reason:", e.getCause());\r
166         }\r
167         \r
168         return content;\r
169         \r
170     }\r
171 }\r