2 * Copyright 2017 BOCO Corporation
3 * Copyright 2018 CMCC Technologies Co. Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.emsdriver.configmgr;
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;
36 import java.io.FileInputStream;
37 import java.io.FileNotFoundException;
38 import java.io.InputStream;
40 import java.util.concurrent.ConcurrentHashMap;
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);
50 static Map<String, EMSInfo> emsInfoCache = new ConcurrentHashMap<>();
52 static Properties properties = null;
53 private static Map<String, CrontabVo> emsCrontab = new ConcurrentHashMap<>();
54 private static List<String> emsIdList = new ArrayList<>();
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");
62 InputStream in = new FileInputStream(file);
63 properties = new Properties();
65 } catch (Exception e) {
66 // TODO Auto-generated catch block
67 logger.error("read [" + file.getAbsolutePath() + "]Exception :", e);
73 public static synchronized List<EMSInfo> getAllEMSInfos() {
74 List<EMSInfo> list = new ArrayList<>();
75 for (EMSInfo emsinfo : emsInfoCache.values()) {
81 public static synchronized EMSInfo getEMSInfoByName(String emsName) {
82 return emsInfoCache.get(emsName);
85 public static synchronized Properties getProperties() {
90 public void dispose() {
91 Map<String, CrontabVo> emsMap = readCorntab();
92 emsCrontab.putAll(emsMap);
93 new ReceiveSource().start();
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);
105 Map<String, CrontabVo> tmpcache = new HashMap<>();
108 InputStream is = new FileInputStream(cfg)){
109 Document doc = XmlUtil.getDocument(is);
111 Element root = doc.getRootElement();
113 @SuppressWarnings("unchecked")
114 List<Element> children = root.getChildren();
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)) {
123 crontabVo.setType(type);
124 if (Constant.COLLECT_TYPE_ALARM.equalsIgnoreCase(type)) {
125 boolean iscollect = Boolean.parseBoolean(child.getAttributeValue("iscollect"));
127 crontabVo.setIscollect(iscollect);
132 crontabVo.setReadTimeout(child.getChildText("readtimeout"));
134 String crontab = child.getAttributeValue("crontab");
135 if (!StringUtil.isBank(type) && !StringUtil.isBank(crontab)) {
136 crontabVo.setCrontab(crontab);
140 crontabVo.setMatch(child.getChildText("match"));
141 crontabVo.setGranularity(child.getChildText("granularity"));
143 tmpcache.put(type.toUpperCase(), crontabVo);
146 } catch (Exception e) {
147 logger.error("load crontab.xml is error " + StringUtil.getStackTrace(e));
152 class ReceiveSource extends Thread {
153 long timeStamp = System.currentTimeMillis();
160 if (System.currentTimeMillis() - timeStamp > Constant.ONEMINUTE) {
161 timeStamp = System.currentTimeMillis();
162 logger.debug("ReceiveSource run");
165 List<String> emsIds = this.getEmsIdList();
166 if (!emsIds.isEmpty()) {
168 emsIdList.addAll(emsIds);
171 for (String emsId : emsIdList) {
172 //get emsInfo by emsId
173 Map<String, EMSInfo> emsInfoMap = this.getEmsInfo(emsId);
175 emsInfoCache.putAll(emsInfoMap);
177 if (emsInfoCache.size() > 0) {
178 Thread.sleep(30 * 60 * 1000L);
180 Thread.sleep(60 * 1000L);
182 } catch (Exception e) {
184 Thread.sleep(60 * 1000L);
185 } catch (Exception e1) {
186 logger.error("InterruptedException :" + StringUtil.getStackTrace(e1));
188 logger.error("ReceiveSource exception", e);
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");
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);
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));
229 collectVo.setRemotepath(collect.getString("remote-path"));
230 collectVo.setMatch(crontabVo.getMatch());
231 collectVo.setPassive(collect.getString("passive"));
232 collectVo.setGranularity(crontabVo.getGranularity());
234 logger.error("emsCrontab.get(systemType) result crontabVo is null systemType=[" + systemType + "] emsCrontabMap=" + emsCrontab);
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));
248 collectVo.setRemotepath(collect.getString("remote-path"));
249 collectVo.setMatch(crontabVo.getMatch());
250 collectVo.setPassive(collect.getString("passive"));
251 collectVo.setGranularity(crontabVo.getGranularity());
253 logger.error("emsCrontab.get(systemType) result crontabVo is null systemType=[" + systemType + "]");
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());
266 logger.error("emsCrontab.get(systemType) result crontabVo is null systemType=[" + systemType + "]");
271 logger.error("ems system-type =" + systemType + " ");
274 emsInfo.putCollectMap(systemType, collectVo);
279 private List<String> getEmsIdList() {
280 List<String> emsIds = new ArrayList<>();
282 String msbAddress = properties.getProperty("msbAddress");
283 String url = properties.getProperty("esr_ems_listUrl");
284 String getemsListUrl = "http://" + msbAddress + url;
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);