Allocate Rule
[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 org.apache.http.HttpResponse;
20 import org.jvnet.hk2.annotations.Service;
21 import org.onap.holmes.common.api.entity.ServiceEntity;
22 import org.onap.holmes.common.api.entity.ServiceNode4Query;
23 import org.onap.holmes.common.config.MicroServiceConfig;
24 import org.onap.holmes.common.utils.GsonUtil;
25 import org.onap.holmes.common.utils.HttpsUtils;
26
27 import javax.annotation.PostConstruct;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31
32 @Service
33 public class EngineIpList {
34
35     private String[] msbAddrInfo;
36     private String ip;
37     private String port;
38     private String url;
39
40     @PostConstruct
41     public void init(){
42         msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
43         ip = msbAddrInfo[0];
44         port = msbAddrInfo[1];
45         url = "http://" + ip + ":" + port + "/api/microservices/v1/services/holmes-engine-mgmt/version/v1" ;
46     }
47
48     public List<String> getServiceCount()throws Exception{
49         String response;
50         try {
51             HttpResponse httpResponse = HttpsUtils
52                     .get(url, new HashMap<>());
53             response = HttpsUtils.extractResponseEntity(httpResponse);
54         } catch (Exception e) {
55             throw e;
56         }
57         ServiceEntity service = GsonUtil.jsonToBean(response, ServiceEntity.class);
58         List<ServiceNode4Query> nodesList = service.getNodes();
59         List<String> ipList = new ArrayList<>();
60         for(ServiceNode4Query node : nodesList){
61             ipList.add(node.getIp());
62         }
63         return ipList;
64
65     }
66
67 }