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.InputStream;
39 import java.util.concurrent.ConcurrentHashMap;
42 public class ConfigurationManager extends DriverThread {
43 public static final String CONFIG_PROPERTIES_LOCATION = Constant.SYS_CFG + "config.properties";
44 protected static final Log log = LogFactory.getLog(ConfigurationManager.class);
49 static Map<String, EMSInfo> emsInfoCache = new ConcurrentHashMap<>();
51 static Properties properties = null;
52 private static Map<String, CrontabVo> emsCrontab = new ConcurrentHashMap<>();
53 private static List<String> emsIdList = new ArrayList<>();
55 public static synchronized List<EMSInfo> getAllEMSInfos() {
56 List<EMSInfo> list = new ArrayList<>();
57 for (EMSInfo emsinfo : emsInfoCache.values()) {
63 public static synchronized EMSInfo getEMSInfoByName(String emsName) {
64 return emsInfoCache.get(emsName);
67 public static synchronized Properties getProperties() {
72 public void dispose() {
74 File file = new File(CONFIG_PROPERTIES_LOCATION);
75 if (!file.exists() || !file.isFile()) {
76 log.error("cacheFilePath " + CONFIG_PROPERTIES_LOCATION + " not exist or is not File");
79 try(InputStream in = new FileInputStream(file)){
80 properties = new Properties();
82 Map<String, CrontabVo> emsMap = readCorntab();
84 emsCrontab.putAll(emsMap);
86 new ReceiveSource().start();
87 } catch (Exception e) {
88 log.error("read [" + file.getAbsolutePath() + "]Exception :", e);
92 public Map<String, CrontabVo> readCorntab() {
93 String path = Constant.SYS_CFG + "crontab.xml";
94 File cfg = new File(path);
95 log.debug("start loading " + path);
96 if (!cfg.exists() || !cfg.isFile()) {
97 log.debug("not exists " + path);
100 Map<String, CrontabVo> tmpcache = new HashMap<>();
103 InputStream 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 (Constant.COLLECT_TYPE_ALARM.equalsIgnoreCase(type)) {
120 boolean iscollect = Boolean.parseBoolean(child.getAttributeValue("iscollect"));
122 crontabVo.setIscollect(iscollect);
127 crontabVo.setReadTimeout(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.toUpperCase(), crontabVo);
141 } catch (Exception e) {
142 log.error("load crontab.xml is error " + StringUtil.getStackTrace(e));
147 class ReceiveSource extends Thread {
148 long timeStamp = System.currentTimeMillis();
155 if (System.currentTimeMillis() - timeStamp > Constant.ONEMINUTE) {
156 timeStamp = System.currentTimeMillis();
157 log.debug("ReceiveSource run");
160 List<String> emsIds = this.getEmsIdList();
161 if (emsIds.size() > 0) {
163 emsIdList.addAll(emsIds);
166 for (String emsId : emsIdList) {
167 //get emsInfo by emsId
168 Map<String, EMSInfo> emsInfoMap = this.getEmsInfo(emsId);
170 emsInfoCache.putAll(emsInfoMap);
172 if (emsInfoCache.size() > 0) {
173 Thread.sleep(30 * 60 * 1000L);
175 Thread.sleep(60 * 1000L);
177 } catch (Exception e) {
179 Thread.sleep(60 * 1000L);
180 } catch (Exception e1) {
181 log.error("InterruptedException :" + StringUtil.getStackTrace(e1));
183 log.error("ReceiveSource exception", e);
188 private Map<String, EMSInfo> getEmsInfo(String emsId) {
189 Map<String, EMSInfo> tmpcache = new ConcurrentHashMap<>();
190 String msbAddress = properties.getProperty("msbAddress");
191 String emstUrl = properties.getProperty("esr_emsUrl");
193 emstUrl = String.format(emstUrl, emsId);
194 String getemstUrl = "http://" + msbAddress + emstUrl;
195 String emsResult = HttpClientUtil.doGet(getemstUrl, Constant.ENCODING_UTF8);
196 log.debug(getemstUrl + " result=" + emsResult);
197 JSONObject reagobj = JSONObject.parseObject(emsResult);
199 JSONObject esrSystemInfoList = reagobj.getJSONObject("esr-system-info-list");
200 JSONArray esrSystemInfo = esrSystemInfoList.getJSONArray("esr-system-info");
201 Iterator<Object> it = esrSystemInfo.iterator();
202 EMSInfo emsInfo = new EMSInfo();
203 emsInfo.setName(emsId);
204 tmpcache.put(emsId, emsInfo);
205 while (it.hasNext()) {
206 Object obj = it.next();
207 JSONObject collect = (JSONObject) obj;
208 String systemType = (String) collect.get("system-type");
209 CollectVo collectVo = new CollectVo();
210 if (Constant.COLLECT_TYPE_CM.equalsIgnoreCase(systemType)) {
211 CrontabVo crontabVo = emsCrontab.get(systemType);
212 if (crontabVo != null) {
213 collectVo.setType(systemType);
214 collectVo.setCrontab(crontabVo.getCrontab());
215 collectVo.setIP(collect.getString("ip-address"));
216 collectVo.setPort(collect.getString("port"));
217 collectVo.setUser(collect.getString("user-name"));
218 collectVo.setPassword(collect.getString("password"));
220 collectVo.setRemotepath(collect.getString("remote-path"));
221 collectVo.setMatch(crontabVo.getMatch());
222 collectVo.setPassive(collect.getString("passive"));
223 collectVo.setGranularity(crontabVo.getGranularity());
225 log.error("emsCrontab.get(systemType) result crontabVo is null systemType=[" + systemType + "] emsCrontabMap=" + emsCrontab);
229 } else if (Constant.COLLECT_TYPE_PM.equalsIgnoreCase(systemType)) {
230 CrontabVo crontabVo = emsCrontab.get(systemType);
231 if (crontabVo != null) {
232 collectVo.setType(systemType);
233 collectVo.setCrontab(crontabVo.getCrontab());
234 collectVo.setIP(collect.getString("ip-address"));
235 collectVo.setPort(collect.getString("port"));
236 collectVo.setUser(collect.getString("user-name"));
237 collectVo.setPassword(collect.getString("password"));
239 collectVo.setRemotepath(collect.getString("remote-path"));
240 collectVo.setMatch(crontabVo.getMatch());
241 collectVo.setPassive(collect.getString("passive"));
242 collectVo.setGranularity(crontabVo.getGranularity());
244 log.error("emsCrontab.get(systemType) result crontabVo is null systemType=[" + systemType + "]");
246 } else if (Constant.COLLECT_TYPE_ALARM.equalsIgnoreCase(systemType)) {
247 CrontabVo crontabVo = emsCrontab.get(systemType);
248 if (crontabVo != null) {
249 collectVo.setIscollect(crontabVo.isIscollect());
250 collectVo.setType(systemType);
251 collectVo.setIP(collect.getString("ip-address"));
252 collectVo.setPort(collect.getString("port"));
253 collectVo.setUser(collect.getString("user-name"));
254 collectVo.setPassword(collect.getString("password"));
255 collectVo.setReadTimeout(crontabVo.getReadTimeout());
257 log.error("emsCrontab.get(systemType) result crontabVo is null systemType=[" + systemType + "]");
262 log.error("ems system-type =" + systemType + " ");
265 emsInfo.putCollectMap(systemType, collectVo);
270 private List<String> getEmsIdList() {
271 List<String> emsIds = new ArrayList<>();
273 String msbAddress = properties.getProperty("msbAddress");
274 String url = properties.getProperty("esr_ems_listUrl");
275 String getemsListUrl = "http://" + msbAddress + url;
277 String result = HttpClientUtil.doGet(getemsListUrl, Constant.ENCODING_UTF8);
278 log.debug(getemsListUrl + " result=" + result);
279 JSONObject reagobj = JSONObject.parseObject(result);
280 JSONArray esrEmsIds = reagobj.getJSONArray("esr-ems");
281 Iterator<Object> it = esrEmsIds.iterator();
282 while (it.hasNext()) {
283 Object obj = it.next();
284 JSONObject emsId = (JSONObject) obj;
285 String emsIdStr = (String) emsId.get("ems-id");
286 emsIds.add(emsIdStr);