Fixed HTTP PATCH Failures
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / AaiQuery4Ccvpn.java
index 42b7bd7..0e444c1 100644 (file)
@@ -17,7 +17,6 @@ package org.onap.holmes.common.aai;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import lombok.extern.slf4j.Slf4j;
 import org.onap.holmes.common.aai.config.AaiConfig;
 import org.onap.holmes.common.config.MicroServiceConfig;
 import org.onap.holmes.common.exception.CorrelationException;
@@ -29,17 +28,21 @@ 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;
 
-@Slf4j
 public class AaiQuery4Ccvpn {
 
     private MultivaluedMap<String, Object> headers;
 
-    public AaiQuery4Ccvpn() {
+    static public AaiQuery4Ccvpn newInstance() {
+        return new AaiQuery4Ccvpn();
+    }
+
+    private AaiQuery4Ccvpn() {
         headers = new MultivaluedHashMap<>();
         headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
         headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
@@ -47,13 +50,25 @@ public class AaiQuery4Ccvpn {
         headers.add("Accept", "application/json");
     }
 
+    /**
+     * Query the logic link information for AAI. This method is based on the API:
+     * https://<AAI host>:<AAI port>/aai/v14/network/network-resources/network-resource/{networkId}/pnfs/pnf/{pnfName}/p-interfaces?interface-name={ifName}&operational-status={status}
+     * provided by AAI.
+     *
+     * @param networkId
+     * @param pnfName
+     * @param ifName
+     * @param status
+     * @return the ID of the logic link
+     */
     public String getLogicLink(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_LINK_UPDATE, params));
+
+        Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.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());
@@ -63,6 +78,19 @@ public class AaiQuery4Ccvpn {
         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:
+     * 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}
+     * provided by AAI. The path for getting the required instance information is: p-interface → vpn-vpnbinding → connectivity → service instance
+     *
+     * @param networkId
+     * @param pnfName
+     * @param ifName
+     * @param status
+     * @return all related service instances in JSONArray format
+     */
     public JSONArray getServiceInstances(String networkId, String pnfName, String ifName, String status) {
         try {
             JSONObject vpnBindingInfo = getVpnBindingInfo(networkId, pnfName, ifName, status);
@@ -75,12 +103,36 @@ public class AaiQuery4Ccvpn {
             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);
+            }
+
             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());
             }
-            return getInstances(JSONObject.toJSONString(response.getEntity()));
+            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]);
+            }
+
+            return instances;
         } catch (CorrelationException e) {
             throw new RuntimeException(e.getMessage(), e);
         }
@@ -108,8 +160,8 @@ public class AaiQuery4Ccvpn {
         }
     }
 
-    public JSONObject getVpnBindingInfo(String networkId, String pnfName,
-                                        String ifName, String status) throws CorrelationException {
+    private JSONObject getVpnBindingInfo(String networkId, String pnfName,
+                                         String ifName, String status) throws CorrelationException {
         Map<String, String> params = new HashMap();
         params.put("networkId", networkId);
         params.put("pnfName", pnfName);
@@ -123,7 +175,7 @@ public class AaiQuery4Ccvpn {
         return getInfo(JSONObject.toJSONString(response.getEntity()), "p-interface", "vpn-binding");
     }
 
-    public JSONObject getConnectivityInfo(String vpnId) throws CorrelationException {
+    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: "
@@ -132,7 +184,7 @@ public class AaiQuery4Ccvpn {
         return getInfo(JSONObject.toJSONString(response.getEntity()), "vpn-binding", "connectivity");
     }
 
-    public JSONObject getServiceInstanceByConn(String connectivityId) throws CorrelationException {
+    private JSONObject getServiceInstanceByConn(String connectivityId) throws CorrelationException {
         Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCE_ADDR_4_CCVPN,
                 "connectivityId", connectivityId));
         if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
@@ -142,7 +194,7 @@ public class AaiQuery4Ccvpn {
         return getInfo(JSONObject.toJSONString(response.getEntity()), "connectivity", "service-instance");
     }
 
-    public JSONArray getServiceInstances(String globalCustomerId, String serviceType) throws CorrelationException {
+    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);
@@ -177,16 +229,17 @@ public class AaiQuery4Ccvpn {
         return ret;
     }
 
-    public Response get(String host, String path) {
+    private Response get(String host, String path) {
         Client client = ClientBuilder.newClient();
         WebTarget target = client.target(host).path(path);
         return target.request().headers(getAaiHeaders()).get();
     }
 
-    public Response patch(String host, String path, Map<String, Object> body) {
+    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));
+        return target.request().headers(getAaiHeaders()).build("PATCH", Entity.json(body))
+                .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true).invoke();
     }
 
     private JSONObject getInfo(String response, String pField, String field) {