Modify emsdriver Code
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / configmgr / ConfigurationManager.java
1 /**
2  * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vfc.nfvo.emsdriver.configmgr;
17
18 import java.io.File;
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;
25 import java.util.Map;
26 import java.util.Properties;
27 import java.util.concurrent.ConcurrentHashMap;
28
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;
41
42 import com.alibaba.fastjson.JSONArray;
43 import com.alibaba.fastjson.JSONObject;
44
45
46 public class ConfigurationManager extends DriverThread{
47         protected static Log log = LogFactory.getLog(ConfigurationManager.class);
48         /**
49          * ESM Cache
50          */
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;
55         
56         private final static String  config = Constant.SYS_CFG + "config.properties";
57         
58         @Override
59         public void dispose() {
60                 
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");
65                 return;
66             }
67             InputStream in  = null;
68                 try{
69                         properties = new Properties();
70                 in = new FileInputStream(file);
71                 properties.load(in);
72                 Map<String, CrontabVo> emsMap = readCorntab();
73                 
74                 emsCrontab.putAll(emsMap);
75                 
76                 //
77                         new ReceiveSource().start();
78                 }catch(Exception e) {
79                         log.error("read ["+file.getAbsolutePath()+"]Exception :",e);
80                 }finally {
81                         if(in != null) {
82                                 try {
83                                         in.close();
84                                 } catch (Exception e) {
85                                 }
86                         }
87                 }
88         }
89         
90         public 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);
96                 return null;
97             }
98             InputStream is = null;
99             Map<String, CrontabVo> tmpcache = new HashMap<String, CrontabVo>();
100             
101             try {
102                         is = new FileInputStream(cfg);
103                         Document doc = XmlUtil.getDocument(is);
104                         
105                         Element root = doc.getRootElement();
106                         
107                         @SuppressWarnings("unchecked")
108                         List<Element> children = root.getChildren();
109                         
110                         for(Iterator<Element> it = children.iterator();it.hasNext();){
111                                 CrontabVo crontabVo = new CrontabVo();
112                                 Element child = it.next();
113                                 String type = child.getAttributeValue("type");
114                                 if(StringUtil.isBank(type)){
115                                         continue;
116                                 }
117                                 crontabVo.setType(type);
118                                 if(Constant.COLLECT_TYPE_ALARM.equalsIgnoreCase(type)){
119                                         boolean iscollect =  Boolean.parseBoolean(child.getAttributeValue("iscollect"));
120                                         if(iscollect){
121                                                 crontabVo.setIscollect(iscollect);
122                                         }else{
123                                                 continue;
124                                         }
125                                         
126                                         crontabVo.setRead_timeout(child.getChildText("readtimeout"));
127                                 }else{
128                                         String crontab = child.getAttributeValue("crontab");
129                                         if(!StringUtil.isBank(type) && !StringUtil.isBank(crontab)){
130                                                 crontabVo.setCrontab(crontab);
131                                         }else{
132                                                 continue;
133                                         }
134                                         crontabVo.setMatch(child.getChildText("match"));
135                                         crontabVo.setGranularity(child.getChildText("granularity"));
136                                 }
137                                 tmpcache.put(type.toUpperCase(), crontabVo);
138                         }
139                         
140                 } catch (Exception e) {
141                         log.error("load crontab.xml is error "+StringUtil.getStackTrace(e));
142                 }finally{
143                         try {
144                                 if(is != null){
145                                         is.close();
146                                         is = null;
147                                 }
148                         } catch (Exception e2) {
149                         }
150                         cfg = null;
151                 }
152                 return tmpcache;
153         }
154
155
156         public  void readcfg(){
157                 String path = Constant.SYS_CFG + "EMSInfo.xml";
158                 File cfg = new File(path);
159                 log.debug("start loading " + path);
160             if(!cfg.exists() || !cfg.isFile()){
161                 log.debug("not exists " + path);
162                 return;
163             }
164            
165         
166             InputStream is = null;
167             Map<String, EMSInfo> tmpcache = new HashMap<String, EMSInfo>();
168             
169             try {
170                         is = new FileInputStream(cfg);
171                         Document doc = XmlUtil.getDocument(is);
172                         
173                         Element root = doc.getRootElement();
174                         
175                         @SuppressWarnings("unchecked")
176                         List<Element> children = root.getChildren();
177                         
178                         for(Iterator<Element> it = children.iterator();it.hasNext();){
179                                 EMSInfo emsInfo = new EMSInfo();
180                                 Element child = it.next();
181                                 String name = child.getAttributeValue("name");
182                                 if(StringUtil.isBank(name)){
183                                         continue;
184                                 }
185                                 emsInfo.setName(name);
186                                 
187 //                              tmpcache.put(name, emsInfo);
188                                 
189                                 @SuppressWarnings("unchecked")
190                                 List<Element> collectList = child.getChildren();
191                                 for(Element collect : collectList){
192                                         
193                                         CollectVo collectVo = new CollectVo();
194                                         
195                                         String type = collect.getAttributeValue("type");
196                                         if("alarm".equalsIgnoreCase(type)){
197                                                 boolean iscollect =  Boolean.parseBoolean(collect.getAttributeValue("iscollect"));
198                                                 if(iscollect){
199                                                         collectVo.setIscollect(iscollect);
200                                                 }else{
201                                                         continue;
202                                                 }
203                                                 collectVo.setType(type);
204                                                 collectVo.setIP(collect.getChildText("ip"));
205                                                 collectVo.setPort(collect.getChildText("port"));
206                                                 collectVo.setUser(collect.getChildText("user"));
207                                                 collectVo.setPassword(collect.getChildText("password"));
208                                                 collectVo.setRead_timeout(collect.getChildText("readtimeout"));
209                                         }else{
210                                                 String crontab = collect.getAttributeValue("crontab");
211                                                 if(!StringUtil.isBank(type) && !StringUtil.isBank(crontab)){
212                                                         collectVo.setType(type);
213                                                         collectVo.setCrontab(crontab);
214                                                 }else{
215                                                         continue;
216                                                 }
217                                                 collectVo.setIP(collect.getChildText("ip"));
218                                                 collectVo.setPort(collect.getChildText("port"));
219                                                 collectVo.setUser(collect.getChildText("user"));
220                                                 collectVo.setPassword(collect.getChildText("password"));
221                                                 collectVo.setRemotepath(collect.getChildText("remotepath"));
222                                                 collectVo.setMatch(collect.getChildText("match"));
223                                                 collectVo.setPassive(collect.getChildText("passive"));
224                                                 collectVo.setFtptype(collect.getChildText("ftptype"));
225                                                 collectVo.setGranularity(collect.getChildText("granularity"));
226                                         }
227                                 
228                                         emsInfo.putCollectMap(type, collectVo);
229                                 }
230                                 tmpcache.put(name, emsInfo);
231                         }
232                         emsInfoCache.putAll(tmpcache);
233                         
234                         File file = new File(config);
235                     if(!file.exists() || !file.isFile()){
236                         log.error("cacheFilePath " + config+" not exist or is not File");
237                         return;
238                     }
239                     InputStream in  = null;
240                         try{
241                                 properties = new Properties();
242                         in = new FileInputStream(file);
243                         properties.load(in);
244                         }catch (Exception e) {
245                                 e.printStackTrace();
246                         }
247                 } catch (Exception e) {
248                         log.error("load EMSInfo.xml is error "+StringUtil.getStackTrace(e));
249                 }finally{
250                         tmpcache.clear();
251                         try {
252                                 if(is != null){
253                                         is.close();
254                                         is = null;
255                                 }
256                         } catch (Exception e2) {
257                         }
258                         cfg = null;
259                 }
260         }
261         
262         
263         public static synchronized List<EMSInfo> getAllEMSInfos(){
264                 List<EMSInfo> list = new ArrayList<EMSInfo>();
265                 for(EMSInfo emsinfo :emsInfoCache.values()){
266                         list.add(emsinfo);
267                 }
268                 return list;
269         }
270         
271         public static synchronized EMSInfo getEMSInfoByName(String emsName){
272                 EMSInfo emsInfo= emsInfoCache.get(emsName);
273                 return emsInfo;
274         }
275         
276         public  static synchronized Properties getProperties() {
277                 return properties;
278         }
279         
280         class ReceiveSource extends Thread{
281                 long timeStamp = System.currentTimeMillis();
282                 
283                 public void run() {
284                         while(true){
285                                 
286                                 try {
287                                         if(System.currentTimeMillis() - timeStamp > Constant.ONEMINUTE){
288                                                 timeStamp = System.currentTimeMillis();
289                                                 log.debug("ReceiveSource run");
290                                         }
291                                         //get emsId list
292                                         List<String> emsIds = this.getEmsIdList();
293                                         if(emsIds.size() > 0){
294                                                 emsIdList.clear();
295                                                 emsIdList.addAll(emsIds);
296                                         }
297                                         
298                                         for(String emsId : emsIdList){
299                                                 //get emsInfo by emsId 
300                                                 Map<String, EMSInfo> emsInfoMap = this.getEmsInfo(emsId);
301                                                 
302                                                 emsInfoCache.putAll(emsInfoMap);
303                                         }
304                                         
305                                         
306                                         //
307                                         if(emsInfoCache.size() > 0){
308                                                 Thread.sleep(30*60*1000);
309                                         }else{
310                                                 Thread.sleep(60*1000);
311                                         }
312                                 } catch (Exception e) {
313                                         try {
314                                                 Thread.sleep(60*1000);
315                                         } catch (InterruptedException e1) {
316                                                 e1.printStackTrace();
317                                         }
318                                         log.error("ReceiveSource exception",e);
319                                 }
320                         }
321                 }
322
323                 private Map<String, EMSInfo> getEmsInfo(String emsId) {
324                         Map<String, EMSInfo> tmpcache = new ConcurrentHashMap<String, EMSInfo>();
325                         String msbAddress = properties.getProperty("msbAddress");
326                         String emstUrl = properties.getProperty("esr_emsUrl");
327                         //set emsId to url
328                         emstUrl = String.format(emstUrl, emsId);
329                         String getemstUrl = "http://"+msbAddress+emstUrl;
330                         String emsResult = HttpClientUtil.doGet(getemstUrl, Constant.ENCODING_UTF8);
331                         log.debug(getemstUrl+" result="+emsResult);
332                         JSONObject reagobj = JSONObject.parseObject(emsResult);
333                         
334                         JSONObject  esr_system_info_list = reagobj.getJSONObject("esr-system-info-list");
335                         JSONArray esr_system_info = esr_system_info_list.getJSONArray("esr-system-info");
336                         Iterator<Object> it = esr_system_info.iterator();
337                         EMSInfo emsInfo = new EMSInfo();
338                         emsInfo.setName(emsId);
339                         tmpcache.put(emsId, emsInfo);
340                         while(it.hasNext()){
341                                 Object obj = it.next();
342                                 JSONObject collect = (JSONObject)obj;
343                                 String system_type = (String)collect.get("system-type");
344                                 CollectVo collectVo = new CollectVo();
345                                 if(Constant.COLLECT_TYPE_CM.equalsIgnoreCase(system_type)){
346                                         CrontabVo crontabVo = emsCrontab.get(system_type);
347                                         if(crontabVo != null){
348                                                 collectVo.setType(system_type);
349                                                 collectVo.setCrontab(crontabVo.getCrontab());
350                                                 collectVo.setIP(collect.getString("ip-address"));
351                                                 collectVo.setPort(collect.getString("port"));
352                                                 collectVo.setUser(collect.getString("user-name"));
353                                                 collectVo.setPassword(collect.getString("password"));
354                                         
355                                                 collectVo.setRemotepath(collect.getString("remote-path"));
356                                                 collectVo.setMatch(crontabVo.getMatch());
357                                                 collectVo.setPassive(collect.getString("passive"));
358                                                 collectVo.setGranularity(crontabVo.getGranularity());
359                                         }else{
360                                                 log.error("emsCrontab.get(system_type) result crontabVo is null system_type=["+system_type+"] emsCrontabMap="+emsCrontab );
361                                         }
362                                         
363                                         
364                                 }else if(Constant.COLLECT_TYPE_PM.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"));
373                                         
374                                                 collectVo.setRemotepath(collect.getString("remote-path"));
375                                                 collectVo.setMatch(crontabVo.getMatch());
376                                                 collectVo.setPassive(collect.getString("passive"));
377                                                 collectVo.setGranularity(crontabVo.getGranularity());
378                                         }else{
379                                                 log.error("emsCrontab.get(system_type) result crontabVo is null system_type=["+system_type+"]" );
380                                         }
381                                 }else if(Constant.COLLECT_TYPE_ALARM.equalsIgnoreCase(system_type)){
382                                         CrontabVo crontabVo = emsCrontab.get(system_type);
383                                         if(crontabVo != null){
384                                                 collectVo.setIscollect(crontabVo.isIscollect());
385                                                 collectVo.setType(system_type);
386                                                 collectVo.setIP(collect.getString("ip-address"));
387                                                 collectVo.setPort(collect.getString("port"));
388                                                 collectVo.setUser(collect.getString("user-name"));
389                                                 collectVo.setPassword(collect.getString("password"));
390                                                 collectVo.setRead_timeout(crontabVo.getRead_timeout());
391                                         }else{
392                                                 log.error("emsCrontab.get(system_type) result crontabVo is null system_type=["+system_type+"]");
393                                         }
394                                         
395                                         
396                                 }else{
397                                         log.error("ems system-type ="+system_type+" ");
398                                 }
399                                 
400                                 emsInfo.putCollectMap(system_type, collectVo);
401                         }
402                         return tmpcache;
403                 }
404
405                 private List<String> getEmsIdList() {
406                         List<String> emsIds = new ArrayList<String>();
407                         //http
408                         String msbAddress = properties.getProperty("msbAddress");
409                         String url = properties.getProperty("esr_ems_listUrl");
410                         String getemsListUrl = "http://"+msbAddress+url;
411                         
412                         String result = HttpClientUtil.doGet(getemsListUrl, Constant.ENCODING_UTF8);
413                         log.debug(getemsListUrl+" result="+result);
414                         JSONObject reagobj = JSONObject.parseObject(result);
415                         JSONArray  esr_emsIds = reagobj.getJSONArray("esr-ems");
416                         Iterator<Object> it = esr_emsIds.iterator();
417                         while(it.hasNext()){
418                                 Object obj = it.next();
419                                 JSONObject emsId = (JSONObject)obj;
420                                 String emsIdStr = (String)emsId.get("ems-id");
421                                 emsIds.add(emsIdStr);
422                         }
423                         return emsIds;
424                 }
425         }
426 }