9de7e6311226979efb28b3a260ed4c89bdd67d5c
[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.FileNotFoundException;
23 import java.io.FileReader;
24 import java.io.IOException;
25
26 import org.json.simple.JSONArray;
27 import org.json.simple.JSONObject;
28 import org.json.simple.parser.JSONParser;
29 import org.json.simple.parser.ParseException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34 public class CollectorConfigPropertyRetrival {
35
36
37          public static String configFile = "/opt/app/VESAdapter/conf/kv.json";
38          //public static String configFile = "src\\main\\resources\\kv.json";
39          private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");
40          private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
41          private static  JSONArray array;
42
43         public static  JSONArray collectorConfigArray(String configFile){
44                 try {
45                 JSONParser parser = new JSONParser();
46                 FileReader fileReader = new FileReader(configFile);
47                 JSONObject obj = (JSONObject) parser.parse(fileReader);
48                 JSONObject appobj = (JSONObject) obj.get("app_preferences");
49                  array =(JSONArray) appobj.get("collectors");
50                 
51                  debugLogger.info("Retrieved JsonArray from Collector Config File");
52                 } catch (ParseException e) {
53                         errorLogger.error("ParseException occured at position:",e.getPosition());
54                 } catch (FileNotFoundException e) {
55                         
56                         errorLogger.error("Collector Config File is not found..",e.getMessage());
57                 } catch (IOException e) {
58                         
59                         errorLogger.error("Error occured due to :",e.getMessage());
60                 }
61                 
62                 
63                 return array;
64                 
65         }
66         
67         
68         public static String [] getProperyArray(String properyName) {
69                 JSONArray jsonArray =collectorConfigArray(configFile);
70                 
71                 String [] propertyArray=new String[jsonArray.size()];
72                 
73                  for (int k=0;k<jsonArray.size();k++) {
74                                 
75                                  JSONObject collJson= (JSONObject) jsonArray.get(k);
76                                  
77                                  propertyArray[k]=(String) collJson.get(properyName);
78                          }
79                  debugLogger.info("returning "+properyName+" array from Collector Config");
80                 return propertyArray;
81                 
82         }
83
84 }