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