992785f387eef4a2e6266e4d43852067f667c3ff
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / msb / EngineIpList.java
1 /**
2  * Copyright 2017 ZTE Corporation.
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.holmes.rulemgt.msb;
17
18
19 import java.io.IOException;
20 import lombok.extern.slf4j.Slf4j;
21 import org.apache.http.HttpResponse;
22 import org.apache.http.impl.client.CloseableHttpClient;
23 import org.jvnet.hk2.annotations.Service;
24 import org.onap.holmes.common.api.entity.ServiceEntity;
25 import org.onap.holmes.common.api.entity.ServiceNode4Query;
26 import org.onap.holmes.common.config.MicroServiceConfig;
27 import org.onap.holmes.common.utils.GsonUtil;
28 import org.onap.holmes.common.utils.HttpsUtils;
29
30 import javax.annotation.PostConstruct;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34
35 @Service
36 @Slf4j
37 public class EngineIpList {
38
39     private String[] msbAddrInfo;
40     private String ip;
41     private String port;
42     private String url;
43
44     @PostConstruct
45     public void init(){
46         msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
47         ip = msbAddrInfo[0];
48         port = msbAddrInfo[1];
49         url = "http://" + ip + ":" + port + "/api/microservices/v1/services/holmes-engine-mgmt/version/v1" ;
50     }
51
52     public List<String> getServiceCount()throws Exception{
53         String response;
54         CloseableHttpClient httpClient = null;
55         try {
56             httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
57             HttpResponse httpResponse = HttpsUtils
58                     .get(url, new HashMap<>(), httpClient);
59             response = HttpsUtils.extractResponseEntity(httpResponse);
60         } catch (Exception e) {
61             throw e;
62         } finally {
63             if (httpClient != null) {
64                 try {
65                     httpClient.close();
66                 } catch (IOException e) {
67                     log.warn("Failed to close http client!");
68                 }
69             }
70         }
71         ServiceEntity service = GsonUtil.jsonToBean(response, ServiceEntity.class);
72         List<ServiceNode4Query> nodesList = service.getNodes();
73         List<String> ipList = new ArrayList<>();
74         for(ServiceNode4Query node : nodesList){
75             ipList.add(node.getIp());
76         }
77         return ipList;
78
79     }
80
81 }