2 * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.vfc.nfvo.emsdriver.configmgr;
19 import java.io.FileInputStream;
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
26 import java.util.Properties;
27 import java.util.concurrent.ConcurrentHashMap;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.jdom.Document;
32 import org.jdom.Element;
33 import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
34 import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;
35 import org.onap.vfc.nfvo.emsdriver.commons.model.CrontabVo;
36 import org.onap.vfc.nfvo.emsdriver.commons.model.EMSInfo;
37 import org.onap.vfc.nfvo.emsdriver.commons.utils.DriverThread;
38 import org.onap.vfc.nfvo.emsdriver.commons.utils.StringUtil;
39 import org.onap.vfc.nfvo.emsdriver.commons.utils.XmlUtil;
40 import org.onap.vfc.nfvo.emsdriver.northbound.client.HttpClientUtil;
42 import com.alibaba.fastjson.JSONArray;
43 import com.alibaba.fastjson.JSONObject;
46 public class ConfigurationManager extends DriverThread{
47 protected static Log log = LogFactory.getLog(ConfigurationManager.class);
51 private static Map<String, EMSInfo> emsInfoCache = new ConcurrentHashMap<String, EMSInfo>();
52 private static Map<String, CrontabVo> emsCrontab= new ConcurrentHashMap<String, CrontabVo>();
53 private static List<String> emsIdList = new ArrayList<String>();
54 private static Properties properties = null;
56 private final static String config = Constant.SYS_CFG + "config.properties";
59 public void dispose() {
61 //this.log.debug("start loading " + cacheFilePath);
62 File file = new File(config);
63 if(!file.exists() || !file.isFile()){
64 log.error("cacheFilePath " + config+" not exist or is not File");
67 InputStream in = null;
69 properties = new Properties();
70 in = new FileInputStream(file);
72 Map<String, CrontabVo> emsMap = readCorntab();
74 emsCrontab.putAll(emsMap);
77 new ReceiveSource().start();
79 log.error("read ["+file.getAbsolutePath()+"]Exception :",e);
84 } catch (Exception e) {
90 public Map<String, CrontabVo> readCorntab() {
91 String path = Constant.SYS_CFG + "crontab.xml";
92 File cfg = new File(path);
93 log.debug("start loading " + path);
94 if(!cfg.exists() || !cfg.isFile()){
95 log.debug("not exists " + path);
98 InputStream is = null;
99 Map<String, CrontabVo> tmpcache = new HashMap<String, CrontabVo>();
102 is = new FileInputStream(cfg);
103 Document doc = XmlUtil.getDocument(is);
105 Element root = doc.getRootElement();
107 @SuppressWarnings("unchecked")
108 List<Element> children = root.getChildren();
110 for(Iterator<Element> it = children.iterator();it.hasNext();){
111 CrontabVo crontabVo = new CrontabVo();
112 Element child = it.next();
113 String type = child.getAttributeValue("type");
114 if(StringUtil.isBank(type)){
117 crontabVo.setType(type);
118 if(Constant.COLLECT_TYPE_ALARM.equalsIgnoreCase(type)){
119 boolean iscollect = Boolean.parseBoolean(child.getAttributeValue("iscollect"));
121 crontabVo.setIscollect(iscollect);
126 crontabVo.setRead_timeout(child.getChildText("readtimeout"));
128 String crontab = child.getAttributeValue("crontab");
129 if(!StringUtil.isBank(type) && !StringUtil.isBank(crontab)){
130 crontabVo.setCrontab(crontab);
134 crontabVo.setMatch(child.getChildText("match"));
135 crontabVo.setGranularity(child.getChildText("granularity"));
137 tmpcache.put(type.toUpperCase(), crontabVo);
140 } catch (Exception e) {
141 log.error("load crontab.xml is error "+StringUtil.getStackTrace(e));
148 } catch (Exception e2) {
156 public void readcfg(){
157 String path = Constant.SYS_CFG + "EMSInfo.xml";
158 File cfg = new File(path);
159 log.debug("start loading " + path);
160 if(!cfg.exists() || !cfg.isFile()){
161 log.debug("not exists " + path);
166 InputStream is = null;
167 Map<String, EMSInfo> tmpcache = new HashMap<String, EMSInfo>();
170 is = new FileInputStream(cfg);
171 Document doc = XmlUtil.getDocument(is);
173 Element root = doc.getRootElement();
175 @SuppressWarnings("unchecked")
176 List<Element> children = root.getChildren();
178 for(Iterator<Element> it = children.iterator();it.hasNext();){
179 EMSInfo emsInfo = new EMSInfo();
180 Element child = it.next();
181 String name = child.getAttributeValue("name");
182 if(StringUtil.isBank(name)){
185 emsInfo.setName(name);
187 // tmpcache.put(name, emsInfo);
189 @SuppressWarnings("unchecked")
190 List<Element> collectList = child.getChildren();
191 for(Element collect : collectList){
193 CollectVo collectVo = new CollectVo();
195 String type = collect.getAttributeValue("type");
196 if("alarm".equalsIgnoreCase(type)){
197 boolean iscollect = Boolean.parseBoolean(collect.getAttributeValue("iscollect"));
199 collectVo.setIscollect(iscollect);
203 collectVo.setType(type);
204 collectVo.setIP(collect.getChildText("ip"));
205 collectVo.setPort(collect.getChildText("port"));
206 collectVo.setUser(collect.getChildText("user"));
207 collectVo.setPassword(collect.getChildText("password"));
208 collectVo.setRead_timeout(collect.getChildText("readtimeout"));
210 String crontab = collect.getAttributeValue("crontab");
211 if(!StringUtil.isBank(type) && !StringUtil.isBank(crontab)){
212 collectVo.setType(type);
213 collectVo.setCrontab(crontab);
217 collectVo.setIP(collect.getChildText("ip"));
218 collectVo.setPort(collect.getChildText("port"));
219 collectVo.setUser(collect.getChildText("user"));
220 collectVo.setPassword(collect.getChildText("password"));
221 collectVo.setRemotepath(collect.getChildText("remotepath"));
222 collectVo.setMatch(collect.getChildText("match"));
223 collectVo.setPassive(collect.getChildText("passive"));
224 collectVo.setFtptype(collect.getChildText("ftptype"));
225 collectVo.setGranularity(collect.getChildText("granularity"));
228 emsInfo.putCollectMap(type, collectVo);
230 tmpcache.put(name, emsInfo);
232 emsInfoCache.putAll(tmpcache);
234 File file = new File(config);
235 if(!file.exists() || !file.isFile()){
236 log.error("cacheFilePath " + config+" not exist or is not File");
239 InputStream in = null;
241 properties = new Properties();
242 in = new FileInputStream(file);
244 }catch (Exception e) {
247 } catch (Exception e) {
248 log.error("load EMSInfo.xml is error "+StringUtil.getStackTrace(e));
256 } catch (Exception e2) {
263 public static synchronized List<EMSInfo> getAllEMSInfos(){
264 List<EMSInfo> list = new ArrayList<EMSInfo>();
265 for(EMSInfo emsinfo :emsInfoCache.values()){
271 public static synchronized EMSInfo getEMSInfoByName(String emsName){
272 EMSInfo emsInfo= emsInfoCache.get(emsName);
276 public static synchronized Properties getProperties() {
280 class ReceiveSource extends Thread{
281 long timeStamp = System.currentTimeMillis();
287 if(System.currentTimeMillis() - timeStamp > Constant.ONEMINUTE){
288 timeStamp = System.currentTimeMillis();
289 log.debug("ReceiveSource run");
292 List<String> emsIds = this.getEmsIdList();
293 if(emsIds.size() > 0){
295 emsIdList.addAll(emsIds);
298 for(String emsId : emsIdList){
299 //get emsInfo by emsId
300 Map<String, EMSInfo> emsInfoMap = this.getEmsInfo(emsId);
302 emsInfoCache.putAll(emsInfoMap);
307 if(emsInfoCache.size() > 0){
308 Thread.sleep(30*60*1000);
310 Thread.sleep(60*1000);
312 } catch (Exception e) {
314 Thread.sleep(60*1000);
315 } catch (InterruptedException e1) {
316 e1.printStackTrace();
318 log.error("ReceiveSource exception",e);
323 private Map<String, EMSInfo> getEmsInfo(String emsId) {
324 Map<String, EMSInfo> tmpcache = new ConcurrentHashMap<String, EMSInfo>();
325 String msbAddress = properties.getProperty("msbAddress");
326 String emstUrl = properties.getProperty("esr_emsUrl");
328 emstUrl = String.format(emstUrl, emsId);
329 String getemstUrl = "http://"+msbAddress+emstUrl;
330 String emsResult = HttpClientUtil.doGet(getemstUrl, Constant.ENCODING_UTF8);
331 log.debug(getemstUrl+" result="+emsResult);
332 JSONObject reagobj = JSONObject.parseObject(emsResult);
334 JSONObject esr_system_info_list = reagobj.getJSONObject("esr-system-info-list");
335 JSONArray esr_system_info = esr_system_info_list.getJSONArray("esr-system-info");
336 Iterator<Object> it = esr_system_info.iterator();
337 EMSInfo emsInfo = new EMSInfo();
338 emsInfo.setName(emsId);
339 tmpcache.put(emsId, emsInfo);
341 Object obj = it.next();
342 JSONObject collect = (JSONObject)obj;
343 String system_type = (String)collect.get("system-type");
344 CollectVo collectVo = new CollectVo();
345 if(Constant.COLLECT_TYPE_CM.equalsIgnoreCase(system_type)){
346 CrontabVo crontabVo = emsCrontab.get(system_type);
347 if(crontabVo != null){
348 collectVo.setType(system_type);
349 collectVo.setCrontab(crontabVo.getCrontab());
350 collectVo.setIP(collect.getString("ip-address"));
351 collectVo.setPort(collect.getString("port"));
352 collectVo.setUser(collect.getString("user-name"));
353 collectVo.setPassword(collect.getString("password"));
355 collectVo.setRemotepath(collect.getString("remote-path"));
356 collectVo.setMatch(crontabVo.getMatch());
357 collectVo.setPassive(collect.getString("passive"));
358 collectVo.setGranularity(crontabVo.getGranularity());
360 log.error("emsCrontab.get(system_type) result crontabVo is null system_type=["+system_type+"] emsCrontabMap="+emsCrontab );
364 }else if(Constant.COLLECT_TYPE_PM.equalsIgnoreCase(system_type)){
365 CrontabVo crontabVo = emsCrontab.get(system_type);
366 if(crontabVo != null){
367 collectVo.setType(system_type);
368 collectVo.setCrontab(crontabVo.getCrontab());
369 collectVo.setIP(collect.getString("ip-address"));
370 collectVo.setPort(collect.getString("port"));
371 collectVo.setUser(collect.getString("user-name"));
372 collectVo.setPassword(collect.getString("password"));
374 collectVo.setRemotepath(collect.getString("remote-path"));
375 collectVo.setMatch(crontabVo.getMatch());
376 collectVo.setPassive(collect.getString("passive"));
377 collectVo.setGranularity(crontabVo.getGranularity());
379 log.error("emsCrontab.get(system_type) result crontabVo is null system_type=["+system_type+"]" );
381 }else if(Constant.COLLECT_TYPE_ALARM.equalsIgnoreCase(system_type)){
382 CrontabVo crontabVo = emsCrontab.get(system_type);
383 if(crontabVo != null){
384 collectVo.setIscollect(crontabVo.isIscollect());
385 collectVo.setType(system_type);
386 collectVo.setIP(collect.getString("ip-address"));
387 collectVo.setPort(collect.getString("port"));
388 collectVo.setUser(collect.getString("user-name"));
389 collectVo.setPassword(collect.getString("password"));
390 collectVo.setRead_timeout(crontabVo.getRead_timeout());
392 log.error("emsCrontab.get(system_type) result crontabVo is null system_type=["+system_type+"]");
397 log.error("ems system-type ="+system_type+" ");
400 emsInfo.putCollectMap(system_type, collectVo);
405 private List<String> getEmsIdList() {
406 List<String> emsIds = new ArrayList<String>();
408 String msbAddress = properties.getProperty("msbAddress");
409 String url = properties.getProperty("esr_ems_listUrl");
410 String getemsListUrl = "http://"+msbAddress+url;
412 String result = HttpClientUtil.doGet(getemsListUrl, Constant.ENCODING_UTF8);
413 log.debug(getemsListUrl+" result="+result);
414 JSONObject reagobj = JSONObject.parseObject(result);
415 JSONArray esr_emsIds = reagobj.getJSONArray("esr-ems");
416 Iterator<Object> it = esr_emsIds.iterator();
418 Object obj = it.next();
419 JSONObject emsId = (JSONObject)obj;
420 String emsIdStr = (String)emsId.get("ems-id");
421 emsIds.add(emsIdStr);