Change HTTP Requests into HTTPS Ones
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / AaiQuery.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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 java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import javax.inject.Inject;
20 import lombok.extern.slf4j.Slf4j;
21 import org.apache.http.HttpResponse;
22 import org.jvnet.hk2.annotations.Service;
23 import org.onap.holmes.common.aai.config.AaiConfig;
24 import org.onap.holmes.common.aai.entity.VmEntity;
25 import org.onap.holmes.common.aai.entity.VnfEntity;
26 import org.onap.holmes.common.config.MicroServiceConfig;
27 import org.onap.holmes.common.exception.CorrelationException;
28 import org.onap.holmes.common.utils.HttpsUtils;
29
30 @Service
31 @Slf4j
32 public class AaiQuery {
33
34     @Inject
35     private AaiResponseUtil aaiResponseUtil;
36
37     public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
38         String response = getVnfDataResponse(vnfId, vnfName);
39         try {
40             return aaiResponseUtil.convertJsonToVnfEntity(response);
41         } catch (Exception e) {
42             throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
43         }
44     }
45
46     public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
47         String url = getVmUrl(vserverId, vserverName);
48         String response = getResponse(url);
49         try {
50             return aaiResponseUtil.convertJsonToVmEntity(response);
51         } catch (Exception e) {
52             throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
53         }
54     }
55
56     private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
57         String url = "";
58         String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
59         String baseUrl = getBaseUrl("");
60         if (baseUrl.startsWith("http")) {
61             url = baseUrl + getMsbSuffixAddr(resourceLinkUrl);
62         } else {
63             url = baseUrl + resourceLinkUrl;
64         }
65         return url;
66     }
67
68     private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
69         String response = getResourceLinksResponse(vserverId, vserverName);
70         List linkList = aaiResponseUtil.convertJsonToVmResourceLink(response);
71         if (linkList.size() != 0) {
72             return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
73         }
74         return  "";
75     }
76
77     private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
78         String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-id:EQUALS:" + vserverId);
79         String response = getResponse(url);
80         if ("".equals(response) || "{}".equals(response)) {
81             url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-name:EQUALS:" + vserverName);
82             response = getResponse(url);
83         }
84         return response;
85     }
86
87     private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
88         String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR)+  "/" + vnfId);
89         String response = getResponse(url);
90         if ("".equals(response) || "{}".equals(response)) {
91             url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR) + "vnf-name=" + vnfName);
92             response = getResponse(url);
93         }
94         return response;
95     }
96
97     private String getBaseUrl(String suffixUrl) {
98         String url = "";
99         try {
100             String[] msbUrl = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":");
101             url = msbUrl[0] + ":" + msbUrl[1] + suffixUrl;
102         } catch (Exception e) {
103             log.info("Failed to get msb address");
104         }
105         if ("".equals(url)) {
106             try {
107                 url = "https://" + MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config").replace("http://", "")
108                         + suffixUrl;
109             } catch (Exception e) {
110                 log.info("Failed to get the address of A&AI.", e);
111             }
112         }
113         return url;
114     }
115
116     private String getMsbSuffixAddr(String suffixUrl) {
117         if (suffixUrl.length() <= 0) {
118             return "";
119         }
120         String[] addrSplits = suffixUrl.substring(1).split("/");
121         String[] conv = addrSplits[2].split("-");
122         addrSplits[2] = conv[0];
123         if (conv.length > 1) {
124             for(int i = 1; i < conv.length; i++) {
125                 addrSplits[2] = addrSplits[2] + conv[i].substring(0, 1).toUpperCase() + conv[i]
126                         .substring(1);
127             }
128         }
129         String ret = addrSplits[1];
130         addrSplits[1] = addrSplits[0] + "-" + addrSplits[2];
131         addrSplits[2] = ret;
132         addrSplits[0] = "api";
133         StringBuffer stringBuffer = new StringBuffer();
134         for (String split : addrSplits) {
135             stringBuffer.append("/" + split);
136         }
137         return stringBuffer.toString();
138     }
139
140     private String getResponse(String url) throws CorrelationException {
141         String response;
142         try {
143             HttpResponse httpResponse = HttpsUtils.get(url, getHeaders());
144             response = HttpsUtils.extractResponseEntity(httpResponse);
145         } catch (Exception e) {
146             throw new CorrelationException("Failed to get data from aai", e);
147         }
148         return response;
149     }
150
151     private Map getHeaders() {
152         Map<String, String> headers = new HashMap<>();
153         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
154         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
155         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
156         headers.put("Accept", "application/json");
157         return headers;
158     }
159 }