2262b9b0cc396d23d9d9b946defe0e05d5290fea
[dcaegen2/services/mapper.git] /
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : DCAE
4 * ================================================================================
5 * Copyright 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 package org.onap.universalvesadapter.utils;
21
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
26
27 import org.json.simple.JSONArray;
28 import org.json.simple.JSONObject;
29 import org.json.simple.parser.JSONParser;
30 import org.json.simple.parser.ParseException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.stereotype.Component;
35
36 @Component
37 public class CollectorConfigPropertyRetrival {
38
39         @Value("${defaultConfigFilelocation}")
40         public String defaultConfigFilelocation;
41         private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");
42         private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
43         private static JSONArray array;
44
45         public static JSONArray collectorConfigArray(String configFile) {
46                 try {
47                         JSONParser parser = new JSONParser();
48
49                         File file = new File(ClassLoader.getSystemResource(configFile.trim()).getFile());
50
51                         FileReader fileReader = new FileReader(file);
52                         JSONObject obj = (JSONObject) parser.parse(fileReader);
53                         JSONObject appobj = (JSONObject) obj.get("app_preferences");
54                         array = (JSONArray) appobj.get("collectors");
55
56                         debugLogger.info("Retrieved JsonArray from Collector Config File");
57                         
58                 } catch (ParseException e) {
59                         errorLogger.error("ParseException occured at position:", e.getPosition());
60                 } catch (FileNotFoundException e) {
61
62                         errorLogger.error("Collector Config File is not found..", e.getMessage());
63                 } catch (IOException e) {
64
65                         errorLogger.error("Error occured due to :", e.getMessage());
66                 }
67
68                 return array;
69
70         }
71
72         public static String[] getProperyArray(String properyName, String defaultConfigFilelocation) {
73                 JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);
74
75                 String[] propertyArray = new String[jsonArray.size()];
76
77                 for (int k = 0; k < jsonArray.size(); k++) {
78
79                         JSONObject collJson = (JSONObject) jsonArray.get(k);
80
81                         propertyArray[k] = (String) collJson.get(properyName);
82                 }
83                 debugLogger.info("returning " + properyName + " array from Collector Config");
84                 return propertyArray;
85
86         }
87
88 }