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;
22 import lombok.extern.slf4j.Slf4j;
23 import org.apache.http.HttpResponse;
24 import org.apache.http.client.methods.HttpGet;
25 import org.apache.http.client.methods.HttpRequestBase;
26 import org.apache.http.impl.client.CloseableHttpClient;
27 import org.jvnet.hk2.annotations.Service;
28 import org.onap.holmes.common.aai.config.AaiConfig;
29 import org.onap.holmes.common.aai.entity.VmEntity;
30 import org.onap.holmes.common.aai.entity.VnfEntity;
31 import org.onap.holmes.common.config.MicroServiceConfig;
32 import org.onap.holmes.common.exception.CorrelationException;
33 import org.onap.holmes.common.utils.HttpsUtils;
37 public class AaiQuery {
40 private AaiResponseUtil aaiResponseUtil;
42 public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
43 String response = getVnfDataResponse(vnfId, vnfName);
45 return aaiResponseUtil.convertJsonToVnfEntity(response);
46 } catch (Exception e) {
47 throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
51 public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
52 String url = getVmUrl(vserverId, vserverName);
53 String response = getResponse(url);
55 return aaiResponseUtil.convertJsonToVmEntity(response);
56 } catch (Exception e) {
57 throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
61 private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
63 String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
64 return getBaseUrl("") + resourceLinkUrl;
67 private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
68 String response = getResourceLinksResponse(vserverId, vserverName);
69 List linkList = aaiResponseUtil.convertJsonToVmResourceLink(response);
70 if (!linkList.isEmpty()) {
71 return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
76 private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
77 String url = getBaseUrl(AaiConfig.AaiConsts.AAI_VM_ADDR + "vserver-id:EQUALS:" + vserverId);
78 String response = getResponse(url);
79 if ("".equals(response) || "{}".equals(response)) {
80 url = getBaseUrl(AaiConfig.AaiConsts.AAI_VM_ADDR + "vserver-name:EQUALS:" + vserverName);
81 response = getResponse(url);
86 private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
87 String url = getBaseUrl(AaiConfig.AaiConsts.AAI_VNF_ADDR + "/" + vnfId);
88 String response = getResponse(url);
89 if ("".equals(response) || "{}".equals(response)) {
90 url = getBaseUrl(AaiConfig.AaiConsts.AAI_VNF_ADDR + "?vnf-name=" + vnfName);
91 response = getResponse(url);
96 private String getBaseUrl(String suffixUrl) {
97 return "https://aai.onap" + suffixUrl;
100 private String getMsbSuffixAddr(String suffixUrl) {
101 if (suffixUrl.length() <= 0) {
104 String[] addrSplits = suffixUrl.substring(1).split("/");
105 String[] conv = addrSplits[2].split("-");
106 addrSplits[2] = conv[0];
107 if (conv.length > 1) {
108 for (int i = 1; i < conv.length; i++) {
109 addrSplits[2] = addrSplits[2] + conv[i].substring(0, 1).toUpperCase() + conv[i]
113 String ret = addrSplits[1];
114 addrSplits[1] = addrSplits[0] + "-" + addrSplits[2];
116 addrSplits[0] = "api";
117 StringBuilder stringBuffer = new StringBuilder();
118 for (String split : addrSplits) {
119 stringBuffer.append("/" + split);
121 return stringBuffer.toString();
124 private String getResponse(String url) throws CorrelationException {
126 CloseableHttpClient httpClient = null;
127 HttpGet httpGet = new HttpGet(url);
129 httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
130 HttpResponse httpResponse = HttpsUtils.get(httpGet, getHeaders(), httpClient);
131 response = HttpsUtils.extractResponseEntity(httpResponse);
132 } catch (Exception e) {
133 throw new CorrelationException("Failed to get data from aai", e);
135 httpGet.releaseConnection();
136 if (httpClient != null) {
139 } catch (IOException e) {
140 log.warn("Failed to close http client!");
147 private Map getHeaders() {
148 Map<String, String> headers = new HashMap<>();
149 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
150 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
151 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
152 headers.put("Accept", "application/json");