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.client.methods.HttpGet;
24 import org.apache.http.client.methods.HttpRequestBase;
25 import org.apache.http.impl.client.CloseableHttpClient;
26 import org.jvnet.hk2.annotations.Service;
27 import org.onap.holmes.common.aai.config.AaiConfig;
28 import org.onap.holmes.common.aai.entity.VmEntity;
29 import org.onap.holmes.common.aai.entity.VnfEntity;
30 import org.onap.holmes.common.config.MicroServiceConfig;
31 import org.onap.holmes.common.exception.CorrelationException;
32 import org.onap.holmes.common.utils.HttpsUtils;
36 public class AaiQuery {
39 private AaiResponseUtil aaiResponseUtil;
41 public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
42 String response = getVnfDataResponse(vnfId, vnfName);
44 return aaiResponseUtil.convertJsonToVnfEntity(response);
45 } catch (Exception e) {
46 throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
50 public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
51 String url = getVmUrl(vserverId, vserverName);
52 String response = getResponse(url);
54 return aaiResponseUtil.convertJsonToVmEntity(response);
55 } catch (Exception e) {
56 throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
60 private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
62 String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
63 String baseUrl = getBaseUrl("");
64 if (baseUrl.startsWith("http")) {
65 url = baseUrl + getMsbSuffixAddr(resourceLinkUrl);
67 url = baseUrl + resourceLinkUrl;
72 private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
73 String response = getResourceLinksResponse(vserverId, vserverName);
74 List linkList = aaiResponseUtil.convertJsonToVmResourceLink(response);
75 if (!linkList.isEmpty()) {
76 return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
81 private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
82 String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AaiConsts.AAI_VM_ADDR) + "vserver-id:EQUALS:" + vserverId);
83 String response = getResponse(url);
84 if ("".equals(response) || "{}".equals(response)) {
85 url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AaiConsts.AAI_VM_ADDR) + "vserver-name:EQUALS:" + vserverName);
86 response = getResponse(url);
91 private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
92 String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AaiConsts.AAI_VNF_ADDR)+ "/" + vnfId);
93 String response = getResponse(url);
94 if ("".equals(response) || "{}".equals(response)) {
95 url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AaiConsts.AAI_VNF_ADDR) + "vnf-name=" + vnfName);
96 response = getResponse(url);
101 private String getBaseUrl(String suffixUrl) {
104 String[] msbUrl = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":");
105 url = msbUrl[0] + ":" + msbUrl[1] + suffixUrl;
106 } catch (Exception e) {
107 log.info("Failed to get msb address");
109 if ("".equals(url)) {
111 url = "https://" + MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config").replace("http://", "")
113 } catch (Exception e) {
114 log.info("Failed to get the address of A&AI.", e);
120 private String getMsbSuffixAddr(String suffixUrl) {
121 if (suffixUrl.length() <= 0) {
124 String[] addrSplits = suffixUrl.substring(1).split("/");
125 String[] conv = addrSplits[2].split("-");
126 addrSplits[2] = conv[0];
127 if (conv.length > 1) {
128 for(int i = 1; i < conv.length; i++) {
129 addrSplits[2] = addrSplits[2] + conv[i].substring(0, 1).toUpperCase() + conv[i]
133 String ret = addrSplits[1];
134 addrSplits[1] = addrSplits[0] + "-" + addrSplits[2];
136 addrSplits[0] = "api";
137 StringBuilder stringBuffer = new StringBuilder();
138 for (String split : addrSplits) {
139 stringBuffer.append("/" + split);
141 return stringBuffer.toString();
144 private String getResponse(String url) throws CorrelationException {
146 CloseableHttpClient httpClient = null;
147 HttpGet httpGet = new HttpGet(url);
149 httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
150 HttpResponse httpResponse = HttpsUtils.get(httpGet, getHeaders(), httpClient);
151 response = HttpsUtils.extractResponseEntity(httpResponse);
152 } catch (Exception e) {
153 throw new CorrelationException("Failed to get data from aai", e);
155 httpGet.releaseConnection();
156 if (httpClient != null) {
159 } catch (IOException e) {
160 log.warn("Failed to close http client!");
167 private Map getHeaders() {
168 Map<String, String> headers = new HashMap<>();
169 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
170 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
171 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
172 headers.put("Accept", "application/json");