Test Replace Jackson with GSON
[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.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;
28
29 @Service
30 @Slf4j
31 public class AaiQuery {
32
33     @Inject
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 url = "";
57         String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
58         String baseUrl = getBaseUrl("");
59         if (baseUrl.startsWith("http")) {
60             url = baseUrl + getMsbSuffixAddr(resourceLinkUrl);
61         } else {
62             url = baseUrl + resourceLinkUrl;
63         }
64         return url;
65     }
66
67     private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
68         String response = getResourceLinksResponse(vserverId, vserverName);
69         List linkList = aaiResponseUtil.convertJsonToVmResourceLink(response);
70         if (linkList.size() != 0) {
71             return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
72         }
73         return  "";
74     }
75
76     private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
77         String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-id:EQUALS:" + vserverId);
78         String response = getResponse(url);
79         if ("".equals(response) || "{}".equals(response)) {
80             url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-name:EQUALS:" + vserverName);
81             response = getResponse(url);
82         }
83         return response;
84     }
85
86     private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
87         String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR)+  "/" + vnfId);
88         String response = getResponse(url);
89         if ("".equals(response) || "{}".equals(response)) {
90             url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR) + "vnf-name=" + vnfName);
91             response = getResponse(url);
92         }
93         return response;
94     }
95
96     private String getBaseUrl(String suffixUrl) {
97         String url = "";
98         try {
99             String[] msbUrl = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":");
100             url = msbUrl[0] + ":" + msbUrl[1] + suffixUrl;
101         } catch (Exception e) {
102             log.info("Failed to get msb address");
103         }
104         if ("".equals(url)) {
105             try {
106                 url = "https://" + MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config").replace("http://", "")
107                         + suffixUrl;
108             } catch (Exception e) {
109                 log.info("Failed to get the address of A&AI.", e);
110             }
111         }
112         return url;
113     }
114
115     private String getMsbSuffixAddr(String suffixUrl) {
116         if (suffixUrl.length() <= 0) {
117             return "";
118         }
119         String[] addrSplits = suffixUrl.substring(1).split("/");
120         String[] conv = addrSplits[2].split("-");
121         addrSplits[2] = conv[0];
122         if (conv.length > 1) {
123             for(int i = 1; i < conv.length; i++) {
124                 addrSplits[2] = addrSplits[2] + conv[i].substring(0, 1).toUpperCase() + conv[i]
125                         .substring(1);
126             }
127         }
128         String ret = addrSplits[1];
129         addrSplits[1] = addrSplits[0] + "-" + addrSplits[2];
130         addrSplits[2] = ret;
131         addrSplits[0] = "api";
132         StringBuffer stringBuffer = new StringBuffer();
133         for (String split : addrSplits) {
134             stringBuffer.append("/" + split);
135         }
136         return stringBuffer.toString();
137     }
138
139     private String getResponse(String url) throws CorrelationException {
140         String response = "";
141         try {
142             response = HttpsUtils.get(url, getHeaders());
143         } catch (Exception e) {
144             throw new CorrelationException("Failed to get data from aai", e);
145         }
146         return response;
147     }
148
149     private Map getHeaders() {
150         Map<String, String> headers = new HashMap<>();
151         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
152         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
153         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
154         headers.put("Accept", "application/json");
155         return headers;
156     }
157 }