/**
- * Copyright 2018 ZTE Corporation.
+ * Copyright 2018-2023 ZTE Corporation.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
package org.onap.holmes.common.aai;
-
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import jakarta.ws.rs.client.Entity;
import org.onap.holmes.common.aai.config.AaiConfig;
import org.onap.holmes.common.config.MicroServiceConfig;
-import org.onap.holmes.common.exception.CorrelationException;
-
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.WebTarget;
-import javax.ws.rs.core.MultivaluedHashMap;
-import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.core.Response;
+import org.onap.holmes.common.utils.JerseyClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
public class AaiQuery4Ccvpn {
- private MultivaluedMap<String, Object> headers;
+ private final Logger log = LoggerFactory.getLogger(AaiQuery4Ccvpn.class);
+
+ private Map<String, Object> headers;
- static public AaiQuery4Ccvpn newInstance() {
+ public static AaiQuery4Ccvpn newInstance() {
return new AaiQuery4Ccvpn();
}
+ private static final String EMPTY_STR = "";
+
+ private static final JsonObject EMPTY_JSON = new JsonObject();
+
private AaiQuery4Ccvpn() {
- headers = new MultivaluedHashMap<>();
- headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
- headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
- headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
- headers.add("Accept", "application/json");
+ headers = new HashMap<>();
+ headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
+ headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
+ headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
+ headers.put("Accept", "application/json");
+ headers.put("Content-Type", "application/json");
}
/**
params.put("pnfName", pnfName);
params.put("ifName", ifName);
- Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_LINK_QUERY, params)
+ String response = get(getPath(AaiConfig.AaiConsts.AAI_LINK_QUERY, params)
+ (status == null ? "" : String.format("&operational-status=%s", status)));
- if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
- throw new RuntimeException("Failed to connect to AAI. Cause: "
- + response.getStatusInfo().getReasonPhrase());
- }
- JSONObject linkInfo = getInfo(JSONObject.toJSONString(response.getEntity()), "p-interface", "logical-link");
- return extractValueFromJsonArray(linkInfo.getJSONArray("relationship-data"), "logical-link.link-name");
+ JsonObject linkInfo = getInfo(response, "p-interface", "logical-link");
+
+ if (linkInfo == null) {
+ log.warn(String.format("Link information is missing from AAI. Method: [getLogicLink], " +
+ "params: [networkId - %s, pnfName - %s, ifName - %s].", networkId, pnfName, ifName));
+ return EMPTY_STR;
+ }
+ return extractValueFromJsonArray(linkInfo.get("relationship-data").getAsJsonArray(), "logical-link.link-name");
}
/**
- * Query all the instances related to a terminal point. This method is mainly based on the API:
+ * Query the service instances related to a terminal point. This method is mainly based on the API:
* https://<AAI host>:<AAI port>/aai/v14/network/connectivities?connectivity-id={connectivityId}
* and
* https://<AAI host>:<AAI port>/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}
* @param pnfName
* @param ifName
* @param status
- * @return all related service instances in JSONArray format
+ * @return service instances in JSONObject format
*/
- public JSONArray getServiceInstances(String networkId, String pnfName, String ifName, String status) {
- try {
- JSONObject vpnBindingInfo = getVpnBindingInfo(networkId, pnfName, ifName, status);
- String vpnBindingId = extractValueFromJsonArray(vpnBindingInfo.getJSONArray("relationship-data"),
- "vpn-binding.vpn-id");
- JSONObject connectivityInfo = getConnectivityInfo(vpnBindingId);
- String connectivityId = extractValueFromJsonArray(connectivityInfo.getJSONArray("relationship-data"),
- "connectivity. connectivity-id");
- JSONObject serviceInstanceInfo = getServiceInstanceByConn(connectivityId);
- String serviceInstancePath = serviceInstanceInfo.getString("related-link");
- serviceInstancePath = serviceInstancePath.substring(0, serviceInstancePath.lastIndexOf('/'));
-
- String[] params = new String[2];
-
- Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)/service-subscriptions/service-subscription/(.+)");
- Matcher matcher = pattern.matcher(serviceInstancePath);
- if (matcher.find()) {
- params[0] = matcher.group(1);
- params[1] = matcher.group(2);
- }
+ public JsonObject getServiceInstance(String networkId, String pnfName, String ifName, String status) {
+ JsonObject vpnBindingInfo = getVpnBindingInfo(networkId, pnfName, ifName, status);
+ if (vpnBindingInfo == null) {
+ log.warn(String.format("VPN binding information is missing from AAI. " +
+ "Method: [getServiceInstance], params: [networkId - %s, pnfName - %s, " +
+ "ifName - %s, status - %s].", networkId, pnfName, ifName, status));
+ return EMPTY_JSON;
+ }
+ String vpnBindingId = extractValueFromJsonArray(vpnBindingInfo.get("relationship-data").getAsJsonArray(),
+ "vpn-binding.vpn-id");
+ JsonObject connectivityInfo = getConnectivityInfo(vpnBindingId);
+ if (connectivityInfo == null) {
+ log.warn(String.format("Connectivity information is missing from AAI. " +
+ "Method: [getServiceInstance], params: [networkId - %s, pnfName - %s, " +
+ "ifName - %s, status - %s].", networkId, pnfName, ifName, status));
+ return EMPTY_JSON;
+ }
+ String connectivityId = extractValueFromJsonArray(connectivityInfo.get("relationship-data").getAsJsonArray(),
+ "connectivity.connectivity-id");
+ JsonObject serviceInstanceInfo = getServiceInstanceByConn(connectivityId);
+ if (serviceInstanceInfo == null) {
+ log.warn(String.format("Service instance information is missing from AAI. " +
+ "Method: [getServiceInstance], params: [networkId - %s, pnfName - %s, " +
+ "ifName - %s, status - %s].", networkId, pnfName, ifName, status));
+ return EMPTY_JSON;
+ }
+ String serviceInstancePath = serviceInstanceInfo.get("related-link").getAsString();
- Response response = get(getHostAddr(), getPath(serviceInstancePath));
- if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
- throw new RuntimeException("Failed to connect to AAI. Cause: "
- + response.getStatusInfo().getReasonPhrase());
- }
- JSONArray instances = getInstances(JSONObject.toJSONString(response.getEntity()));
- for (int i = 0; i < instances.size(); ++i) {
- JSONObject instance = instances.getJSONObject(i);
- Response res = get(getHostAddr(), serviceInstancePath + "/service-instances?service-instance-id="
- + instance.getString("service-instance-id"));
- if (res.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
- throw new RuntimeException("Failed to connect to AAI. Cause: "
- + response.getStatusInfo().getReasonPhrase());
- }
- String inputParams = JSONObject.parseObject(response.readEntity(String.class)).getString("input-parameters");
- instance.put("input-parameters", inputParams);
- instance.put("globalSubscriberId", params[0]);
- instance.put("serviceType", params[1]);
- }
+ String response = get(serviceInstancePath);
+ JsonObject instance = JsonParser.parseString(response).getAsJsonObject();
- return instances;
- } catch (CorrelationException e) {
- throw new RuntimeException(e.getMessage(), e);
+ String[] params = new String[2];
+ Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)" +
+ "/service-subscriptions/service-subscription/(.+)" +
+ "/service-instances/service-instance/(.+)");
+ Matcher matcher = pattern.matcher(serviceInstancePath);
+ if (matcher.find()) {
+ params[0] = matcher.group(1);
+ params[1] = matcher.group(2);
}
+ instance.addProperty("globalSubscriberId", params[0]);
+ instance.addProperty("serviceType", params[1]);
+ return instance;
}
+
public void updateTerminalPointStatus(String networkId, String pnfName, String ifName,
- Map<String, Object> body) throws CorrelationException {
+ Map<String, Object> body) {
Map<String, String> params = new HashMap<>();
params.put("networkId", networkId);
params.put("pnfName", pnfName);
params.put("ifName", ifName);
- Response response = patch(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, params), body);
- if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
- throw new CorrelationException("Failed to connecto to AAI. Cause: "
- + response.getStatusInfo().getReasonPhrase());
- }
+ String r = get(getPath(AaiConfig.AaiConsts.AAI_TP_UPDATE, params));
+ JsonObject jsonObject = JsonParser.parseString(r).getAsJsonObject();
+ body.put("resource-version", jsonObject.get("resource-version").toString());
+
+ put(getPath(AaiConfig.AaiConsts.AAI_TP_UPDATE, params), body);
}
- public void updateLogicLinkStatus(String linkName, Map<String, Object> body) throws CorrelationException {
- Response response = patch(getHostAddr(),
- getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, "linkName", linkName), body);
- if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
- throw new CorrelationException("Failed to connecto to AAI. Cause: "
- + response.getStatusInfo().getReasonPhrase());
- }
+
+ public void updateLogicLinkStatus(String linkName, Map<String, Object> body) {
+ String r = get(getPath(AaiConfig.AaiConsts.AAI_LINK_UPDATE, "linkName", linkName));
+ JsonObject jsonObject = JsonParser.parseString(r).getAsJsonObject();
+ body.put("resource-version", jsonObject.get("resource-version").toString());
+ body.put("link-type", jsonObject.get("link-type").toString());
+ put(getPath(AaiConfig.AaiConsts.AAI_LINK_UPDATE, "linkName", linkName), body);
}
- private JSONObject getVpnBindingInfo(String networkId, String pnfName,
- String ifName, String status) throws CorrelationException {
+ private JsonObject getVpnBindingInfo(String networkId, String pnfName,
+ String ifName, String status) {
Map<String, String> params = new HashMap();
params.put("networkId", networkId);
params.put("pnfName", pnfName);
params.put("ifName", ifName);
params.put("status", status);
- Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_VPN_ADDR, params));
- if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
- throw new CorrelationException("Failed to connecto to AAI. Cause: "
- + response.getStatusInfo().getReasonPhrase());
- }
- return getInfo(JSONObject.toJSONString(response.getEntity()), "p-interface", "vpn-binding");
+ String response = get(getPath(AaiConfig.AaiConsts.AAI_VPN_ADDR, params));
+ return getInfo(response, "p-interface", "vpn-binding");
}
- private JSONObject getConnectivityInfo(String vpnId) throws CorrelationException {
- Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_CONN_ADDR, "vpnId", vpnId));
- if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
- throw new CorrelationException("Failed to connect to AAI. Cause: "
- + response.getStatusInfo().getReasonPhrase());
- }
- return getInfo(JSONObject.toJSONString(response.getEntity()), "vpn-binding", "connectivity");
+ private JsonObject getConnectivityInfo(String vpnId) {
+ String response = get(getPath(AaiConfig.AaiConsts.AAI_CONN_ADDR, "vpnId", vpnId));
+ return getInfo(response, "vpn-binding", "connectivity");
}
- private JSONObject getServiceInstanceByConn(String connectivityId) throws CorrelationException {
- Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCE_ADDR_4_CCVPN,
+ private JsonObject getServiceInstanceByConn(String connectivityId) {
+ String response = get(getPath(AaiConfig.AaiConsts.AAI_SERVICE_INSTANCE_ADDR_4_CCVPN,
"connectivityId", connectivityId));
- if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
- throw new CorrelationException("Failed to connect to AAI. Cause: "
- + response.getStatusInfo().getReasonPhrase());
- }
- return getInfo(JSONObject.toJSONString(response.getEntity()), "connectivity", "service-instance");
- }
-
- private JSONArray getServiceInstances(String globalCustomerId, String serviceType) throws CorrelationException {
- Map<String, String> params = new HashMap();
- params.put("global-customer-id", globalCustomerId);
- params.put("service-type", serviceType);
- Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCES_ADDR_4_CCVPN, params));
- if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
- throw new CorrelationException("Failed to connect to AAI. Cause: "
- + response.getStatusInfo().getReasonPhrase());
- }
- return getInstances(JSONObject.toJSONString(response.getEntity()));
+ return getInfo(response, "connectivity", "service-instance");
}
private String getPath(String urlTemplate, Map<String, String> pathParams) {
String url = urlTemplate;
- for (String key : pathParams.keySet()) {
- url = url.replaceAll("\\{" + key + "\\}", pathParams.get(key));
+ for (Map.Entry<String, String> entry : pathParams.entrySet()) {
+ url = url.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue());
}
return url;
}
return ret;
}
- private Response get(String host, String path) {
- Client client = ClientBuilder.newClient();
- WebTarget target = client.target(host).path(path);
- return target.request().headers(getAaiHeaders()).get();
- }
-
- private Response patch(String host, String path, Map<String, Object> body) {
- Client client = ClientBuilder.newClient();
- WebTarget target = client.target(host).path(path);
- return target.request().headers(getAaiHeaders()).method("PATCH", Entity.json(body));
- }
-
- private JSONObject getInfo(String response, String pField, String field) {
- JSONArray results = extractJsonArray(JSONObject.parseObject(response), "results");
- JSONObject pInterface = extractJsonObject(results.getJSONObject(0), pField);
- JSONObject relationshipList = extractJsonObject(pInterface, "relationship-list");
- JSONArray relationShip = extractJsonArray(relationshipList, "relationship");
+ private JsonObject getInfo(String response, String pField, String field) {
+ JsonObject jObject = JsonParser.parseString(response).getAsJsonObject();
+ JsonObject pInterface = extractJsonObject(jObject, pField);
+ if (pInterface == null) {
+ pInterface = jObject;
+ }
+ JsonObject relationshipList = extractJsonObject(pInterface, "relationship-list");
+ JsonArray relationShip = extractJsonArray(relationshipList, "relationship");
if (relationShip != null) {
for (int i = 0; i < relationShip.size(); ++i) {
- final JSONObject object = relationShip.getJSONObject(i);
- if (object.getString("related-to").equals(field)) {
+ final JsonObject object = relationShip.get(i).getAsJsonObject();
+ if (object.get("related-to").getAsString().equals(field)) {
return object;
}
}
return null;
}
- private JSONArray getInstances(String response) {
- JSONArray results = extractJsonArray(JSONObject.parseObject(response), "results");
- JSONObject pInterface = extractJsonObject(results.getJSONObject(0), "service-subscription");
- JSONObject serviceInstances = extractJsonObject(pInterface, "service-instances");
- JSONArray instance = extractJsonArray(serviceInstances, "service-instance");
- return instance;
- }
-
- private JSONObject extractJsonObject(JSONObject obj, String key) {
- if (obj != null && key != null && obj.containsKey(key)) {
- return obj.getJSONObject(key);
+ private JsonObject extractJsonObject(JsonObject obj, String key) {
+ if (obj != null && key != null && obj.has(key)) {
+ return obj.get(key).getAsJsonObject();
}
return null;
}
- private JSONArray extractJsonArray(JSONObject obj, String key) {
- if (obj != null && key != null && obj.containsKey(key)) {
- return obj.getJSONArray(key);
+ private JsonArray extractJsonArray(JsonObject obj, String key) {
+ if (obj != null && key != null && obj.has(key)) {
+ return obj.get(key).getAsJsonArray();
}
return null;
}
- private MultivaluedMap getAaiHeaders() {
- return headers;
+ private String extractValueFromJsonArray(JsonArray relationshipData, String keyName) {
+ if (relationshipData != null) {
+ for (int i = 0; i < relationshipData.size(); ++i) {
+ JsonObject item = relationshipData.get(i).getAsJsonObject();
+ if (item.get("relationship-key").getAsString().equals(keyName)) {
+ return item.get("relationship-value").getAsString();
+ }
+ }
+ }
+ return null;
}
- private String getHostAddr() {
- String[] msbInfo = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":");
- StringBuilder sb = new StringBuilder("http://");
- sb.append(msbInfo[0]).append(msbInfo[1]);
- return sb.toString();
+ private String get(String path) {
+ return JerseyClient.newInstance().path(path).headers(headers).get(MicroServiceConfig.getAaiAddr());
}
- private String extractValueFromJsonArray(JSONArray relationshipData, String keyName) {
- for (int i = 0; i < relationshipData.size(); ++i) {
- JSONObject item = relationshipData.getJSONObject(i);
- if (item.getString("relationship-key").equals(keyName)) {
- return item.getString("relationship-value");
- }
- }
- return null;
+ private String put(String path, Map<String, Object> body) {
+ return JerseyClient.newInstance().path(path).headers(headers).put(MicroServiceConfig.getAaiAddr(), Entity.json(body));
}
}