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 private 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);
99 InputStream is = null;
100 Map<String, CrontabVo> tmpcache = new HashMap<String, CrontabVo>();
103 is = new FileInputStream(cfg);
104 Document doc = XmlUtil.getDocument(is);
106 Element root = doc.getRootElement();
108 @SuppressWarnings("unchecked")
109 List<Element> children = root.getChildren();
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)){
118 crontabVo.setType(type);
119 if("ems-alarm".equalsIgnoreCase(type)){
120 boolean iscollect = Boolean.parseBoolean(child.getAttributeValue("iscollect"));
122 crontabVo.setIscollect(iscollect);
127 crontabVo.setRead_timeout(child.getChildText("readtimeout"));
129 String crontab = child.getAttributeValue("crontab");
130 if(!StringUtil.isBank(type) && !StringUtil.isBank(crontab)){
131 crontabVo.setCrontab(crontab);
135 crontabVo.setMatch(child.getChildText("match"));
136 crontabVo.setGranularity(child.getChildText("granularity"));
138 tmpcache.put(type, crontabVo);
141 } catch (Exception e) {
142 log.error("load crontab.xml is error "+StringUtil.getStackTrace(e));
150 } catch (Exception e2) {
158 public void readcfg(){
159 String path = Constant.SYS_CFG + "EMSInfo.xml";
160 File cfg = new File(path);
161 log.debug("start loading " + path);
162 if(!cfg.exists() || !cfg.isFile()){
163 log.debug("not exists " + path);
168 InputStream is = null;
169 Map<String, EMSInfo> tmpcache = new HashMap<String, EMSInfo>();
172 is = new FileInputStream(cfg);
173 Document doc = XmlUtil.getDocument(is);
175 Element root = doc.getRootElement();
177 @SuppressWarnings("unchecked")
178 List<Element> children = root.getChildren();
180 for(Iterator<Element> it = children.iterator();it.hasNext();){
181 EMSInfo emsInfo = new EMSInfo();
182 Element child = it.next();
183 String name = child.getAttributeValue("name");
184 if(StringUtil.isBank(name)){
187 emsInfo.setName(name);
189 // tmpcache.put(name, emsInfo);
191 @SuppressWarnings("unchecked")
192 List<Element> collectList = child.getChildren();
193 for(Element collect : collectList){
195 CollectVo collectVo = new CollectVo();
197 String type = collect.getAttributeValue("type");
198 if("alarm".equalsIgnoreCase(type)){
199 boolean iscollect = Boolean.parseBoolean(collect.getAttributeValue("iscollect"));
201 collectVo.setIscollect(iscollect);
205 collectVo.setType(type);
206 collectVo.setIP(collect.getChildText("ip"));
207 collectVo.setPort(collect.getChildText("port"));
208 collectVo.setUser(collect.getChildText("user"));
209 collectVo.setPassword(collect.getChildText("password"));
210 collectVo.setRead_timeout(collect.getChildText("readtimeout"));
212 String crontab = collect.getAttributeValue("crontab");
213 if(!StringUtil.isBank(type) && !StringUtil.isBank(crontab)){
214 collectVo.setType(type);
215 collectVo.setCrontab(crontab);
219 collectVo.setIP(collect.getChildText("ip"));
220 collectVo.setPort(collect.getChildText("port"));
221 collectVo.setUser(collect.getChildText("user"));
222 collectVo.setPassword(collect.getChildText("password"));
223 collectVo.setRemotepath(collect.getChildText("remotepath"));
224 collectVo.setMatch(collect.getChildText("match"));
225 collectVo.setPassive(collect.getChildText("passive"));
226 collectVo.setFtptype(collect.getChildText("ftptype"));
227 collectVo.setGranularity(collect.getChildText("granularity"));
230 emsInfo.putCollectMap(type, collectVo);
232 tmpcache.put(name, emsInfo);
234 emsInfoCache.putAll(tmpcache);
236 File file = new File(config);
237 if(!file.exists() || !file.isFile()){
238 log.error("cacheFilePath " + config+" not exist or is not File");
241 InputStream in = null;
243 properties = new Properties();
244 in = new FileInputStream(file);
246 }catch (Exception e) {
249 } catch (Exception e) {
250 log.error("load EMSInfo.xml is error "+StringUtil.getStackTrace(e));
258 } catch (Exception e2) {
265 public static synchronized List<EMSInfo> getAllEMSInfos(){
266 List<EMSInfo> list = new ArrayList<EMSInfo>();
267 for(EMSInfo emsinfo :emsInfoCache.values()){
273 public static synchronized EMSInfo getEMSInfoByName(String emsName){
274 EMSInfo emsInfo= emsInfoCache.get(emsName);
278 public static synchronized Properties getProperties() {
282 class ReceiveSource extends Thread{
283 long timeStamp = System.currentTimeMillis();
289 if(System.currentTimeMillis() - timeStamp > Constant.ONEMINUTE){
290 timeStamp = System.currentTimeMillis();
291 log.debug("ReceiveSource run");
294 List<String> emsIds = this.getEmsIdList();
295 if(emsIds.size() > 0){
297 emsIdList.addAll(emsIds);
300 for(String emsId : emsIdList){
301 //get emsInfo by emsId
302 Map<String, EMSInfo> emsInfoMap = this.getEmsInfo(emsId);
304 emsInfoCache.putAll(emsInfoMap);
309 if(emsInfoCache.size() > 0){
310 Thread.sleep(5*60*1000);
312 Thread.sleep(60*1000);
314 } catch (Exception e) {
316 Thread.sleep(60*1000);
317 } catch (InterruptedException e1) {
318 e1.printStackTrace();
320 log.error("ReceiveSource exception",e);
325 private Map<String, EMSInfo> getEmsInfo(String emsId) {
326 Map<String, EMSInfo> tmpcache = new ConcurrentHashMap<String, EMSInfo>();
327 String msbAddress = properties.getProperty("msbAddress");
328 String emstUrl = properties.getProperty("esr_emsUrl");
330 String.format(emstUrl, emsId);
331 String getemstUrl = "http://"+msbAddress+emstUrl;
332 String emsResult = HttpClientUtil.doGet(getemstUrl, Constant.ENCODING_UTF8);
333 log.debug(getemstUrl+" result="+emsResult);
334 JSONObject reagobj = JSONObject.parseObject(emsResult);
336 JSONObject esr_system_info_list = reagobj.getJSONObject("esr-system-info-list");
337 JSONArray esr_system_info = esr_system_info_list.getJSONArray("esr-system-info");
338 Iterator<Object> it = esr_system_info.iterator();
339 EMSInfo emsInfo = new EMSInfo();
340 emsInfo.setName(emsId);
341 tmpcache.put(emsId, emsInfo);
343 Object obj = it.next();
344 JSONObject collect = (JSONObject)obj;
345 String system_type = (String)collect.get("system-type");
346 CollectVo collectVo = new CollectVo();
347 if("ems-resource".equalsIgnoreCase(system_type)){
348 CrontabVo crontabVo = emsCrontab.get(system_type);
349 if(crontabVo != null){
350 collectVo.setType(system_type);
351 collectVo.setCrontab(crontabVo.getCrontab());
352 collectVo.setIP(collect.getString("ip-address"));
353 collectVo.setPort(collect.getString("port"));
354 collectVo.setUser(collect.getString("user-name"));
355 collectVo.setPassword(collect.getString("password"));
357 collectVo.setRemotepath(collect.getString("remote-path"));
358 collectVo.setMatch(crontabVo.getMatch());
359 collectVo.setPassive(collect.getString("passive"));
360 collectVo.setGranularity(crontabVo.getGranularity());
364 }else if("ems-performance".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 }else if("ems-alarm".equalsIgnoreCase(system_type)){
380 CrontabVo crontabVo = emsCrontab.get(system_type);
381 if(crontabVo != null){
382 collectVo.setIscollect(crontabVo.isIscollect());
383 collectVo.setType(system_type);
384 collectVo.setIP(collect.getString("ip-address"));
385 collectVo.setPort(collect.getString("port"));
386 collectVo.setUser(collect.getString("user-name"));
387 collectVo.setPassword(collect.getString("password"));
388 collectVo.setRead_timeout(crontabVo.getRead_timeout());
390 log.error("emsCrontab.get(system_type) result crontabVo is null" );
395 log.error("ems system-type ="+system_type+" ");
398 emsInfo.putCollectMap(system_type, collectVo);
403 private List<String> getEmsIdList() {
404 List<String> emsIds = new ArrayList<String>();
406 String msbAddress = properties.getProperty("msbAddress");
407 String url = properties.getProperty("esr_ems_listUrl");
408 String getemsListUrl = "http://"+msbAddress+url;
410 String result = HttpClientUtil.doGet(getemsListUrl, Constant.ENCODING_UTF8);
411 log.debug(getemsListUrl+" result="+result);
412 JSONObject reagobj = JSONObject.parseObject(result);
413 JSONArray esr_emsIds = reagobj.getJSONArray("esr-ems");
414 Iterator<Object> it = esr_emsIds.iterator();
416 Object obj = it.next();
417 JSONObject emsId = (JSONObject)obj;
418 String emsIdStr = (String)emsId.get("ems-id");
419 emsIds.add(emsIdStr);