2 * Copyright 2017 ZTE Corporation.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
14 package org.onap.holmes.common.aai;
16 import java.io.IOException;
17 import java.util.HashMap;
18 import java.util.List;
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;
34 public class AaiQuery {
37 private AaiResponseUtil aaiResponseUtil;
39 public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
40 String response = getVnfDataResponse(vnfId, vnfName);
42 return aaiResponseUtil.convertJsonToVnfEntity(response);
43 } catch (Exception e) {
44 throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
48 public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
49 String url = getVmUrl(vserverId, vserverName);
50 String response = getResponse(url);
52 return aaiResponseUtil.convertJsonToVmEntity(response);
53 } catch (Exception e) {
54 throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
58 private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
60 String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
61 String baseUrl = getBaseUrl("");
62 if (baseUrl.startsWith("http")) {
63 url = baseUrl + getMsbSuffixAddr(resourceLinkUrl);
65 url = baseUrl + resourceLinkUrl;
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();
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);
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);
99 private String getBaseUrl(String suffixUrl) {
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");
107 if ("".equals(url)) {
109 url = "https://" + MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config").replace("http://", "")
111 } catch (Exception e) {
112 log.info("Failed to get the address of A&AI.", e);
118 private String getMsbSuffixAddr(String suffixUrl) {
119 if (suffixUrl.length() <= 0) {
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]
131 String ret = addrSplits[1];
132 addrSplits[1] = addrSplits[0] + "-" + addrSplits[2];
134 addrSplits[0] = "api";
135 StringBuffer stringBuffer = new StringBuffer();
136 for (String split : addrSplits) {
137 stringBuffer.append("/" + split);
139 return stringBuffer.toString();
142 private String getResponse(String url) throws CorrelationException {
144 CloseableHttpClient httpClient = null;
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);
152 if (httpClient != null) {
155 } catch (IOException e) {
156 log.warn("Failed to close http client!");
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");