Added UT Codes
[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 java.io.IOException;
20
21 import lombok.extern.slf4j.Slf4j;
22 import org.apache.http.HttpResponse;
23 import org.apache.http.client.methods.HttpGet;
24 import org.apache.http.impl.client.CloseableHttpClient;
25 import org.jvnet.hk2.annotations.Service;
26 import org.onap.holmes.common.api.entity.ServiceEntity;
27 import org.onap.holmes.common.api.entity.ServiceNode4Query;
28 import org.onap.holmes.common.config.MicroServiceConfig;
29 import org.onap.holmes.common.utils.GsonUtil;
30 import org.onap.holmes.common.utils.HttpsUtils;
31
32 import javax.annotation.PostConstruct;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36
37 @Service
38 @Slf4j
39 public class EngineInsQueryTool {
40
41     private String url;
42
43     @PostConstruct
44     public void init() {
45         String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
46         url = String.format("http://%s:%s/api/microservices/v1/services/holmes-engine-mgmt/version/v1",
47                 msbAddrInfo[0], msbAddrInfo[1]);
48     }
49
50     public List<String> getInstanceList() throws Exception {
51                 String response;
52                 HttpGet httpGet = new HttpGet(url);
53                 try (CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT)) {
54                         HttpResponse httpResponse = HttpsUtils.get(httpGet, new HashMap<>(), httpClient);
55                         response = HttpsUtils.extractResponseEntity(httpResponse);
56                 } catch (Exception e) {
57                         throw e;
58                 } finally {
59                         httpGet.releaseConnection();
60
61                 }
62                 ServiceEntity service = GsonUtil.jsonToBean(response, ServiceEntity.class);
63                 List<ServiceNode4Query> nodesList = service.getNodes();
64                 List<String> ipList = new ArrayList<>();
65                 for (ServiceNode4Query node : nodesList) {
66                         ipList.add(node.getIp());
67                 }
68                 return ipList;
69
70     }
71
72 }