Improved the UT coverage
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / AaiQuery4Ccvpn.java
index de3cbf9..4cf2aba 100644 (file)
 
 package org.onap.holmes.common.aai;
 
-
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import org.glassfish.jersey.client.HttpUrlConnectorProvider;
 import org.onap.holmes.common.aai.config.AaiConfig;
 import org.onap.holmes.common.config.MicroServiceConfig;
 import org.onap.holmes.common.exception.CorrelationException;
@@ -28,28 +29,33 @@ 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.glassfish.jersey.client.HttpUrlConnectorProvider;
-
 import java.util.HashMap;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
 public class AaiQuery4Ccvpn {
 
     private MultivaluedMap<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.add("Content-Type", "application/json");
     }
 
     /**
@@ -72,11 +78,16 @@ public class AaiQuery4Ccvpn {
         Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_LINK_QUERY, params)
                 + (status == null ? "" : String.format("&operational-status=%s", status)));
         JSONObject linkInfo = getInfo(response.readEntity(String.class), "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.getJSONArray("relationship-data"), "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}
@@ -86,42 +97,52 @@ public class AaiQuery4Ccvpn {
      * @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) {
+    public JSONObject getServiceInstance(String networkId, String pnfName, String ifName, String status) {
         try {
             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.getJSONArray("relationship-data"),
                     "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.getJSONArray("relationship-data"),
-                    "connectivity. connectivity-id");
+                    "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.getString("related-link");
-            serviceInstancePath = serviceInstancePath.substring(0, serviceInstancePath.lastIndexOf('/'));
 
-            String[] params = new String[2];
+            Response response = get(getHostAddr(), getPath(serviceInstancePath));
+            JSONObject instance = JSON.parseObject(response.readEntity(String.class));
 
-            Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)/service-subscriptions/service-subscription/(.+)");
+            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);
             }
-
-            Response response = get(getHostAddr(), getPath(serviceInstancePath));
-            JSONArray instances = getInstances(response.readEntity(String.class));
-            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"));
-                String inputParams = JSONObject.parseObject(res.readEntity(String.class)).getString("input-parameters");
-                instance.put("input-parameters", inputParams);
-                instance.put("globalSubscriberId", params[0]);
-                instance.put("serviceType", params[1]);
-            }
-
-            return instances;
+            instance.put("globalSubscriberId", params[0]);
+            instance.put("serviceType", params[1]);
+            return instance;
         } catch (CorrelationException e) {
             throw new RuntimeException(e.getMessage(), e);
         }
@@ -133,12 +154,19 @@ public class AaiQuery4Ccvpn {
         params.put("networkId", networkId);
         params.put("pnfName", pnfName);
         params.put("ifName", ifName);
-        patch(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, params), body);
+        Response r = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, params));
+        JSONObject jsonObject = JSONObject.parseObject(r.readEntity(String.class));
+        body.put("resource-version", jsonObject.get("resource-version").toString());
+
+        put(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, params), body);
     }
 
     public void updateLogicLinkStatus(String linkName, Map<String, Object> body) throws CorrelationException {
-        patch(getHostAddr(),
-                getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, "linkName", linkName), body);
+        Response r = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_LINK_UPDATE, "linkName", linkName));
+        JSONObject jsonObject = JSONObject.parseObject(r.readEntity(String.class));
+        body.put("resource-version", jsonObject.get("resource-version").toString());
+        body.put("link-type", jsonObject.get("link-type").toString());
+        put(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_LINK_UPDATE, "linkName", linkName), body);
     }
 
     private JSONObject getVpnBindingInfo(String networkId, String pnfName,
@@ -163,18 +191,18 @@ public class AaiQuery4Ccvpn {
         return getInfo(response.readEntity(String.class), "connectivity", "service-instance");
     }
 
-    private JSONArray getServiceInstances(String globalCustomerId, String serviceType) throws CorrelationException {
+    private JSONObject getServiceInstance(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));
-        return getInstances(response.readEntity(String.class));
+        return JSON.parseObject(response.readEntity(String.class));
     }
 
     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;
     }
@@ -213,14 +241,14 @@ public class AaiQuery4Ccvpn {
         }
     }
 
-    private void patch(String host, String path, Map<String, Object> body) throws CorrelationException {
+    private void put(String host, String path, Map<String, Object> body) throws CorrelationException {
         Client client = ClientBuilder.newClient();
         WebTarget target = client.target(host).path(path);
         try {
-            Response response = target.request().headers(getAaiHeaders()).build("PATCH", Entity.json(body))
+            Response response = target.request().headers(getAaiHeaders()).build("PUT", Entity.json(body))
                     .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true).invoke();
             if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
-                throw new CorrelationException("Failed to connecto to AAI. \nCause: "
+                throw new CorrelationException("Failed to connect to AAI. \nCause: "
                         + response.getStatusInfo().getReasonPhrase() + "\nDetails: \n"
                         + getErrorMsg(String.format("%s%s", host, path), body, response));
             }
@@ -233,8 +261,11 @@ public class AaiQuery4Ccvpn {
     }
 
     private JSONObject getInfo(String response, String pField, String field) {
-        JSONArray results = extractJsonArray(JSONObject.parseObject(response), "results");
-        JSONObject pInterface = extractJsonObject(results.getJSONObject(0), pField);
+        JSONObject jObject = JSONObject.parseObject(response);
+        JSONObject pInterface = extractJsonObject(jObject, pField);
+        if (pInterface == null) {
+            pInterface = jObject;
+        }
         JSONObject relationshipList = extractJsonObject(pInterface, "relationship-list");
         JSONArray relationShip = extractJsonArray(relationshipList, "relationship");
         if (relationShip != null) {
@@ -248,14 +279,6 @@ public class AaiQuery4Ccvpn {
         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);
@@ -279,10 +302,12 @@ public class AaiQuery4Ccvpn {
     }
 
     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");
+        if (relationshipData != null) {
+            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;