f775d923b4d831d1af3a42fa53fdcb6ee0af1b77
[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.FileNotFoundException;
38 import java.io.InputStream;
39 import java.util.*;
40 import java.util.concurrent.ConcurrentHashMap;
41
42
43 public class ConfigurationManager extends DriverThread {
44     public static final String CONFIG_PROPERTIES_LOCATION = Constant.SYS_CFG + "config.properties";
45     protected static final Log logger = LogFactory.getLog(ConfigurationManager.class);
46     /**
47      * ESM Cache
48      */
49     @VisibleForTesting
50     static Map<String, EMSInfo> emsInfoCache = new ConcurrentHashMap<>();
51     @VisibleForTesting
52     static Properties properties = null;
53     private static Map<String, CrontabVo> emsCrontab = new ConcurrentHashMap<>();
54     private static List<String> emsIdList = new ArrayList<>();
55
56     static{
57          File file = new File(CONFIG_PROPERTIES_LOCATION);
58          if (!file.exists() || !file.isFile()) {
59                 logger.error("cacheFilePath " + CONFIG_PROPERTIES_LOCATION + " not exist or is not File");
60          }else{
61                         try {
62                                 InputStream in = new FileInputStream(file);
63                          properties = new Properties();
64                      properties.load(in);
65                         } catch (Exception e) {
66                                 // TODO Auto-generated catch block
67                                 logger.error("read [" + file.getAbsolutePath() + "]Exception :", e);
68                         }
69
70          }
71     }
72     
73     public static synchronized List<EMSInfo> getAllEMSInfos() {
74         List<EMSInfo> list = new ArrayList<>();
75         for (EMSInfo emsinfo : emsInfoCache.values()) {
76             list.add(emsinfo);
77         }
78         return list;
79     }
80
81     public static synchronized EMSInfo getEMSInfoByName(String emsName) {
82         return emsInfoCache.get(emsName);
83     }
84
85     public static synchronized Properties getProperties() {
86         return properties;
87     }
88
89     @Override
90     public void dispose() {
91         Map<String, CrontabVo> emsMap = readCorntab();
92         emsCrontab.putAll(emsMap);
93         new ReceiveSource().start();
94         
95     }
96
97     public Map<String, CrontabVo> readCorntab() {
98         String path = Constant.SYS_CFG + "crontab.xml";
99         File cfg = new File(path);
100         logger.debug("start loading " + path);
101         if (!cfg.exists() || !cfg.isFile()) {
102                 logger.debug("not exists " + path);
103             return null;
104         }
105         Map<String, CrontabVo> tmpcache = new HashMap<>();
106
107         try (
108             InputStream is = new FileInputStream(cfg)){
109             Document doc = XmlUtil.getDocument(is);
110
111             Element root = doc.getRootElement();
112
113             @SuppressWarnings("unchecked")
114             List<Element> children = root.getChildren();
115
116             for (Iterator<Element> it = children.iterator(); it.hasNext(); ) {
117                 CrontabVo crontabVo = new CrontabVo();
118                 Element child = it.next();
119                 String type = child.getAttributeValue("type");
120                 if (StringUtil.isBank(type)) {
121                     continue;
122                 }
123                 crontabVo.setType(type);
124                 if (Constant.COLLECT_TYPE_ALARM.equalsIgnoreCase(type)) {
125                     boolean iscollect = Boolean.parseBoolean(child.getAttributeValue("iscollect"));
126                     if (iscollect) {
127                         crontabVo.setIscollect(iscollect);
128                     } else {
129                         continue;
130                     }
131
132                     crontabVo.setReadTimeout(child.getChildText("readtimeout"));
133                 } else {
134                     String crontab = child.getAttributeValue("crontab");
135                     if (!StringUtil.isBank(type) && !StringUtil.isBank(crontab)) {
136                         crontabVo.setCrontab(crontab);
137                     } else {
138                         continue;
139                     }
140                     crontabVo.setMatch(child.getChildText("match"));
141                     crontabVo.setGranularity(child.getChildText("granularity"));
142                 }
143                 tmpcache.put(type.toUpperCase(), crontabVo);
144             }
145
146         } catch (Exception e) {
147                 logger.error("load crontab.xml is error " + StringUtil.getStackTrace(e));
148         } 
149         return tmpcache;
150     }
151
152     class ReceiveSource extends Thread {
153         long timeStamp = System.currentTimeMillis();
154         
155         @Override
156         public void run() {
157             while (true) {
158
159                 try {
160                     if (System.currentTimeMillis() - timeStamp > Constant.ONEMINUTE) {
161                         timeStamp = System.currentTimeMillis();
162                         logger.debug("ReceiveSource run");
163                     }
164                     //get emsId list
165                     List<String> emsIds = this.getEmsIdList();
166                     if (!emsIds.isEmpty()) {
167                         emsIdList.clear();
168                         emsIdList.addAll(emsIds);
169                     }
170
171                     for (String emsId : emsIdList) {
172                         //get emsInfo by emsId
173                         Map<String, EMSInfo> emsInfoMap = this.getEmsInfo(emsId);
174
175                         emsInfoCache.putAll(emsInfoMap);
176                     }
177                     if (emsInfoCache.size() > 0) {
178                         Thread.sleep(30 * 60 * 1000L);
179                     } else {
180                         Thread.sleep(60 * 1000L);
181                     }
182                 } catch (Exception e) {
183                     try {
184                         Thread.sleep(60 * 1000L);
185                     } catch (Exception e1) {
186                         logger.error("InterruptedException :" + StringUtil.getStackTrace(e1));
187                     }
188                     logger.error("ReceiveSource exception", e);
189                 }
190             }
191         }
192
193         private Map<String, EMSInfo> getEmsInfo(String emsId) {
194             Map<String, EMSInfo> tmpcache = new ConcurrentHashMap<>();
195             String msbAddress = properties.getProperty("msbAddress");
196             String emstUrl = properties.getProperty("esr_emsUrl");
197             //set emsId to url
198             emstUrl = String.format(emstUrl, emsId);
199             String getemstUrl = "http://" + msbAddress + emstUrl;
200             String emsResult = HttpClientUtil.doGet(getemstUrl, Constant.ENCODING_UTF8);
201             logger.debug(getemstUrl + " result=" + emsResult);
202             JSONObject reagobj = JSONObject.parseObject(emsResult);
203
204             JSONObject esrSystemInfoList = reagobj.getJSONObject("esr-system-info-list");
205             JSONArray esrSystemInfo = esrSystemInfoList.getJSONArray("esr-system-info");
206             Iterator<Object> it = esrSystemInfo.iterator();
207             EMSInfo emsInfo = new EMSInfo();
208             emsInfo.setName(emsId);
209             tmpcache.put(emsId, emsInfo);
210             String ipAddressConstant = "ip-address";
211             String userNameConstant = "user-name";
212             String passwordConstant = "password";
213             String portConstant = "port";
214             while (it.hasNext()) {
215                 Object obj = it.next();
216                 JSONObject collect = (JSONObject) obj;
217                 String systemType = (String) collect.get("system-type");
218                 CollectVo collectVo = new CollectVo();
219                 if (Constant.COLLECT_TYPE_CM.equalsIgnoreCase(systemType)) {
220                     CrontabVo crontabVo = emsCrontab.get(systemType);
221                     if (crontabVo != null) {
222                         collectVo.setType(systemType);
223                         collectVo.setCrontab(crontabVo.getCrontab());
224                         collectVo.setIP(collect.getString(ipAddressConstant));
225                         collectVo.setPort(collect.getString(portConstant));
226                         collectVo.setUser(collect.getString(userNameConstant));
227                         collectVo.setPassword(collect.getString(passwordConstant));
228
229                         collectVo.setRemotepath(collect.getString("remote-path"));
230                         collectVo.setMatch(crontabVo.getMatch());
231                         collectVo.setPassive(collect.getString("passive"));
232                         collectVo.setGranularity(crontabVo.getGranularity());
233                     } else {
234                         logger.error("emsCrontab.get(systemType) result crontabVo is null systemType=[" + systemType + "] emsCrontabMap=" + emsCrontab);
235                     }
236
237
238                 } else if (Constant.COLLECT_TYPE_PM.equalsIgnoreCase(systemType)) {
239                     CrontabVo crontabVo = emsCrontab.get(systemType);
240                     if (crontabVo != null) {
241                         collectVo.setType(systemType);
242                         collectVo.setCrontab(crontabVo.getCrontab());
243                         collectVo.setIP(collect.getString(ipAddressConstant));
244                         collectVo.setPort(collect.getString(portConstant));
245                         collectVo.setUser(collect.getString(userNameConstant));
246                         collectVo.setPassword(collect.getString(passwordConstant));
247
248                         collectVo.setRemotepath(collect.getString("remote-path"));
249                         collectVo.setMatch(crontabVo.getMatch());
250                         collectVo.setPassive(collect.getString("passive"));
251                         collectVo.setGranularity(crontabVo.getGranularity());
252                     } else {
253                         logger.error("emsCrontab.get(systemType) result crontabVo is null systemType=[" + systemType + "]");
254                     }
255                 } else if (Constant.COLLECT_TYPE_ALARM.equalsIgnoreCase(systemType)) {
256                     CrontabVo crontabVo = emsCrontab.get(systemType);
257                     if (crontabVo != null) {
258                         collectVo.setIscollect(crontabVo.isIscollect());
259                         collectVo.setType(systemType);
260                         collectVo.setIP(collect.getString(ipAddressConstant));
261                         collectVo.setPort(collect.getString(portConstant));
262                         collectVo.setUser(collect.getString(userNameConstant));
263                         collectVo.setPassword(collect.getString(passwordConstant));
264                         collectVo.setReadTimeout(crontabVo.getReadTimeout());
265                     } else {
266                         logger.error("emsCrontab.get(systemType) result crontabVo is null systemType=[" + systemType + "]");
267                     }
268
269
270                 } else {
271                         logger.error("ems system-type =" + systemType + " ");
272                 }
273
274                 emsInfo.putCollectMap(systemType, collectVo);
275             }
276             return tmpcache;
277         }
278
279         private List<String> getEmsIdList() {
280             List<String> emsIds = new ArrayList<>();
281             //http
282             String msbAddress = properties.getProperty("msbAddress");
283             String url = properties.getProperty("esr_ems_listUrl");
284             String getemsListUrl = "http://" + msbAddress + url;
285
286             String result = HttpClientUtil.doGet(getemsListUrl, Constant.ENCODING_UTF8);
287             logger.debug(getemsListUrl + " result=" + result);
288             JSONObject reagobj = JSONObject.parseObject(result);
289             JSONArray esrEmsIds = reagobj.getJSONArray("esr-ems");
290             Iterator<Object> it = esrEmsIds.iterator();
291             while (it.hasNext()) {
292                 Object obj = it.next();
293                 JSONObject emsId = (JSONObject) obj;
294                 String emsIdStr = (String) emsId.get("ems-id");
295                 emsIds.add(emsIdStr);
296             }
297             return emsIds;
298         }
299     }
300 }