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.jvnet.hk2.annotations.Service;
18 import org.onap.holmes.common.aai.config.AaiConfig;
19 import org.onap.holmes.common.aai.entity.VmEntity;
20 import org.onap.holmes.common.aai.entity.VnfEntity;
21 import org.onap.holmes.common.exception.CorrelationException;
22 import org.onap.holmes.common.utils.JerseyClient;
24 import javax.inject.Inject;
25 import java.util.HashMap;
26 import java.util.List;
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 {
56 String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
57 return getBaseUrl("") + resourceLinkUrl;
60 private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
61 String response = getResourceLinksResponse(vserverId, vserverName);
62 List linkList = aaiResponseUtil.convertJsonToVmResourceLink(response);
63 if (!linkList.isEmpty()) {
64 return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
69 private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
70 String url = getBaseUrl(AaiConfig.AaiConsts.AAI_VM_ADDR + "vserver-id:EQUALS:" + vserverId);
71 String response = getResponse(url);
72 if ("".equals(response) || "{}".equals(response)) {
73 url = getBaseUrl(AaiConfig.AaiConsts.AAI_VM_ADDR + "vserver-name:EQUALS:" + vserverName);
74 response = getResponse(url);
79 private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
80 String url = getBaseUrl(AaiConfig.AaiConsts.AAI_VNF_ADDR + "/" + vnfId);
81 String response = getResponse(url);
82 if ("".equals(response) || "{}".equals(response)) {
83 url = getBaseUrl(AaiConfig.AaiConsts.AAI_VNF_ADDR + "?vnf-name=" + vnfName);
84 response = getResponse(url);
89 private String getBaseUrl(String suffixUrl) {
90 return "https://aai.onap:8443" + suffixUrl;
93 private String getResponse(String url) throws CorrelationException {
95 return new JerseyClient().headers(getHeaders()).get(url);
96 } catch (Exception e) {
97 throw new CorrelationException("Failed to get data from aai", e);
101 private Map getHeaders() {
102 Map<String, String> headers = new HashMap<>();
103 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
104 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
105 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
106 headers.put("Accept", "application/json");