summaryrefslogtreecommitdiffstats
path: root/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/configmgr/ConfigurationManager.java
blob: 5b7a2c17f9a5afda92265527abb66e26b62b62b7 (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
/*
 * 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 com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.annotations.VisibleForTesting;
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 java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


public class ConfigurationManager extends DriverThread {
    public final static String CONFIG_PROPERTIES_LOCATION = Constant.SYS_CFG + "config.properties";
    protected static Log log = LogFactory.getLog(ConfigurationManager.class);
    /**
     * ESM Cache
     */
    @VisibleForTesting
    static Map<String, EMSInfo> emsInfoCache = new ConcurrentHashMap<String, EMSInfo>();
    @VisibleForTesting
    static Properties properties = null;
    private static Map<String, CrontabVo> emsCrontab = new ConcurrentHashMap<String, CrontabVo>();
    private static List<String> emsIdList = new ArrayList<String>();

    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;
    }

    @Override
    public void dispose() {

        File file = new File(CONFIG_PROPERTIES_LOCATION);
        if (!file.exists() || !file.isFile()) {
            log.error("cacheFilePath " + CONFIG_PROPERTIES_LOCATION + " not exist or is not File");
            return;
        }
        try(InputStream in = new FileInputStream(file)){
            properties = new Properties();
            properties.load(in);
            Map<String, CrontabVo> emsMap = readCorntab();

            emsCrontab.putAll(emsMap);

            new ReceiveSource().start();
        } catch (Exception e) {
            log.error("read [" + file.getAbsolutePath() + "]Exception :", e);
        } 
    }

    public 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;
        }
        Map<String, CrontabVo> tmpcache = new HashMap<String, CrontabVo>();

        try (
            InputStream 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 (Constant.COLLECT_TYPE_ALARM.equalsIgnoreCase(type)) {
                    boolean iscollect = Boolean.parseBoolean(child.getAttributeValue("iscollect"));
                    if (iscollect) {
                        crontabVo.setIscollect(iscollect);
                    } else {
                        continue;
                    }

                    crontabVo.setReadTimeout(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.toUpperCase(), crontabVo);
            }

        } catch (Exception e) {
            log.error("load crontab.xml is error " + StringUtil.getStackTrace(e));
        } 
        return tmpcache;
    }

    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(30 * 60 * 1000L);
                    } else {
                        Thread.sleep(60 * 1000L);
                    }
                } catch (Exception e) {
                    try {
                        Thread.sleep(60 * 1000L);
                    } catch (Exception e1) {
                        log.error("InterruptedException :" + StringUtil.getStackTrace(e1));
                    }
                    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
            emstUrl = 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 (Constant.COLLECT_TYPE_CM.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 {
                        log.error("emsCrontab.get(system_type) result crontabVo is null system_type=[" + system_type + "] emsCrontabMap=" + emsCrontab);
                    }


                } else if (Constant.COLLECT_TYPE_PM.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 {
                        log.error("emsCrontab.get(system_type) result crontabVo is null system_type=[" + system_type + "]");
                    }
                } else if (Constant.COLLECT_TYPE_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.setReadTimeout(crontabVo.getReadTimeout());
                    } else {
                        log.error("emsCrontab.get(system_type) result crontabVo is null system_type=[" + system_type + "]");
                    }


                } 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;
        }
    }
}