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 lombok.extern.slf4j.Slf4j;
17 import org.apache.http.HttpResponse;
18 import org.apache.http.client.methods.HttpGet;
19 import org.apache.http.impl.client.CloseableHttpClient;
20 import org.jvnet.hk2.annotations.Service;
21 import org.onap.holmes.common.aai.config.AaiConfig;
22 import org.onap.holmes.common.aai.entity.VmEntity;
23 import org.onap.holmes.common.aai.entity.VnfEntity;
24 import org.onap.holmes.common.exception.CorrelationException;
25 import org.onap.holmes.common.utils.HttpsUtils;
27 import javax.inject.Inject;
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.List;
35 public class AaiQuery {
38 private AaiResponseUtil aaiResponseUtil;
40 public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
41 String response = getVnfDataResponse(vnfId, vnfName);
43 return aaiResponseUtil.convertJsonToVnfEntity(response);
44 } catch (Exception e) {
45 throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
49 public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
50 String url = getVmUrl(vserverId, vserverName);
51 String response = getResponse(url);
53 return aaiResponseUtil.convertJsonToVmEntity(response);
54 } catch (Exception e) {
55 throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
59 private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
60 String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
61 return getBaseUrl("") + resourceLinkUrl;
64 private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
65 String response = getResourceLinksResponse(vserverId, vserverName);
66 List linkList = aaiResponseUtil.convertJsonToVmResourceLink(response);
67 if (!linkList.isEmpty()) {
68 return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
73 private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
74 String url = getBaseUrl(AaiConfig.AaiConsts.AAI_VM_ADDR + "vserver-id:EQUALS:" + vserverId);
75 String response = getResponse(url);
76 if ("".equals(response) || "{}".equals(response)) {
77 url = getBaseUrl(AaiConfig.AaiConsts.AAI_VM_ADDR + "vserver-name:EQUALS:" + vserverName);
78 response = getResponse(url);
83 private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
84 String url = getBaseUrl(AaiConfig.AaiConsts.AAI_VNF_ADDR + "/" + vnfId);
85 String response = getResponse(url);
86 if ("".equals(response) || "{}".equals(response)) {
87 url = getBaseUrl(AaiConfig.AaiConsts.AAI_VNF_ADDR + "?vnf-name=" + vnfName);
88 response = getResponse(url);
93 private String getBaseUrl(String suffixUrl) {
94 return "https://aai.onap:8443" + suffixUrl;
97 private String getResponse(String url) throws CorrelationException {
99 CloseableHttpClient httpClient = null;
100 HttpGet httpGet = new HttpGet(url);
102 httpClient = HttpsUtils.getHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
103 HttpResponse httpResponse = HttpsUtils.get(httpGet, getHeaders(), httpClient);
104 response = HttpsUtils.extractResponseEntity(httpResponse);
105 } catch (Exception e) {
106 throw new CorrelationException("Failed to get data from aai", e);
108 httpGet.releaseConnection();
109 if (httpClient != null) {
112 } catch (IOException e) {
113 log.warn("Failed to close http client!");
120 private Map getHeaders() {
121 Map<String, String> headers = new HashMap<>();
122 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
123 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
124 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
125 headers.put("Accept", "application/json");