2 * Copyright 2018 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
15 package org.onap.holmes.common.aai;
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;
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;
32 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
34 import java.util.HashMap;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
39 public class AaiQuery4Ccvpn {
41 private MultivaluedMap<String, Object> headers;
43 static public AaiQuery4Ccvpn newInstance() {
44 return new AaiQuery4Ccvpn();
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");
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}
64 * @return the ID of the logic link
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);
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");
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}
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
89 * @return all related service instances in JSONArray format
91 public JSONArray getServiceInstances(String networkId, String pnfName, String ifName, String status) {
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('/'));
103 String[] params = new String[2];
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);
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]);
125 } catch (CorrelationException e) {
126 throw new RuntimeException(e.getMessage(), e);
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);
139 public void updateLogicLinkStatus(String linkName, Map<String, Object> body) throws CorrelationException {
141 getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, "linkName", linkName), body);
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");
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");
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");
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));
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));
182 private String getPath(String urlTemplate, String paramName, String paramValue) {
183 return urlTemplate.replaceAll("\\{" + paramName + "\\}", paramValue);
186 private String getPath(String serviceInstancePath) {
187 Pattern pattern = Pattern.compile("/aai/(v\\d+)/([A-Za-z0-9\\-]+[^/])(/*.*)");
188 Matcher matcher = pattern.matcher(serviceInstancePath);
190 if (matcher.find()) {
191 ret += "/aai-" + matcher.group(2) + "/" + matcher.group(1) + matcher.group(3);
197 private Response get(String host, String path) throws CorrelationException {
198 Client client = ClientBuilder.newClient();
199 WebTarget target = client.target(host).path(path);
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));
208 } catch (CorrelationException e) {
210 } catch (Exception e) {
211 throw new CorrelationException(e.getMessage() + "More info: "
212 + getErrorMsg(String.format("%s%s", host, path), null, null), e);
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);
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));
227 } catch (CorrelationException e) {
229 } catch (Exception e) {
230 throw new CorrelationException(e.getMessage() + "More info: "
231 + getErrorMsg(String.format("%s%s", host, path), body, null), e);
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)) {
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");
259 private JSONObject extractJsonObject(JSONObject obj, String key) {
260 if (obj != null && key != null && obj.containsKey(key)) {
261 return obj.getJSONObject(key);
266 private JSONArray extractJsonArray(JSONObject obj, String key) {
267 if (obj != null && key != null && obj.containsKey(key)) {
268 return obj.getJSONArray(key);
273 private MultivaluedMap getAaiHeaders() {
277 private String getHostAddr() {
278 return MicroServiceConfig.getMsbServerAddrWithHttpPrefix();
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");
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");
296 sb.append("Request Body: ").append(JSONObject.toJSONString(body)).append("\n");
298 if (response != null) {
299 sb.append("Request Body: ").append(response.readEntity(String.class));
301 return sb.toString();