summaryrefslogtreecommitdiffstats
path: root/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/configmgr/ConfigurationManager.java
blob: b7346b5805ce6bc28996d24f2436281495449eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
 * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.vfc.nfvo.emsdriver.configmgr;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;
import org.onap.vfc.nfvo.emsdriver.commons.model.CrontabVo;
import org.onap.vfc.nfvo.emsdriver.commons.model.EMSInfo;
import org.onap.vfc.nfvo.emsdriver.commons.utils.DriverThread;
import org.onap.vfc.nfvo.emsdriver.commons.utils.StringUtil;
import org.onap.vfc.nfvo.emsdriver.commons.utils.XmlUtil;
import org.onap.vfc.nfvo.emsdriver.northbound.client.HttpClientUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class ConfigurationManager extends DriverThread{
	protected static Log log = LogFactory.getLog(ConfigurationManager.class);
	/**
	 * ESM Cache
	 */
	private static Map<String, EMSInfo> emsInfoCache = new ConcurrentHashMap<String, EMSInfo>();
	private static Map<String, CrontabVo> emsCrontab= new ConcurrentHashMap<String, CrontabVo>();
	private static List<String> emsIdList = new ArrayList<String>();
	private static Properties properties = null;
	
	private final static String  config = Constant.SYS_CFG + "config.properties";
	
	@Override
	public void dispose() {
		
		//this.log.debug("start loading " + cacheFilePath);
		File file = new File(config);
	    if(!file.exists() || !file.isFile()){
	    	log.error("cacheFilePath " + config+" not exist or is not File");
	    	return;
	    }
	    InputStream in  = null;
		try{
			properties = new Properties();
	        in = new FileInputStream(file);
	        properties.load(in);
	        Map<String, CrontabVo> emsMap = readCorntab();
	        
	        emsCrontab.putAll(emsMap);
	        
	        //
			new ReceiveSource().start();
		}catch(Exception e) {
			log.error("read ["+file.getAbsolutePath()+"]Exception :",e);
		}finally {
			if(in != null) {
				try {
					in.close();
				} catch (Exception e) {
				}
			}
		}
	}
	
	private Map<String, CrontabVo> readCorntab() {
		String path = Constant.SYS_CFG + "crontab.xml";
		File cfg = new File(path);
		log.debug("start loading " + path);
	    if(!cfg.exists() || !cfg.isFile()){
	    	log.debug("not exists " + path);
	    	return null;
	    }
	    
	    InputStream is = null;
	    Map<String, CrontabVo> tmpcache = new HashMap<String, CrontabVo>();
	    
	    try {
			is = new FileInputStream(cfg);
			Document doc = XmlUtil.getDocument(is);
			
			Element root = doc.getRootElement();
			
			@SuppressWarnings("unchecked")
			List<Element> children = root.getChildren();
			
			for(Iterator<Element> it = children.iterator();it.hasNext();){
				CrontabVo crontabVo = new CrontabVo();
				Element child = it.next();
				String type = child.getAttributeValue("type");
				if(StringUtil.isBank(type)){
					continue;
				}
				crontabVo.setType(type);
				if("ems-alarm".equalsIgnoreCase(type)){
					boolean iscollect =  Boolean.parseBoolean(child.getAttributeValue("iscollect"));
					if(iscollect){
						crontabVo.setIscollect(iscollect);
					}else{
						continue;
					}
					
					crontabVo.setRead_timeout(child.getChildText("readtimeout"));
				}else{
					String crontab = child.getAttributeValue("crontab");
					if(!StringUtil.isBank(type) && !StringUtil.isBank(crontab)){
						crontabVo.setCrontab(crontab);
					}else{
						continue;
					}
					crontabVo.setMatch(child.getChildText("match"));
					crontabVo.setGranularity(child.getChildText("granularity"));
				}
				tmpcache.put(type, crontabVo);
			}
			
		} catch (Exception e) {
			log.error("load crontab.xml is error "+StringUtil.getStackTrace(e));
		}finally{
			tmpcache.clear();
			try {
				if(is != null){
					is.close();
					is = null;
				}
			} catch (Exception e2) {
			}
			cfg = null;
		}
		return tmpcache;
	}


	public  void readcfg(){
		String path = Constant.SYS_CFG + "EMSInfo.xml";
		File cfg = new File(path);
		log.debug("start loading " + path);
	    if(!cfg.exists() || !cfg.isFile()){
	    	log.debug("not exists " + path);
	    	return;
	    }
	   
        
	    InputStream is = null;
	    Map<String, EMSInfo> tmpcache = new HashMap<String, EMSInfo>();
	    
	    try {
			is = new FileInputStream(cfg);
			Document doc = XmlUtil.getDocument(is);
			
			Element root = doc.getRootElement();
			
			@SuppressWarnings("unchecked")
			List<Element> children = root.getChildren();
			
			for(Iterator<Element> it = children.iterator();it.hasNext();){
				EMSInfo emsInfo = new EMSInfo();
				Element child = it.next();
				String name = child.getAttributeValue("name");
				if(StringUtil.isBank(name)){
					continue;
				}
				emsInfo.setName(name);
				
//				tmpcache.put(name, emsInfo);
				
				@SuppressWarnings("unchecked")
				List<Element> collectList = child.getChildren();
				for(Element collect : collectList){
					
					CollectVo collectVo = new CollectVo();
					
					String type = collect.getAttributeValue("type");
					if("alarm".equalsIgnoreCase(type)){
						boolean iscollect =  Boolean.parseBoolean(collect.getAttributeValue("iscollect"));
						if(iscollect){
							collectVo.setIscollect(iscollect);
						}else{
							continue;
						}
						collectVo.setType(type);
						collectVo.setIP(collect.getChildText("ip"));
						collectVo.setPort(collect.getChildText("port"));
						collectVo.setUser(collect.getChildText("user"));
						collectVo.setPassword(collect.getChildText("password"));
						collectVo.setRead_timeout(collect.getChildText("readtimeout"));
					}else{
						String crontab = collect.getAttributeValue("crontab");
						if(!StringUtil.isBank(type) && !StringUtil.isBank(crontab)){
							collectVo.setType(type);
							collectVo.setCrontab(crontab);
						}else{
							continue;
						}
						collectVo.setIP(collect.getChildText("ip"));
						collectVo.setPort(collect.getChildText("port"));
						collectVo.setUser(collect.getChildText("user"));
						collectVo.setPassword(collect.getChildText("password"));
						collectVo.setRemotepath(collect.getChildText("remotepath"));
						collectVo.setMatch(collect.getChildText("match"));
						collectVo.setPassive(collect.getChildText("passive"));
						collectVo.setFtptype(collect.getChildText("ftptype"));
						collectVo.setGranularity(collect.getChildText("granularity"));
					}
				
					emsInfo.putCollectMap(type, collectVo);
				}
				tmpcache.put(name, emsInfo);
			}
			emsInfoCache.putAll(tmpcache);
			
			File file = new File(config);
		    if(!file.exists() || !file.isFile()){
		    	log.error("cacheFilePath " + config+" not exist or is not File");
		    	return;
		    }
		    InputStream in  = null;
			try{
				properties = new Properties();
		        in = new FileInputStream(file);
		        properties.load(in);
			}catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			log.error("load EMSInfo.xml is error "+StringUtil.getStackTrace(e));
		}finally{
			tmpcache.clear();
			try {
				if(is != null){
					is.close();
					is = null;
				}
			} catch (Exception e2) {
			}
			cfg = null;
		}
	}
	
	
	public static synchronized List<EMSInfo> getAllEMSInfos(){
		List<EMSInfo> list = new ArrayList<EMSInfo>();
		for(EMSInfo emsinfo :emsInfoCache.values()){
			list.add(emsinfo);
		}
		return list;
	}
	
	public static synchronized EMSInfo getEMSInfoByName(String emsName){
		EMSInfo emsInfo= emsInfoCache.get(emsName);
		return emsInfo;
	}
	
	public  static synchronized Properties getProperties() {
		return properties;
	}
	
	class ReceiveSource extends Thread{
		long timeStamp = System.currentTimeMillis();
		
		public void run() {
			while(true){
				
				try {
					if(System.currentTimeMillis() - timeStamp > Constant.ONEMINUTE){
						timeStamp = System.currentTimeMillis();
						log.debug("ReceiveSource run");
					}
					//get emsId list
					List<String> emsIds = this.getEmsIdList();
					if(emsIds.size() > 0){
						emsIdList.clear();
						emsIdList.addAll(emsIds);
					}
					
					for(String emsId : emsIdList){
						//get emsInfo by emsId 
						Map<String, EMSInfo> emsInfoMap = this.getEmsInfo(emsId);
						
						emsInfoCache.putAll(emsInfoMap);
					}
					
					
					//
					if(emsInfoCache.size() > 0){
						Thread.sleep(5*60*1000);
					}else{
						Thread.sleep(60*1000);
					}
				} catch (Exception e) {
					log.error("ReceiveSource exception",e);
				}
			}
		}

		private Map<String, EMSInfo> getEmsInfo(String emsId) {
			Map<String, EMSInfo> tmpcache = new ConcurrentHashMap<String, EMSInfo>();
			String msbAddress = properties.getProperty("msbAddress");
			String emstUrl = properties.getProperty("esr_emsUrl");
			//set emsId to url
			String.format(emstUrl, emsId);
			String getemstUrl = "http://"+msbAddress+emstUrl;
			String emsResult = HttpClientUtil.doGet(getemstUrl, Constant.ENCODING_UTF8);
			log.debug(getemstUrl+" result="+emsResult);
			JSONObject reagobj = JSONObject.parseObject(emsResult);
			
			JSONObject  esr_system_info_list = reagobj.getJSONObject("esr-system-info-list");
			JSONArray esr_system_info = esr_system_info_list.getJSONArray("esr-system-info");
			Iterator<Object> it = esr_system_info.iterator();
			EMSInfo emsInfo = new EMSInfo();
			emsInfo.setName(emsId);
			tmpcache.put(emsId, emsInfo);
			while(it.hasNext()){
				Object obj = it.next();
				JSONObject collect = (JSONObject)obj;
				String system_type = (String)collect.get("system-type");
				CollectVo collectVo = new CollectVo();
				if("ems-resource".equalsIgnoreCase(system_type)){
					CrontabVo crontabVo = emsCrontab.get(system_type);
					if(crontabVo != null){
						collectVo.setType(system_type);
						collectVo.setCrontab(crontabVo.getCrontab());
						collectVo.setIP(collect.getString("ip-address"));
						collectVo.setPort(collect.getString("port"));
						collectVo.setUser(collect.getString("user-name"));
						collectVo.setPassword(collect.getString("password"));
					
						collectVo.setRemotepath(collect.getString("remote-path"));
						collectVo.setMatch(crontabVo.getMatch());
						collectVo.setPassive(collect.getString("passive"));
						collectVo.setGranularity(crontabVo.getGranularity());
					}
					
					
				}else if("ems-performance".equalsIgnoreCase(system_type)){
					CrontabVo crontabVo = emsCrontab.get(system_type);
					if(crontabVo != null){
						collectVo.setType(system_type);
						collectVo.setCrontab(crontabVo.getCrontab());
						collectVo.setIP(collect.getString("ip-address"));
						collectVo.setPort(collect.getString("port"));
						collectVo.setUser(collect.getString("user-name"));
						collectVo.setPassword(collect.getString("password"));
					
						collectVo.setRemotepath(collect.getString("remote-path"));
						collectVo.setMatch(crontabVo.getMatch());
						collectVo.setPassive(collect.getString("passive"));
						collectVo.setGranularity(crontabVo.getGranularity());
					}
				}else if("ems-alarm".equalsIgnoreCase(system_type)){
					CrontabVo crontabVo = emsCrontab.get(system_type);
					if(crontabVo != null){
						collectVo.setIscollect(crontabVo.isIscollect());
						collectVo.setType(system_type);
						collectVo.setIP(collect.getString("ip-address"));
						collectVo.setPort(collect.getString("port"));
						collectVo.setUser(collect.getString("user-name"));
						collectVo.setPassword(collect.getString("password"));
						collectVo.setRead_timeout(crontabVo.getRead_timeout());
					}else{
						log.error("emsCrontab.get(system_type) result crontabVo is null" );
					}
					
					
				}else{
					log.error("ems system-type ="+system_type+" ");
				}
				
				emsInfo.putCollectMap(system_type, collectVo);
			}
			return tmpcache;
		}

		private List<String> getEmsIdList() {
			List<String> emsIds = new ArrayList<String>();
			//http
			String msbAddress = properties.getProperty("msbAddress");
			String url = properties.getProperty("esr_ems_listUrl");
			String getemsListUrl = "http://"+msbAddress+url;
			
			String result = HttpClientUtil.doGet(getemsListUrl, Constant.ENCODING_UTF8);
			log.debug(getemsListUrl+" result="+result);
			JSONObject reagobj = JSONObject.parseObject(result);
			JSONArray  esr_emsIds = reagobj.getJSONArray("esr-ems");
			Iterator<Object> it = esr_emsIds.iterator();
			while(it.hasNext()){
				Object obj = it.next();
				JSONObject emsId = (JSONObject)obj;
				String emsIdStr = (String)emsId.get("ems-id");
				emsIds.add(emsIdStr);
			}
			return emsIds;
		}
	}
}