Add some logs to AAI queries
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / AaiQuery4Ccvpn.java
1 /**
2  * Copyright 2018 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
15 package org.onap.holmes.common.aai;
16
17
18 import com.alibaba.fastjson.JSONArray;
19 import com.alibaba.fastjson.JSONObject;
20 import org.onap.holmes.common.aai.config.AaiConfig;
21 import org.onap.holmes.common.config.MicroServiceConfig;
22 import org.onap.holmes.common.exception.CorrelationException;
23
24 import javax.ws.rs.client.Client;
25 import javax.ws.rs.client.ClientBuilder;
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.client.WebTarget;
28 import javax.ws.rs.core.MultivaluedHashMap;
29 import javax.ws.rs.core.MultivaluedMap;
30 import javax.ws.rs.core.Response;
31
32 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
33
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38
39 public class AaiQuery4Ccvpn {
40
41     private MultivaluedMap<String, Object> headers;
42
43     static public AaiQuery4Ccvpn newInstance() {
44         return new AaiQuery4Ccvpn();
45     }
46
47     private AaiQuery4Ccvpn() {
48         headers = new MultivaluedHashMap<>();
49         headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
50         headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
51         headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
52         headers.add("Accept", "application/json");
53     }
54
55     /**
56      * Query the logic link information for AAI. This method is based on the API:
57      * https://<AAI host>:<AAI port>/aai/v14/network/network-resources/network-resource/{networkId}/pnfs/pnf/{pnfName}/p-interfaces?interface-name={ifName}&operational-status={status}
58      * provided by AAI.
59      *
60      * @param networkId
61      * @param pnfName
62      * @param ifName
63      * @param status
64      * @return the ID of the logic link
65      */
66     public String getLogicLink(String networkId, String pnfName, String ifName, String status) throws CorrelationException {
67         Map<String, String> params = new HashMap<>();
68         params.put("networkId", networkId);
69         params.put("pnfName", pnfName);
70         params.put("ifName", ifName);
71
72         Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_LINK_QUERY, params)
73                 + (status == null ? "" : String.format("&operational-status=%s", status)));
74         JSONObject linkInfo = getInfo(response.readEntity(String.class), "p-interface", "logical-link");
75         return extractValueFromJsonArray(linkInfo.getJSONArray("relationship-data"), "logical-link.link-name");
76     }
77
78     /**
79      * Query all the instances related to a terminal point. This method is mainly based on the API:
80      * https://<AAI host>:<AAI port>/aai/v14/network/connectivities?connectivity-id={connectivityId}
81      * and
82      * https://<AAI host>:<AAI port>/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}
83      * provided by AAI. The path for getting the required instance information is: p-interface → vpn-vpnbinding → connectivity → service instance
84      *
85      * @param networkId
86      * @param pnfName
87      * @param ifName
88      * @param status
89      * @return all related service instances in JSONArray format
90      */
91     public JSONArray getServiceInstances(String networkId, String pnfName, String ifName, String status) {
92         try {
93             JSONObject vpnBindingInfo = getVpnBindingInfo(networkId, pnfName, ifName, status);
94             String vpnBindingId = extractValueFromJsonArray(vpnBindingInfo.getJSONArray("relationship-data"),
95                     "vpn-binding.vpn-id");
96             JSONObject connectivityInfo = getConnectivityInfo(vpnBindingId);
97             String connectivityId = extractValueFromJsonArray(connectivityInfo.getJSONArray("relationship-data"),
98                     "connectivity. connectivity-id");
99             JSONObject serviceInstanceInfo = getServiceInstanceByConn(connectivityId);
100             String serviceInstancePath = serviceInstanceInfo.getString("related-link");
101             serviceInstancePath = serviceInstancePath.substring(0, serviceInstancePath.lastIndexOf('/'));
102
103             String[] params = new String[2];
104
105             Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)/service-subscriptions/service-subscription/(.+)");
106             Matcher matcher = pattern.matcher(serviceInstancePath);
107             if (matcher.find()) {
108                 params[0] = matcher.group(1);
109                 params[1] = matcher.group(2);
110             }
111
112             Response response = get(getHostAddr(), getPath(serviceInstancePath));
113             JSONArray instances = getInstances(response.readEntity(String.class));
114             for (int i = 0; i < instances.size(); ++i) {
115                 JSONObject instance = instances.getJSONObject(i);
116                 Response res = get(getHostAddr(), serviceInstancePath + "/service-instances?service-instance-id="
117                         + instance.getString("service-instance-id"));
118                 String inputParams = JSONObject.parseObject(res.readEntity(String.class)).getString("input-parameters");
119                 instance.put("input-parameters", inputParams);
120                 instance.put("globalSubscriberId", params[0]);
121                 instance.put("serviceType", params[1]);
122             }
123
124             return instances;
125         } catch (CorrelationException e) {
126             throw new RuntimeException(e.getMessage(), e);
127         }
128     }
129
130     public void updateTerminalPointStatus(String networkId, String pnfName, String ifName,
131                                           Map<String, Object> body) throws CorrelationException {
132         Map<String, String> params = new HashMap<>();
133         params.put("networkId", networkId);
134         params.put("pnfName", pnfName);
135         params.put("ifName", ifName);
136         patch(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, params), body);
137     }
138
139     public void updateLogicLinkStatus(String linkName, Map<String, Object> body) throws CorrelationException {
140         patch(getHostAddr(),
141                 getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, "linkName", linkName), body);
142     }
143
144     private JSONObject getVpnBindingInfo(String networkId, String pnfName,
145                                          String ifName, String status) throws CorrelationException {
146         Map<String, String> params = new HashMap();
147         params.put("networkId", networkId);
148         params.put("pnfName", pnfName);
149         params.put("ifName", ifName);
150         params.put("status", status);
151         Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_VPN_ADDR, params));
152         return getInfo(response.readEntity(String.class), "p-interface", "vpn-binding");
153     }
154
155     private JSONObject getConnectivityInfo(String vpnId) throws CorrelationException {
156         Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_CONN_ADDR, "vpnId", vpnId));
157         return getInfo(response.readEntity(String.class), "vpn-binding", "connectivity");
158     }
159
160     private JSONObject getServiceInstanceByConn(String connectivityId) throws CorrelationException {
161         Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCE_ADDR_4_CCVPN,
162                 "connectivityId", connectivityId));
163         return getInfo(response.readEntity(String.class), "connectivity", "service-instance");
164     }
165
166     private JSONArray getServiceInstances(String globalCustomerId, String serviceType) throws CorrelationException {
167         Map<String, String> params = new HashMap();
168         params.put("global-customer-id", globalCustomerId);
169         params.put("service-type", serviceType);
170         Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCES_ADDR_4_CCVPN, params));
171         return getInstances(response.readEntity(String.class));
172     }
173
174     private String getPath(String urlTemplate, Map<String, String> pathParams) {
175         String url = urlTemplate;
176         for (String key : pathParams.keySet()) {
177             url = url.replaceAll("\\{" + key + "\\}", pathParams.get(key));
178         }
179         return url;
180     }
181
182     private String getPath(String urlTemplate, String paramName, String paramValue) {
183         return urlTemplate.replaceAll("\\{" + paramName + "\\}", paramValue);
184     }
185
186     private String getPath(String serviceInstancePath) {
187         Pattern pattern = Pattern.compile("/aai/(v\\d+)/([A-Za-z0-9\\-]+[^/])(/*.*)");
188         Matcher matcher = pattern.matcher(serviceInstancePath);
189         String ret = "/api";
190         if (matcher.find()) {
191             ret += "/aai-" + matcher.group(2) + "/" + matcher.group(1) + matcher.group(3);
192         }
193
194         return ret;
195     }
196
197     private Response get(String host, String path) throws CorrelationException {
198         Client client = ClientBuilder.newClient();
199         WebTarget target = client.target(host).path(path);
200         try {
201             Response response = target.request().headers(getAaiHeaders()).get();
202             if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
203                 throw new CorrelationException("Failed to connect to AAI. \nCause: "
204                         + response.getStatusInfo().getReasonPhrase() + "\nDetails: \n"
205                         + getErrorMsg(String.format("%s%s", host, path), null, response));
206             }
207             return response;
208         } catch (CorrelationException e) {
209             throw e;
210         } catch (Exception e) {
211             throw new CorrelationException(e.getMessage() + "More info: "
212                     + getErrorMsg(String.format("%s%s", host, path), null, null), e);
213         }
214     }
215
216     private void patch(String host, String path, Map<String, Object> body) throws CorrelationException {
217         Client client = ClientBuilder.newClient();
218         WebTarget target = client.target(host).path(path);
219         try {
220             Response response = target.request().headers(getAaiHeaders()).build("PATCH", Entity.json(body))
221                     .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true).invoke();
222             if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
223                 throw new CorrelationException("Failed to connecto to AAI. \nCause: "
224                         + response.getStatusInfo().getReasonPhrase() + "\nDetails: \n"
225                         + getErrorMsg(String.format("%s%s", host, path), body, response));
226             }
227         } catch (CorrelationException e) {
228             throw e;
229         } catch (Exception e) {
230             throw new CorrelationException(e.getMessage() + "More info: "
231                     + getErrorMsg(String.format("%s%s", host, path), body, null), e);
232         }
233     }
234
235     private JSONObject getInfo(String response, String pField, String field) {
236         JSONArray results = extractJsonArray(JSONObject.parseObject(response), "results");
237         JSONObject pInterface = extractJsonObject(results.getJSONObject(0), pField);
238         JSONObject relationshipList = extractJsonObject(pInterface, "relationship-list");
239         JSONArray relationShip = extractJsonArray(relationshipList, "relationship");
240         if (relationShip != null) {
241             for (int i = 0; i < relationShip.size(); ++i) {
242                 final JSONObject object = relationShip.getJSONObject(i);
243                 if (object.getString("related-to").equals(field)) {
244                     return object;
245                 }
246             }
247         }
248         return null;
249     }
250
251     private JSONArray getInstances(String response) {
252         JSONArray results = extractJsonArray(JSONObject.parseObject(response), "results");
253         JSONObject pInterface = extractJsonObject(results.getJSONObject(0), "service-subscription");
254         JSONObject serviceInstances = extractJsonObject(pInterface, "service-instances");
255         JSONArray instance = extractJsonArray(serviceInstances, "service-instance");
256         return instance;
257     }
258
259     private JSONObject extractJsonObject(JSONObject obj, String key) {
260         if (obj != null && key != null && obj.containsKey(key)) {
261             return obj.getJSONObject(key);
262         }
263         return null;
264     }
265
266     private JSONArray extractJsonArray(JSONObject obj, String key) {
267         if (obj != null && key != null && obj.containsKey(key)) {
268             return obj.getJSONArray(key);
269         }
270         return null;
271     }
272
273     private MultivaluedMap getAaiHeaders() {
274         return headers;
275     }
276
277     private String getHostAddr() {
278         return MicroServiceConfig.getMsbServerAddrWithHttpPrefix();
279     }
280
281     private String extractValueFromJsonArray(JSONArray relationshipData, String keyName) {
282         for (int i = 0; i < relationshipData.size(); ++i) {
283             JSONObject item = relationshipData.getJSONObject(i);
284             if (item.getString("relationship-key").equals(keyName)) {
285                 return item.getString("relationship-value");
286             }
287         }
288         return null;
289     }
290
291     private String getErrorMsg(String url, Map<String, Object> body, Response response) {
292         StringBuilder sb = new StringBuilder();
293         sb.append("Rerquest URL: ").append(url).append("\n");
294         sb.append("Request Header: ").append(JSONObject.toJSONString(headers)).append("\n");
295         if (body != null) {
296             sb.append("Request Body: ").append(JSONObject.toJSONString(body)).append("\n");
297         }
298         if (response != null) {
299             sb.append("Request Body: ").append(response.readEntity(String.class));
300         }
301         return sb.toString();
302     }
303 }