Modify groupId for ems driver
[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.jdom.Document;
30 import org.jdom.Element;
31 import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
32 import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;
33 import org.onap.vfc.nfvo.emsdriver.commons.model.EMSInfo;
34 import org.onap.vfc.nfvo.emsdriver.commons.utils.DriverThread;
35 import org.onap.vfc.nfvo.emsdriver.commons.utils.StringUtil;
36 import org.onap.vfc.nfvo.emsdriver.commons.utils.XmlUtil;
37
38
39 public class ConfigurationManager extends DriverThread{
40
41         /**
42          * ESM Cache
43          */
44         private static Map<String, EMSInfo> emsInfoCache = new ConcurrentHashMap<String, EMSInfo>();
45         
46         private static Properties properties = null;
47         
48         private final static String  ftpconfig = Constant.SYS_CFG + "ftpconfig.properties";
49         
50         @Override
51         @SuppressWarnings("unchecked")
52         public void dispose() {
53                 String path = Constant.SYS_CFG + "EMSInfo.xml";
54                 File cfg = new File(path);
55                 this.log.debug("start loading " + path);
56             if(!cfg.exists() || !cfg.isFile()){
57                 this.log.debug("not exists " + path);
58                 return;
59             }
60             
61             InputStream is = null;
62             Map<String, EMSInfo> tmpcache = new HashMap<String, EMSInfo>();
63             
64             try {
65                         is = new FileInputStream(cfg);
66                         Document doc = XmlUtil.getDocument(is);
67                         
68                         Element root = doc.getRootElement();
69                         
70                         List<Element> children = root.getChildren();
71                         
72                         for(Iterator<Element> it = children.iterator();it.hasNext();){
73                                 EMSInfo emsInfo = new EMSInfo();
74                                 Element child = it.next();
75                                 String name = child.getAttributeValue("name");
76                                 if(StringUtil.isBank(name)){
77                                         continue;
78                                 }
79                                 emsInfo.setName(name);
80                                 
81                                 tmpcache.put(name, emsInfo);
82                                 
83                                 List<Element> collectList = child.getChildren();
84                                 for(Element collect : collectList){
85                                         
86                                         CollectVo collectVo = new CollectVo();
87                                         
88                                         String type = collect.getAttributeValue("type");
89                                         if("alarm".equalsIgnoreCase(type)){
90                                                 boolean iscollect =  Boolean.parseBoolean(collect.getAttributeValue("iscollect"));
91                                                 if(iscollect){
92                                                         collectVo.setIscollect(iscollect);
93                                                 }else{
94                                                         continue;
95                                                 }
96                                                 collectVo.setType(type);
97                                                 collectVo.setIP(collect.getChildText("ip"));
98                                                 collectVo.setPort(collect.getChildText("port"));
99                                                 collectVo.setUser(collect.getChildText("user"));
100                                                 collectVo.setPassword(collect.getChildText("password"));
101                                                 collectVo.setRead_timeout(collect.getChildText("readtimeout"));
102                                         }else{
103                                                 String crontab = collect.getAttributeValue("crontab");
104                                                 if(!StringUtil.isBank(type) && !StringUtil.isBank(crontab)){
105                                                         collectVo.setType(type);
106                                                         collectVo.setCrontab(crontab);
107                                                 }else{
108                                                         continue;
109                                                 }
110                                                 collectVo.setIP(collect.getChildText("ip"));
111                                                 collectVo.setPort(collect.getChildText("port"));
112                                                 collectVo.setUser(collect.getChildText("user"));
113                                                 collectVo.setPassword(collect.getChildText("password"));
114                                                 collectVo.setRemotepath(collect.getChildText("remotepath"));
115                                                 collectVo.setMatch(collect.getChildText("match"));
116                                                 collectVo.setPassive(collect.getChildText("passive"));
117                                                 collectVo.setFtptype(collect.getChildText("ftptype"));
118                                                 collectVo.setGranularity(collect.getChildText("granularity"));
119                                         }
120                                 
121                                         emsInfo.putCollectMap(type, collectVo);
122                                 }
123                                 tmpcache.put(name, emsInfo);
124                         }
125                         emsInfoCache.putAll(tmpcache);
126                         
127                 } catch (Exception e) {
128                         log.error("load EMSInfo.xml is error "+StringUtil.getStackTrace(e));
129                 }finally{
130                         tmpcache.clear();
131                         try {
132                                 if(is != null){
133                                         is.close();
134                                         is = null;
135                                 }
136                         } catch (Exception e2) {
137                         }
138                         cfg = null;
139                 }
140                 
141                 
142                 //this.log.debug("start loading " + cacheFilePath);
143                 File file = new File(ftpconfig);
144             if(!file.exists() || !file.isFile()){
145                 this.log.error("cacheFilePath " + ftpconfig+"not exist or is not File");
146                 return;
147             }
148             InputStream in  = null;
149                 try{
150                         properties = new Properties();
151                 in = new FileInputStream(file);
152                 properties.load(in);
153                
154                 }catch(Exception e) {
155                         log.error("read ["+file.getAbsolutePath()+"]Exception :",e);
156                 }finally {
157                         if(in != null) {
158                                 try {
159                                         in.close();
160                                 } catch (Exception e) {
161                                 }
162                         }
163                 }
164         }
165         
166         public static synchronized List<EMSInfo> getAllEMSInfos(){
167                 List<EMSInfo> list = new ArrayList<EMSInfo>();
168                 for(EMSInfo emsinfo :emsInfoCache.values()){
169                         list.add(emsinfo);
170                 }
171                 return list;
172         }
173         
174         public static synchronized EMSInfo getEMSInfoByName(String emsName){
175                 EMSInfo emsInfo= emsInfoCache.get(emsName);
176                 return emsInfo;
177         }
178         
179         public  static synchronized Properties getProperties() {
180                 return properties;
181         }
182         
183
184 }