Merge "change to java 8 lambda"
[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                 HttpGet httpGet = new HttpGet(url);
56                 try (CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT)) {
57                         HttpResponse httpResponse = HttpsUtils.get(httpGet, new HashMap<>(), httpClient);
58                         response = HttpsUtils.extractResponseEntity(httpResponse);
59                 } catch (Exception e) {
60                         throw e;
61                 } finally {
62                         httpGet.releaseConnection();
63
64                 }
65                 ServiceEntity service = GsonUtil.jsonToBean(response, ServiceEntity.class);
66                 List<ServiceNode4Query> nodesList = service.getNodes();
67                 List<String> ipList = new ArrayList<>();
68                 for (ServiceNode4Query node : nodesList) {
69                         ipList.add(node.getIp());
70                 }
71                 return ipList;
72
73     }
74
75 }