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;
31 import java.util.HashMap;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
36 public class AaiQuery4Ccvpn {
38 private MultivaluedMap<String, Object> headers;
40 static public AaiQuery4Ccvpn newInstance() {
41 return new AaiQuery4Ccvpn();
44 private AaiQuery4Ccvpn() {
45 headers = new MultivaluedHashMap<>();
46 headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
47 headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
48 headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
49 headers.add("Accept", "application/json");
53 * Query the logic link information for AAI. This method is based on the API:
54 * https://<AAI host>:<AAI port>/aai/v14/network/network-resources/network-resource/{networkId}/pnfs/pnf/{pnfName}/p-interfaces?interface-name={ifName}&operational-status={status}
61 * @return the ID of the logic link
63 public String getLogicLink(String networkId, String pnfName, String ifName, String status) {
64 Map<String, String> params = new HashMap<>();
65 params.put("networkId", networkId);
66 params.put("pnfName", pnfName);
67 params.put("ifName", ifName);
69 Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_LINK_QUERY, params)
70 + (status == null ? "" : String.format("&operational-status=%s", status)));
71 if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
72 throw new RuntimeException("Failed to connect to AAI. Cause: "
73 + response.getStatusInfo().getReasonPhrase());
76 JSONObject linkInfo = getInfo(JSONObject.toJSONString(response.getEntity()), "p-interface", "logical-link");
77 return extractValueFromJsonArray(linkInfo.getJSONArray("relationship-data"), "logical-link.link-name");
81 * Query all the instances related to a terminal point. This method is mainly based on the API:
82 * https://<AAI host>:<AAI port>/aai/v14/network/connectivities?connectivity-id={connectivityId}
84 * https://<AAI host>:<AAI port>/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}
85 * provided by AAI. The path for getting the required instance information is: p-interface → vpn-vpnbinding → connectivity → service instance
91 * @return all related service instances in JSONArray format
93 public JSONArray getServiceInstances(String networkId, String pnfName, String ifName, String status) {
95 JSONObject vpnBindingInfo = getVpnBindingInfo(networkId, pnfName, ifName, status);
96 String vpnBindingId = extractValueFromJsonArray(vpnBindingInfo.getJSONArray("relationship-data"),
97 "vpn-binding.vpn-id");
98 JSONObject connectivityInfo = getConnectivityInfo(vpnBindingId);
99 String connectivityId = extractValueFromJsonArray(connectivityInfo.getJSONArray("relationship-data"),
100 "connectivity. connectivity-id");
101 JSONObject serviceInstanceInfo = getServiceInstanceByConn(connectivityId);
102 String serviceInstancePath = serviceInstanceInfo.getString("related-link");
103 serviceInstancePath = serviceInstancePath.substring(0, serviceInstancePath.lastIndexOf('/'));
105 String[] params = new String[2];
107 Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)/service-subscriptions/service-subscription/(.+)");
108 Matcher matcher = pattern.matcher(serviceInstancePath);
109 if (matcher.find()) {
110 params[0] = matcher.group(1);
111 params[1] = matcher.group(2);
114 Response response = get(getHostAddr(), getPath(serviceInstancePath));
115 if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
116 throw new RuntimeException("Failed to connect to AAI. Cause: "
117 + response.getStatusInfo().getReasonPhrase());
119 JSONArray instances = getInstances(JSONObject.toJSONString(response.getEntity()));
120 for (int i = 0; i < instances.size(); ++i) {
121 JSONObject instance = instances.getJSONObject(i);
122 Response res = get(getHostAddr(), serviceInstancePath + "/service-instances?service-instance-id="
123 + instance.getString("service-instance-id"));
124 if (res.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
125 throw new RuntimeException("Failed to connect to AAI. Cause: "
126 + response.getStatusInfo().getReasonPhrase());
128 String inputParams = JSONObject.parseObject(response.readEntity(String.class)).getString("input-parameters");
129 instance.put("input-parameters", inputParams);
130 instance.put("globalSubscriberId", params[0]);
131 instance.put("serviceType", params[1]);
135 } catch (CorrelationException e) {
136 throw new RuntimeException(e.getMessage(), e);
140 public void updateTerminalPointStatus(String networkId, String pnfName, String ifName,
141 Map<String, Object> body) throws CorrelationException {
142 Map<String, String> params = new HashMap<>();
143 params.put("networkId", networkId);
144 params.put("pnfName", pnfName);
145 params.put("ifName", ifName);
146 Response response = patch(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, params), body);
147 if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
148 throw new CorrelationException("Failed to connecto to AAI. Cause: "
149 + response.getStatusInfo().getReasonPhrase());
153 public void updateLogicLinkStatus(String linkName, Map<String, Object> body) throws CorrelationException {
154 Response response = patch(getHostAddr(),
155 getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, "linkName", linkName), body);
156 if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
157 throw new CorrelationException("Failed to connecto to AAI. Cause: "
158 + response.getStatusInfo().getReasonPhrase());
162 private JSONObject getVpnBindingInfo(String networkId, String pnfName,
163 String ifName, String status) throws CorrelationException {
164 Map<String, String> params = new HashMap();
165 params.put("networkId", networkId);
166 params.put("pnfName", pnfName);
167 params.put("ifName", ifName);
168 params.put("status", status);
169 Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_VPN_ADDR, params));
170 if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
171 throw new CorrelationException("Failed to connecto to AAI. Cause: "
172 + response.getStatusInfo().getReasonPhrase());
174 return getInfo(JSONObject.toJSONString(response.getEntity()), "p-interface", "vpn-binding");
177 private JSONObject getConnectivityInfo(String vpnId) throws CorrelationException {
178 Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_CONN_ADDR, "vpnId", vpnId));
179 if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
180 throw new CorrelationException("Failed to connect to AAI. Cause: "
181 + response.getStatusInfo().getReasonPhrase());
183 return getInfo(JSONObject.toJSONString(response.getEntity()), "vpn-binding", "connectivity");
186 private JSONObject getServiceInstanceByConn(String connectivityId) throws CorrelationException {
187 Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCE_ADDR_4_CCVPN,
188 "connectivityId", connectivityId));
189 if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
190 throw new CorrelationException("Failed to connect to AAI. Cause: "
191 + response.getStatusInfo().getReasonPhrase());
193 return getInfo(JSONObject.toJSONString(response.getEntity()), "connectivity", "service-instance");
196 private JSONArray getServiceInstances(String globalCustomerId, String serviceType) throws CorrelationException {
197 Map<String, String> params = new HashMap();
198 params.put("global-customer-id", globalCustomerId);
199 params.put("service-type", serviceType);
200 Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCES_ADDR_4_CCVPN, params));
201 if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
202 throw new CorrelationException("Failed to connect to AAI. Cause: "
203 + response.getStatusInfo().getReasonPhrase());
205 return getInstances(JSONObject.toJSONString(response.getEntity()));
208 private String getPath(String urlTemplate, Map<String, String> pathParams) {
209 String url = urlTemplate;
210 for (String key : pathParams.keySet()) {
211 url = url.replaceAll("\\{" + key + "\\}", pathParams.get(key));
216 private String getPath(String urlTemplate, String paramName, String paramValue) {
217 return urlTemplate.replaceAll("\\{" + paramName + "\\}", paramValue);
220 private String getPath(String serviceInstancePath) {
221 Pattern pattern = Pattern.compile("/aai/(v\\d+)/([A-Za-z0-9\\-]+[^/])(/*.*)");
222 Matcher matcher = pattern.matcher(serviceInstancePath);
224 if (matcher.find()) {
225 ret += "/aai-" + matcher.group(2) + "/" + matcher.group(1) + matcher.group(3);
231 private Response get(String host, String path) {
232 Client client = ClientBuilder.newClient();
233 WebTarget target = client.target(host).path(path);
234 return target.request().headers(getAaiHeaders()).get();
237 private Response patch(String host, String path, Map<String, Object> body) {
238 Client client = ClientBuilder.newClient();
239 WebTarget target = client.target(host).path(path);
240 return target.request().headers(getAaiHeaders()).method("PATCH", Entity.json(body));
243 private JSONObject getInfo(String response, String pField, String field) {
244 JSONArray results = extractJsonArray(JSONObject.parseObject(response), "results");
245 JSONObject pInterface = extractJsonObject(results.getJSONObject(0), pField);
246 JSONObject relationshipList = extractJsonObject(pInterface, "relationship-list");
247 JSONArray relationShip = extractJsonArray(relationshipList, "relationship");
248 if (relationShip != null) {
249 for (int i = 0; i < relationShip.size(); ++i) {
250 final JSONObject object = relationShip.getJSONObject(i);
251 if (object.getString("related-to").equals(field)) {
259 private JSONArray getInstances(String response) {
260 JSONArray results = extractJsonArray(JSONObject.parseObject(response), "results");
261 JSONObject pInterface = extractJsonObject(results.getJSONObject(0), "service-subscription");
262 JSONObject serviceInstances = extractJsonObject(pInterface, "service-instances");
263 JSONArray instance = extractJsonArray(serviceInstances, "service-instance");
267 private JSONObject extractJsonObject(JSONObject obj, String key) {
268 if (obj != null && key != null && obj.containsKey(key)) {
269 return obj.getJSONObject(key);
274 private JSONArray extractJsonArray(JSONObject obj, String key) {
275 if (obj != null && key != null && obj.containsKey(key)) {
276 return obj.getJSONArray(key);
281 private MultivaluedMap getAaiHeaders() {
285 private String getHostAddr() {
286 String[] msbInfo = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":");
287 StringBuilder sb = new StringBuilder("http://");
288 sb.append(msbInfo[0]).append(msbInfo[1]);
289 return sb.toString();
292 private String extractValueFromJsonArray(JSONArray relationshipData, String keyName) {
293 for (int i = 0; i < relationshipData.size(); ++i) {
294 JSONObject item = relationshipData.getJSONObject(i);
295 if (item.getString("relationship-key").equals(keyName)) {
296 return item.getString("relationship-value");