Updated to Java 11
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / msb / EngineInsQueryTool.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  * <p>
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  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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 lombok.extern.slf4j.Slf4j;
20 import org.apache.http.HttpResponse;
21 import org.apache.http.client.methods.HttpGet;
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 EngineInsQueryTool {
38
39     private String url;
40
41     @PostConstruct
42     public void init() {
43         String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
44         url = String.format("http://%s:%s/api/microservices/v1/services/holmes-engine-mgmt/version/v1",
45                 msbAddrInfo[0], msbAddrInfo[1]);
46     }
47
48     public List<String> getInstanceList() throws Exception {
49         String response;
50         HttpGet httpGet = new HttpGet(url);
51         try (CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT)) {
52             HttpResponse httpResponse = HttpsUtils.get(httpGet, new HashMap<>(), httpClient);
53             response = HttpsUtils.extractResponseEntity(httpResponse);
54         } catch (Exception e) {
55             throw e;
56         } finally {
57             httpGet.releaseConnection();
58
59         }
60         ServiceEntity service = GsonUtil.jsonToBean(response, ServiceEntity.class);
61         List<ServiceNode4Query> nodesList = service.getNodes();
62         List<String> ipList = new ArrayList<>();
63         for (ServiceNode4Query node : nodesList) {
64             ipList.add(node.getIp());
65         }
66         return ipList;
67
68     }
69
70 }