fix https bug
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / AaiQuery.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.holmes.common.aai;
15
16 import java.io.IOException;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import javax.inject.Inject;
21 import lombok.extern.slf4j.Slf4j;
22 import org.apache.http.HttpResponse;
23 import org.apache.http.impl.client.CloseableHttpClient;
24 import org.jvnet.hk2.annotations.Service;
25 import org.onap.holmes.common.aai.config.AaiConfig;
26 import org.onap.holmes.common.aai.entity.VmEntity;
27 import org.onap.holmes.common.aai.entity.VnfEntity;
28 import org.onap.holmes.common.config.MicroServiceConfig;
29 import org.onap.holmes.common.exception.CorrelationException;
30 import org.onap.holmes.common.utils.HttpsUtils;
31
32 @Service
33 @Slf4j
34 public class AaiQuery {
35
36     @Inject
37     private AaiResponseUtil aaiResponseUtil;
38
39     public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
40         String response = getVnfDataResponse(vnfId, vnfName);
41         try {
42             return aaiResponseUtil.convertJsonToVnfEntity(response);
43         } catch (Exception e) {
44             throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
45         }
46     }
47
48     public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
49         String url = getVmUrl(vserverId, vserverName);
50         String response = getResponse(url);
51         try {
52             return aaiResponseUtil.convertJsonToVmEntity(response);
53         } catch (Exception e) {
54             throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
55         }
56     }
57
58     private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
59         String url = "";
60         String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
61         String baseUrl = getBaseUrl("");
62         if (baseUrl.startsWith("http")) {
63             url = baseUrl + getMsbSuffixAddr(resourceLinkUrl);
64         } else {
65             url = baseUrl + resourceLinkUrl;
66         }
67         return url;
68     }
69
70     private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
71         String response = getResourceLinksResponse(vserverId, vserverName);
72         List linkList = aaiResponseUtil.convertJsonToVmResourceLink(response);
73         if (linkList.size() != 0) {
74             return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
75         }
76         return  "";
77     }
78
79     private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
80         String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-id:EQUALS:" + vserverId);
81         String response = getResponse(url);
82         if ("".equals(response) || "{}".equals(response)) {
83             url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-name:EQUALS:" + vserverName);
84             response = getResponse(url);
85         }
86         return response;
87     }
88
89     private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
90         String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR)+  "/" + vnfId);
91         String response = getResponse(url);
92         if ("".equals(response) || "{}".equals(response)) {
93             url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR) + "vnf-name=" + vnfName);
94             response = getResponse(url);
95         }
96         return response;
97     }
98
99     private String getBaseUrl(String suffixUrl) {
100         String url = "";
101         try {
102             String[] msbUrl = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":");
103             url = msbUrl[0] + ":" + msbUrl[1] + suffixUrl;
104         } catch (Exception e) {
105             log.info("Failed to get msb address");
106         }
107         if ("".equals(url)) {
108             try {
109                 url = "https://" + MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config").replace("http://", "")
110                         + suffixUrl;
111             } catch (Exception e) {
112                 log.info("Failed to get the address of A&AI.", e);
113             }
114         }
115         return url;
116     }
117
118     private String getMsbSuffixAddr(String suffixUrl) {
119         if (suffixUrl.length() <= 0) {
120             return "";
121         }
122         String[] addrSplits = suffixUrl.substring(1).split("/");
123         String[] conv = addrSplits[2].split("-");
124         addrSplits[2] = conv[0];
125         if (conv.length > 1) {
126             for(int i = 1; i < conv.length; i++) {
127                 addrSplits[2] = addrSplits[2] + conv[i].substring(0, 1).toUpperCase() + conv[i]
128                         .substring(1);
129             }
130         }
131         String ret = addrSplits[1];
132         addrSplits[1] = addrSplits[0] + "-" + addrSplits[2];
133         addrSplits[2] = ret;
134         addrSplits[0] = "api";
135         StringBuffer stringBuffer = new StringBuffer();
136         for (String split : addrSplits) {
137             stringBuffer.append("/" + split);
138         }
139         return stringBuffer.toString();
140     }
141
142     private String getResponse(String url) throws CorrelationException {
143         String response;
144         CloseableHttpClient httpClient = null;
145         try {
146             httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
147             HttpResponse httpResponse = HttpsUtils.get(url, getHeaders(), httpClient);
148             response = HttpsUtils.extractResponseEntity(httpResponse);
149         } catch (Exception e) {
150             throw new CorrelationException("Failed to get data from aai", e);
151         } finally {
152             if (httpClient != null) {
153                 try {
154                     httpClient.close();
155                 } catch (IOException e) {
156                     log.warn("Failed to close http client!");
157                 }
158             }
159         }
160         return response;
161     }
162
163     private Map getHeaders() {
164         Map<String, String> headers = new HashMap<>();
165         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
166         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
167         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
168         headers.put("Accept", "application/json");
169         return headers;
170     }
171 }