afa5c7c8a44e449beb3b5839bbabdcf7c1771477
[dcaegen2/services/mapper.git] /
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,\r
75                         String defaultConfigFilelocation) {\r
76                 JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);\r
77                 \r
78                 String[] propertyArray = new String[jsonArray.size()];\r
79                 \r
80                 for (int k = 0; k < jsonArray.size(); k++) {\r
81                         \r
82                         JSONObject collJson = (JSONObject) jsonArray.get(k);\r
83                         \r
84                         propertyArray[k] = (String) collJson.get(properyName);\r
85                 }\r
86                 debugLogger.info("returning " + properyName + " array from Collector Config");\r
87                 return propertyArray;\r
88                 \r
89         }\r
90         \r
91         public Map<String, String> getDmaapTopics(String subscriber, String publisher,\r
92                         String defaultConfigFilelocation) {\r
93                 JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);\r
94                 \r
95                 Map<String, String> dmaapTopics = new HashMap<>();\r
96                 \r
97                 for (int k = 0; k < jsonArray.size(); k++) {\r
98                         \r
99                         JSONObject collJson = (JSONObject) jsonArray.get(k);\r
100                         \r
101                         dmaapTopics.put(collJson.get(subscriber).toString(),\r
102                                         collJson.get(publisher).toString());\r
103                         \r
104                 }\r
105                 debugLogger.info("returning Dmaap topics from Collector Config");\r
106                 return dmaapTopics;\r
107                 \r
108         }\r
109         \r
110         public Map<String, String> getTopics(String subscriber, String publisher,\r
111                         String defaultConfigFilelocation) {\r
112                 Map<String, String> dmaapTopics = new HashMap<>();\r
113                 \r
114                 try {\r
115                         \r
116                         ObjectMapper objectMapper = new ObjectMapper();\r
117                         String content = readFile(defaultConfigFilelocation);\r
118                         // read JSON like DOM Parser\r
119                         JsonNode rootNode = objectMapper.readTree(content);\r
120                         JsonNode subscriberUrl = rootNode.path("streams_subscribes")\r
121                                         .path(subscriber).path("dmaap_info").path("topic_url");\r
122                         JsonNode publisherUrl = rootNode.path("streams_publishes").path(publisher)\r
123                                         .path("dmaap_info").path("topic_url");\r
124                         \r
125                         dmaapTopics.put(getTopicName(subscriberUrl.asText()),\r
126                                         getTopicName(publisherUrl.asText()));\r
127                         setDmaapConfig(subscriberUrl.asText());\r
128                 } catch (IOException ex) {\r
129                         errorLogger.error("IOException occured:" + ex.getMessage());\r
130                         \r
131                 } catch (URISyntaxException e) {\r
132                         \r
133                         errorLogger.error("Invalid URI :" + e.getInput() + ": " + e.getReason());\r
134                 }\r
135                 \r
136                 return dmaapTopics;\r
137                 \r
138         }\r
139         \r
140         public String getTopicName(String url) throws URISyntaxException {\r
141                 URI uri = new URI(url);\r
142                 String path = uri.getPath();\r
143                 String idStr = path.substring(path.lastIndexOf('/') + 1);\r
144                 return idStr;\r
145                 \r
146         }\r
147         \r
148         public void setDmaapConfig(String url) throws URISyntaxException {\r
149                 URI uri = new URI(url);\r
150                 dmaapConfig.setDmaaphost(uri.getHost());\r
151                 dmaapConfig.setDEFAULT_PORT_NUMBER(uri.getPort());\r
152                 \r
153         }\r
154         \r
155         public static String readFile(String configFileName) {\r
156                 String content = null;\r
157                 File file = null;\r
158                 \r
159                 try {\r
160                         file = ResourceUtils.getFile("classpath:" + configFileName);\r
161                         content = new String(Files.readAllBytes(file.toPath()));\r
162                 } catch (FileNotFoundException e) {\r
163                         errorLogger.error("colud not find file :", configFileName);\r
164                         \r
165                 } catch (IOException e) {\r
166                         errorLogger.error("unable to read the file , reason:", e.getCause());\r
167                 }\r
168                 \r
169                 return content;\r
170                 \r
171         }\r
172 }\r