Released Version 1.4.7
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / AaiQuery.java
1 /**
2  * Copyright 2017 - 2021 ZTE Corporation.
3  * <p>
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
6  * <p>
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * <p>
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
12  * the License.
13  */
14 package org.onap.holmes.common.aai;
15
16 import lombok.extern.slf4j.Slf4j;
17 import org.onap.holmes.common.aai.config.AaiConfig;
18 import org.onap.holmes.common.aai.entity.VmEntity;
19 import org.onap.holmes.common.aai.entity.VnfEntity;
20 import org.onap.holmes.common.exception.CorrelationException;
21 import org.onap.holmes.common.utils.JerseyClient;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.stereotype.Service;
24
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 @Service
30 @Slf4j
31 public class AaiQuery {
32
33     @Autowired
34     private AaiResponseUtil aaiResponseUtil;
35
36     public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
37         String response = getVnfDataResponse(vnfId, vnfName);
38         try {
39             return aaiResponseUtil.convertJsonToVnfEntity(response);
40         } catch (Exception e) {
41             throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
42         }
43     }
44
45     public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
46         String url = getVmUrl(vserverId, vserverName);
47         String response = getResponse(url);
48         try {
49             return aaiResponseUtil.convertJsonToVmEntity(response);
50         } catch (Exception e) {
51             throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
52         }
53     }
54
55     private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
56         String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
57         return getBaseUrl("") + resourceLinkUrl;
58     }
59
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();
65         }
66         return "";
67     }
68
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);
75         }
76         return response;
77     }
78
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);
85         }
86         return response;
87     }
88
89     private String getBaseUrl(String suffixUrl) {
90         return "https://aai.onap:8443" + suffixUrl;
91     }
92
93     private String getResponse(String url) throws CorrelationException {
94         try {
95             return JerseyClient.newInstance().headers(getHeaders()).get(url);
96         } catch (Exception e) {
97             throw new CorrelationException("Failed to get data from aai", e);
98         }
99     }
100
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");
107         return headers;
108     }
109 }