Code Smells in emsdriver conf
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / configmgr / ConfigurationManager.java
1 /**
2  * Copyright 2017 BOCO Corporation 
3  * Copyright 2018 CMCC Technologies Co. Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.onap.vfc.nfvo.emsdriver.configmgr;
18
19 import com.alibaba.fastjson.JSONArray;
20 import com.alibaba.fastjson.JSONObject;
21 import com.google.common.annotations.VisibleForTesting;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.jdom.Document;
25 import org.jdom.Element;
26 import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
27 import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;
28 import org.onap.vfc.nfvo.emsdriver.commons.model.CrontabVo;
29 import org.onap.vfc.nfvo.emsdriver.commons.model.EMSInfo;
30 import org.onap.vfc.nfvo.emsdriver.commons.utils.DriverThread;
31 import org.onap.vfc.nfvo.emsdriver.commons.utils.StringUtil;
32 import org.onap.vfc.nfvo.emsdriver.commons.utils.XmlUtil;
33 import org.onap.vfc.nfvo.emsdriver.northbound.client.HttpClientUtil;
34
35 import java.io.File;
36 import java.io.FileInputStream;
37 import java.io.InputStream;
38 import java.util.*;
39 import java.util.concurrent.ConcurrentHashMap;
40
41
42 public class ConfigurationManager extends DriverThread {
43     public static final String CONFIG_PROPERTIES_LOCATION = Constant.SYS_CFG + "config.properties";
44     protected static final Log log = LogFactory.getLog(ConfigurationManager.class);
45     /**
46      * ESM Cache
47      */
48     @VisibleForTesting
49     static Map<String, EMSInfo> emsInfoCache = new ConcurrentHashMap<>();
50     @VisibleForTesting
51     static Properties properties = null;
52     private static Map<String, CrontabVo> emsCrontab = new ConcurrentHashMap<>();
53     private static List<String> emsIdList = new ArrayList<>();
54
55     public static synchronized List<EMSInfo> getAllEMSInfos() {
56         List<EMSInfo> list = new ArrayList<>();
57         for (EMSInfo emsinfo : emsInfoCache.values()) {
58             list.add(emsinfo);
59         }
60         return list;
61     }
62
63     public static synchronized EMSInfo getEMSInfoByName(String emsName) {
64         return emsInfoCache.get(emsName);
65     }
66
67     public static synchronized Properties getProperties() {
68         return properties;
69     }
70
71     @Override
72     public void dispose() {
73
74         File file = new File(CONFIG_PROPERTIES_LOCATION);
75         if (!file.exists() || !file.isFile()) {
76             log.error("cacheFilePath " + CONFIG_PROPERTIES_LOCATION + " not exist or is not File");
77             return;
78         }
79         try(InputStream in = new FileInputStream(file)){
80             properties = new Properties();
81             properties.load(in);
82             Map<String, CrontabVo> emsMap = readCorntab();
83
84             emsCrontab.putAll(emsMap);
85
86             new ReceiveSource().start();
87         } catch (Exception e) {
88             log.error("read [" + file.getAbsolutePath() + "]Exception :", e);
89         } 
90     }
91
92     public Map<String, CrontabVo> readCorntab() {
93         String path = Constant.SYS_CFG + "crontab.xml";
94         File cfg = new File(path);
95         log.debug("start loading " + path);
96         if (!cfg.exists() || !cfg.isFile()) {
97             log.debug("not exists " + path);
98             return null;
99         }
100         Map<String, CrontabVo> tmpcache = new HashMap<>();
101
102         try (
103             InputStream is = new FileInputStream(cfg)){
104             Document doc = XmlUtil.getDocument(is);
105
106             Element root = doc.getRootElement();
107
108             @SuppressWarnings("unchecked")
109             List<Element> children = root.getChildren();
110
111             for (Iterator<Element> it = children.iterator(); it.hasNext(); ) {
112                 CrontabVo crontabVo = new CrontabVo();
113                 Element child = it.next();
114                 String type = child.getAttributeValue("type");
115                 if (StringUtil.isBank(type)) {
116                     continue;
117                 }
118                 crontabVo.setType(type);
119                 if (Constant.COLLECT_TYPE_ALARM.equalsIgnoreCase(type)) {
120                     boolean iscollect = Boolean.parseBoolean(child.getAttributeValue("iscollect"));
121                     if (iscollect) {
122                         crontabVo.setIscollect(iscollect);
123                     } else {
124                         continue;
125                     }
126
127                     crontabVo.setReadTimeout(child.getChildText("readtimeout"));
128                 } else {
129                     String crontab = child.getAttributeValue("crontab");
130                     if (!StringUtil.isBank(type) && !StringUtil.isBank(crontab)) {
131                         crontabVo.setCrontab(crontab);
132                     } else {
133                         continue;
134                     }
135                     crontabVo.setMatch(child.getChildText("match"));
136                     crontabVo.setGranularity(child.getChildText("granularity"));
137                 }
138                 tmpcache.put(type.toUpperCase(), crontabVo);
139             }
140
141         } catch (Exception e) {
142             log.error("load crontab.xml is error " + StringUtil.getStackTrace(e));
143         } 
144         return tmpcache;
145     }
146
147     class ReceiveSource extends Thread {
148         long timeStamp = System.currentTimeMillis();
149         
150         @Override
151         public void run() {
152             while (true) {
153
154                 try {
155                     if (System.currentTimeMillis() - timeStamp > Constant.ONEMINUTE) {
156                         timeStamp = System.currentTimeMillis();
157                         log.debug("ReceiveSource run");
158                     }
159                     //get emsId list
160                     List<String> emsIds = this.getEmsIdList();
161                     if (emsIds.size() > 0) {
162                         emsIdList.clear();
163                         emsIdList.addAll(emsIds);
164                     }
165
166                     for (String emsId : emsIdList) {
167                         //get emsInfo by emsId
168                         Map<String, EMSInfo> emsInfoMap = this.getEmsInfo(emsId);
169
170                         emsInfoCache.putAll(emsInfoMap);
171                     }
172                     if (emsInfoCache.size() > 0) {
173                         Thread.sleep(30 * 60 * 1000L);
174                     } else {
175                         Thread.sleep(60 * 1000L);
176                     }
177                 } catch (Exception e) {
178                     try {
179                         Thread.sleep(60 * 1000L);
180                     } catch (Exception e1) {
181                         log.error("InterruptedException :" + StringUtil.getStackTrace(e1));
182                     }
183                     log.error("ReceiveSource exception", e);
184                 }
185             }
186         }
187
188         private Map<String, EMSInfo> getEmsInfo(String emsId) {
189             Map<String, EMSInfo> tmpcache = new ConcurrentHashMap<>();
190             String msbAddress = properties.getProperty("msbAddress");
191             String emstUrl = properties.getProperty("esr_emsUrl");
192             //set emsId to url
193             emstUrl = String.format(emstUrl, emsId);
194             String getemstUrl = "http://" + msbAddress + emstUrl;
195             String emsResult = HttpClientUtil.doGet(getemstUrl, Constant.ENCODING_UTF8);
196             log.debug(getemstUrl + " result=" + emsResult);
197             JSONObject reagobj = JSONObject.parseObject(emsResult);
198
199             JSONObject esrSystemInfoList = reagobj.getJSONObject("esr-system-info-list");
200             JSONArray esrSystemInfo = esrSystemInfoList.getJSONArray("esr-system-info");
201             Iterator<Object> it = esrSystemInfo.iterator();
202             EMSInfo emsInfo = new EMSInfo();
203             emsInfo.setName(emsId);
204             tmpcache.put(emsId, emsInfo);
205             while (it.hasNext()) {
206                 Object obj = it.next();
207                 JSONObject collect = (JSONObject) obj;
208                 String systemType = (String) collect.get("system-type");
209                 CollectVo collectVo = new CollectVo();
210                 if (Constant.COLLECT_TYPE_CM.equalsIgnoreCase(systemType)) {
211                     CrontabVo crontabVo = emsCrontab.get(systemType);
212                     if (crontabVo != null) {
213                         collectVo.setType(systemType);
214                         collectVo.setCrontab(crontabVo.getCrontab());
215                         collectVo.setIP(collect.getString("ip-address"));
216                         collectVo.setPort(collect.getString("port"));
217                         collectVo.setUser(collect.getString("user-name"));
218                         collectVo.setPassword(collect.getString("password"));
219
220                         collectVo.setRemotepath(collect.getString("remote-path"));
221                         collectVo.setMatch(crontabVo.getMatch());
222                         collectVo.setPassive(collect.getString("passive"));
223                         collectVo.setGranularity(crontabVo.getGranularity());
224                     } else {
225                         log.error("emsCrontab.get(systemType) result crontabVo is null systemType=[" + systemType + "] emsCrontabMap=" + emsCrontab);
226                     }
227
228
229                 } else if (Constant.COLLECT_TYPE_PM.equalsIgnoreCase(systemType)) {
230                     CrontabVo crontabVo = emsCrontab.get(systemType);
231                     if (crontabVo != null) {
232                         collectVo.setType(systemType);
233                         collectVo.setCrontab(crontabVo.getCrontab());
234                         collectVo.setIP(collect.getString("ip-address"));
235                         collectVo.setPort(collect.getString("port"));
236                         collectVo.setUser(collect.getString("user-name"));
237                         collectVo.setPassword(collect.getString("password"));
238
239                         collectVo.setRemotepath(collect.getString("remote-path"));
240                         collectVo.setMatch(crontabVo.getMatch());
241                         collectVo.setPassive(collect.getString("passive"));
242                         collectVo.setGranularity(crontabVo.getGranularity());
243                     } else {
244                         log.error("emsCrontab.get(systemType) result crontabVo is null systemType=[" + systemType + "]");
245                     }
246                 } else if (Constant.COLLECT_TYPE_ALARM.equalsIgnoreCase(systemType)) {
247                     CrontabVo crontabVo = emsCrontab.get(systemType);
248                     if (crontabVo != null) {
249                         collectVo.setIscollect(crontabVo.isIscollect());
250                         collectVo.setType(systemType);
251                         collectVo.setIP(collect.getString("ip-address"));
252                         collectVo.setPort(collect.getString("port"));
253                         collectVo.setUser(collect.getString("user-name"));
254                         collectVo.setPassword(collect.getString("password"));
255                         collectVo.setReadTimeout(crontabVo.getReadTimeout());
256                     } else {
257                         log.error("emsCrontab.get(systemType) result crontabVo is null systemType=[" + systemType + "]");
258                     }
259
260
261                 } else {
262                     log.error("ems system-type =" + systemType + " ");
263                 }
264
265                 emsInfo.putCollectMap(systemType, collectVo);
266             }
267             return tmpcache;
268         }
269
270         private List<String> getEmsIdList() {
271             List<String> emsIds = new ArrayList<>();
272             //http
273             String msbAddress = properties.getProperty("msbAddress");
274             String url = properties.getProperty("esr_ems_listUrl");
275             String getemsListUrl = "http://" + msbAddress + url;
276
277             String result = HttpClientUtil.doGet(getemsListUrl, Constant.ENCODING_UTF8);
278             log.debug(getemsListUrl + " result=" + result);
279             JSONObject reagobj = JSONObject.parseObject(result);
280             JSONArray esrEmsIds = reagobj.getJSONArray("esr-ems");
281             Iterator<Object> it = esrEmsIds.iterator();
282             while (it.hasNext()) {
283                 Object obj = it.next();
284                 JSONObject emsId = (JSONObject) obj;
285                 String emsIdStr = (String) emsId.get("ems-id");
286                 emsIds.add(emsIdStr);
287             }
288             return emsIds;
289         }
290     }
291 }