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.util.HashMap;
18 import java.util.stream.Stream;
19 import javax.inject.Inject;
20 import lombok.extern.slf4j.Slf4j;
21 import org.jvnet.hk2.annotations.Service;
22 import org.onap.holmes.common.aai.config.AaiConfig;
23 import org.onap.holmes.common.aai.entity.VmEntity;
24 import org.onap.holmes.common.aai.entity.VnfEntity;
25 import org.onap.holmes.common.config.MicroServiceConfig;
26 import org.onap.holmes.common.exception.CorrelationException;
27 import org.onap.holmes.common.utils.HttpsUtils;
31 public class AaiQuery {
34 private AaiResponseUtil aaiResponseUtil;
36 public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
37 String response = getVnfDataResponse(vnfId, vnfName);
39 return aaiResponseUtil.convertJsonToVnfEntity(response);
40 } catch (Exception e) {
41 throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
45 public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
46 String url = getVmUrl(vserverId, vserverName);
47 String response = getResponse(url);
49 return aaiResponseUtil.convertJsonToVmEntity(response);
50 } catch (Exception e) {
51 throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
55 private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
57 String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
58 String baseUrl = getBaseUrl("");
59 if (baseUrl.startsWith("http")) {
60 url = baseUrl + getMsbSuffixAddr(resourceLinkUrl);
62 url = baseUrl + resourceLinkUrl;
67 private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
68 String response = getResourceLinksResponse(vserverId, vserverName);
70 return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
71 } catch (Exception e) {
72 throw new CorrelationException("Failed to get aai resource link", e);
76 private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
77 String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR) + "vserver-id:EQUALS:" + vserverId);
78 String response = getResponse(url);
79 if ("".equals(response) || "{}".equals(response)) {
80 url = getBaseUrl(AaiConfig.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(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR)+ "vnf-id=" + vnfId);
88 String response = getResponse(url);
89 if ("".equals(response) || "{}".equals(response)) {
90 url = getBaseUrl(AaiConfig.AAI_VNF_ADDR + "vnf-name=" + vnfName);
91 response = getResponse(url);
96 private String getBaseUrl(String suffixUrl) {
99 url = MicroServiceConfig.getMsbServerAddr() + suffixUrl;
100 } catch (Exception e) {
101 log.info("Failed to get msb address");
103 if (url.equals("")) {
105 url = "https:\\\\" + MicroServiceConfig.getServiceAddrInfoFromCBS("aai_config")
107 } catch (Exception e) {
108 log.info("Failed to get aai address");
114 private String getMsbSuffixAddr(String suffixUrl) {
115 String[] addrSplits = suffixUrl.substring(1).split("/");
116 String ret = addrSplits[1];
117 addrSplits[1] = addrSplits[2];
119 StringBuffer stringBuffer = new StringBuffer();
120 for (String split : addrSplits) {
121 stringBuffer.append("/" + split);
123 return stringBuffer.toString();
126 private String getResponse(String url) throws CorrelationException {
127 String response = "";
129 response = HttpsUtils.get(url, getHeaders());
130 } catch (Exception e) {
131 throw new CorrelationException("Failed to get data from aai", e);
136 private Map getHeaders() {
137 Map<String, String> headers = new HashMap<>();
138 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
139 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
140 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
141 headers.put("Accept", "application/json");