923d2ede6f8b38136b5c7852e203fd34da53bdf2
[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         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);
96                 return null;
97             }
98             
99             InputStream is = null;
100             Map<String, CrontabVo> tmpcache = new HashMap<String, CrontabVo>();
101             
102             try {
103                         is = new FileInputStream(cfg);
104                         Document doc = XmlUtil.getDocument(is);
105                         
106                         Element root = doc.getRootElement();
107                         
108                         @SuppressWarnings("unchecked")
109                         List<Element> children = root.getChildren();
110                         
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)){
116                                         continue;
117                                 }
118                                 crontabVo.setType(type);
119                                 if("ems-alarm".equalsIgnoreCase(type)){
120                                         boolean iscollect =  Boolean.parseBoolean(child.getAttributeValue("iscollect"));
121                                         if(iscollect){
122                                                 crontabVo.setIscollect(iscollect);
123                                         }else{
124                                                 continue;
125                                         }
126                                         
127                                         crontabVo.setRead_timeout(child.getChildText("readtimeout"));
128                                 }else{
129                                         String crontab = child.getAttributeValue("crontab");
130                                         if(!StringUtil.isBank(type) && !StringUtil.isBank(crontab)){
131                                                 crontabVo.setCrontab(crontab);
132                                         }else{
133                                                 continue;
134                                         }
135                                         crontabVo.setMatch(child.getChildText("match"));
136                                         crontabVo.setGranularity(child.getChildText("granularity"));
137                                 }
138                                 tmpcache.put(type, crontabVo);
139                         }
140                         
141                 } catch (Exception e) {
142                         log.error("load crontab.xml is error "+StringUtil.getStackTrace(e));
143                 }finally{
144                         tmpcache.clear();
145                         try {
146                                 if(is != null){
147                                         is.close();
148                                         is = null;
149                                 }
150                         } catch (Exception e2) {
151                         }
152                         cfg = null;
153                 }
154                 return tmpcache;
155         }
156
157
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);
164                 return;
165             }
166            
167         
168             InputStream is = null;
169             Map<String, EMSInfo> tmpcache = new HashMap<String, EMSInfo>();
170             
171             try {
172                         is = new FileInputStream(cfg);
173                         Document doc = XmlUtil.getDocument(is);
174                         
175                         Element root = doc.getRootElement();
176                         
177                         @SuppressWarnings("unchecked")
178                         List<Element> children = root.getChildren();
179                         
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)){
185                                         continue;
186                                 }
187                                 emsInfo.setName(name);
188                                 
189 //                              tmpcache.put(name, emsInfo);
190                                 
191                                 @SuppressWarnings("unchecked")
192                                 List<Element> collectList = child.getChildren();
193                                 for(Element collect : collectList){
194                                         
195                                         CollectVo collectVo = new CollectVo();
196                                         
197                                         String type = collect.getAttributeValue("type");
198                                         if("alarm".equalsIgnoreCase(type)){
199                                                 boolean iscollect =  Boolean.parseBoolean(collect.getAttributeValue("iscollect"));
200                                                 if(iscollect){
201                                                         collectVo.setIscollect(iscollect);
202                                                 }else{
203                                                         continue;
204                                                 }
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"));
211                                         }else{
212                                                 String crontab = collect.getAttributeValue("crontab");
213                                                 if(!StringUtil.isBank(type) && !StringUtil.isBank(crontab)){
214                                                         collectVo.setType(type);
215                                                         collectVo.setCrontab(crontab);
216                                                 }else{
217                                                         continue;
218                                                 }
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"));
228                                         }
229                                 
230                                         emsInfo.putCollectMap(type, collectVo);
231                                 }
232                                 tmpcache.put(name, emsInfo);
233                         }
234                         emsInfoCache.putAll(tmpcache);
235                         
236                         File file = new File(config);
237                     if(!file.exists() || !file.isFile()){
238                         log.error("cacheFilePath " + config+" not exist or is not File");
239                         return;
240                     }
241                     InputStream in  = null;
242                         try{
243                                 properties = new Properties();
244                         in = new FileInputStream(file);
245                         properties.load(in);
246                         }catch (Exception e) {
247                                 e.printStackTrace();
248                         }
249                 } catch (Exception e) {
250                         log.error("load EMSInfo.xml is error "+StringUtil.getStackTrace(e));
251                 }finally{
252                         tmpcache.clear();
253                         try {
254                                 if(is != null){
255                                         is.close();
256                                         is = null;
257                                 }
258                         } catch (Exception e2) {
259                         }
260                         cfg = null;
261                 }
262         }
263         
264         
265         public static synchronized List<EMSInfo> getAllEMSInfos(){
266                 List<EMSInfo> list = new ArrayList<EMSInfo>();
267                 for(EMSInfo emsinfo :emsInfoCache.values()){
268                         list.add(emsinfo);
269                 }
270                 return list;
271         }
272         
273         public static synchronized EMSInfo getEMSInfoByName(String emsName){
274                 EMSInfo emsInfo= emsInfoCache.get(emsName);
275                 return emsInfo;
276         }
277         
278         public  static synchronized Properties getProperties() {
279                 return properties;
280         }
281         
282         class ReceiveSource extends Thread{
283                 long timeStamp = System.currentTimeMillis();
284                 
285                 public void run() {
286                         while(true){
287                                 
288                                 try {
289                                         if(System.currentTimeMillis() - timeStamp > Constant.ONEMINUTE){
290                                                 timeStamp = System.currentTimeMillis();
291                                                 log.debug("ReceiveSource run");
292                                         }
293                                         //get emsId list
294                                         List<String> emsIds = this.getEmsIdList();
295                                         if(emsIds.size() > 0){
296                                                 emsIdList.clear();
297                                                 emsIdList.addAll(emsIds);
298                                         }
299                                         
300                                         for(String emsId : emsIdList){
301                                                 //get emsInfo by emsId 
302                                                 Map<String, EMSInfo> emsInfoMap = this.getEmsInfo(emsId);
303                                                 
304                                                 emsInfoCache.putAll(emsInfoMap);
305                                         }
306                                         
307                                         
308                                         //
309                                         if(emsInfoCache.size() > 0){
310                                                 Thread.sleep(5*60*1000);
311                                         }else{
312                                                 Thread.sleep(60*1000);
313                                         }
314                                 } catch (Exception e) {
315                                         try {
316                                                 Thread.sleep(60*1000);
317                                         } catch (InterruptedException e1) {
318                                                 e1.printStackTrace();
319                                         }
320                                         log.error("ReceiveSource exception",e);
321                                 }
322                         }
323                 }
324
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");
329                         //set emsId to url
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);
335                         
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);
342                         while(it.hasNext()){
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"));
356                                         
357                                                 collectVo.setRemotepath(collect.getString("remote-path"));
358                                                 collectVo.setMatch(crontabVo.getMatch());
359                                                 collectVo.setPassive(collect.getString("passive"));
360                                                 collectVo.setGranularity(crontabVo.getGranularity());
361                                         }
362                                         
363                                         
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"));
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                                         }
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());
389                                         }else{
390                                                 log.error("emsCrontab.get(system_type) result crontabVo is null" );
391                                         }
392                                         
393                                         
394                                 }else{
395                                         log.error("ems system-type ="+system_type+" ");
396                                 }
397                                 
398                                 emsInfo.putCollectMap(system_type, collectVo);
399                         }
400                         return tmpcache;
401                 }
402
403                 private List<String> getEmsIdList() {
404                         List<String> emsIds = new ArrayList<String>();
405                         //http
406                         String msbAddress = properties.getProperty("msbAddress");
407                         String url = properties.getProperty("esr_ems_listUrl");
408                         String getemsListUrl = "http://"+msbAddress+url;
409                         
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();
415                         while(it.hasNext()){
416                                 Object obj = it.next();
417                                 JSONObject emsId = (JSONObject)obj;
418                                 String emsIdStr = (String)emsId.get("ems-id");
419                                 emsIds.add(emsIdStr);
420                         }
421                         return emsIds;
422                 }
423         }
424 }