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.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.jdom.Document;
30 import org.jdom.Element;
31 import org.onap.nfvo.emsdriver.commons.constant.Constant;
32 import org.onap.nfvo.emsdriver.commons.model.CollectVo;
33 import org.onap.nfvo.emsdriver.commons.model.EMSInfo;
34 import org.onap.nfvo.emsdriver.commons.utils.DriverThread;
35 import org.onap.nfvo.emsdriver.commons.utils.StringUtil;
36 import org.onap.nfvo.emsdriver.commons.utils.XmlUtil;
39 public class ConfigurationManager extends DriverThread{
44 private static Map<String, EMSInfo> emsInfoCache = new ConcurrentHashMap<String, EMSInfo>();
46 private static Properties properties = null;
48 private final static String ftpconfig = Constant.SYS_CFG + "ftpconfig.properties";
51 @SuppressWarnings("unchecked")
52 public void dispose() {
53 String path = Constant.SYS_CFG + "EMSInfo.xml";
54 File cfg = new File(path);
55 this.log.debug("start loading " + path);
56 if(!cfg.exists() || !cfg.isFile()){
57 this.log.debug("not exists " + path);
61 InputStream is = null;
62 Map<String, EMSInfo> tmpcache = new HashMap<String, EMSInfo>();
65 is = new FileInputStream(cfg);
66 Document doc = XmlUtil.getDocument(is);
68 Element root = doc.getRootElement();
70 List<Element> children = root.getChildren();
72 for(Iterator<Element> it = children.iterator();it.hasNext();){
73 EMSInfo emsInfo = new EMSInfo();
74 Element child = it.next();
75 String name = child.getAttributeValue("name");
76 if(StringUtil.isBank(name)){
79 emsInfo.setName(name);
81 tmpcache.put(name, emsInfo);
83 List<Element> collectList = child.getChildren();
84 for(Element collect : collectList){
86 CollectVo collectVo = new CollectVo();
88 String type = collect.getAttributeValue("type");
89 if("alarm".equalsIgnoreCase(type)){
90 boolean iscollect = Boolean.parseBoolean(collect.getAttributeValue("iscollect"));
92 collectVo.setIscollect(iscollect);
96 collectVo.setType(type);
97 collectVo.setIP(collect.getChildText("ip"));
98 collectVo.setPort(collect.getChildText("port"));
99 collectVo.setUser(collect.getChildText("user"));
100 collectVo.setPassword(collect.getChildText("password"));
101 collectVo.setRead_timeout(collect.getChildText("readtimeout"));
103 String crontab = collect.getAttributeValue("crontab");
104 if(!StringUtil.isBank(type) && !StringUtil.isBank(crontab)){
105 collectVo.setType(type);
106 collectVo.setCrontab(crontab);
110 collectVo.setIP(collect.getChildText("ip"));
111 collectVo.setPort(collect.getChildText("port"));
112 collectVo.setUser(collect.getChildText("user"));
113 collectVo.setPassword(collect.getChildText("password"));
114 collectVo.setRemotepath(collect.getChildText("remotepath"));
115 collectVo.setMatch(collect.getChildText("match"));
116 collectVo.setPassive(collect.getChildText("passive"));
117 collectVo.setFtptype(collect.getChildText("ftptype"));
118 collectVo.setGranularity(collect.getChildText("granularity"));
121 emsInfo.putCollectMap(type, collectVo);
123 tmpcache.put(name, emsInfo);
125 emsInfoCache.putAll(tmpcache);
127 } catch (Exception e) {
128 log.error("load EMSInfo.xml is error "+StringUtil.getStackTrace(e));
136 } catch (Exception e2) {
142 //this.log.debug("start loading " + cacheFilePath);
143 File file = new File(ftpconfig);
144 if(!file.exists() || !file.isFile()){
145 this.log.error("cacheFilePath " + ftpconfig+"not exist or is not File");
148 InputStream in = null;
150 properties = new Properties();
151 in = new FileInputStream(file);
154 }catch(Exception e) {
155 log.error("read ["+file.getAbsolutePath()+"]Exception :",e);
160 } catch (Exception e) {
166 public static synchronized List<EMSInfo> getAllEMSInfos(){
167 List<EMSInfo> list = new ArrayList<EMSInfo>();
168 for(EMSInfo emsinfo :emsInfoCache.values()){
174 public static synchronized EMSInfo getEMSInfoByName(String emsName){
175 EMSInfo emsInfo= emsInfoCache.get(emsName);
179 public static synchronized Properties getProperties() {