Update snapshot and/or references of policy/models to latest snapshots
[policy/models.git] / models-interactions / model-simulators / src / main / java / org / onap / policy / simulators / AaiSimulatorJaxRs.java
index 8c96ee5..146dcec 100644 (file)
@@ -2,8 +2,9 @@
  * ============LICENSE_START=======================================================
  * simulators
  * ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2017-2018, 2020-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019, 2023 Nordix Foundation.
+ * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.simulators;
 
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.util.UUID;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
-import javax.ws.rs.PUT;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.MediaType;
-import org.apache.commons.io.IOUtils;
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.PUT;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.PathParam;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.QueryParam;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.common.utils.services.Registry;
 
 @Path("/aai")
 public class AaiSimulatorJaxRs {
 
-    private static final String DISABLE_CLOSEDLOOP = "disableClosedLoop";
-    private static final String ERROR = "error";
-    private static final String GETFAIL = "getFail";
+    private static final String DOT_JSON = ".json";
+    private static final String DEFAULT_RESOURCE_LOCATION = "org/onap/policy/simulators/aai/";
+    private static final String INVALID_VNF_FILE_NAME = "invalid-vnf";
+    private static final String INVALID_PNF_FILE_NAME = "invalid-pnf";
 
     /**
      * A&AI get query.
      *
-     * @param vnfId the VNF Id
      * @return the result
      */
     @GET
-    @Path("/v8/network/generic-vnfs/generic-vnf/{vnfId}")
-    @Consumes(MediaType.APPLICATION_JSON)
-    @Produces("application/json")
-    public String aaiGetQuery(@PathParam("vnfID") final String vnfId) {
-        return "{\"relationship-list\": {\"relationship\":[{\"related-to-property\": [{\"property-key\": "
-            + "\"service-instance.service-instance-name\"}]},{\"related-to-property\": [ {\"property-key\": "
-            + "\"vserver.vserver-name\",\"property-value\": \"USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
-    }
-
-    /**
-     * A&AI get query.
-     *
-     * @return the result
-     */
-    @GET
-    @Path("/v16/search/nodes-query")
+    @Path("/{version:v16|v21}/search/nodes-query")
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces("application/json")
     public String aaiGetVserverQuery(@QueryParam("filter") final String filter) {
         if (filter.equals("vserver-name:EQUALS:f953c499-4b1e-426b-8c6d-e9e9f1fc730f")
+            || filter.equals("vserver-name:EQUALS:Ete_vFWCLvFWSNK_7ba1fbde_0")
             || filter.equals("vserver-name:EQUALS:OzVServer")
             || filter.equals("vserver-name:EQUALS:testVserverName")) {
             return "{\"result-data\":[{\"resource-type\": \"vserver\",\"resource-link\":\"/aai/v15/"
@@ -84,173 +70,85 @@ public class AaiSimulatorJaxRs {
      *
      * @param req the request
      * @return the response
-     * @throws IOException if a response file cannot be read
      */
     @PUT
-    @Path("/v16/query")
+    @Path("/{version:v16|v21}/query")
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces("application/json")
-    public String aaiPutQuery(final String req) throws IOException {
-        return IOUtils.toString(getClass().getResource("aai/AaiCqResponse.json"),
-            StandardCharsets.UTF_8);
+    public Response aaiPutQuery(final String req) {
+        return getResponse("AaiCqResponse", "invalid-cq");
     }
 
     /**
-     * Get by VNF name.
+     * A&AI get PNF query using pnfName.
      *
-     * @param vnfName the VNF name
-     * @return the response
+     * @return the result
      */
     @GET
-    @Path("/v11/network/generic-vnfs/generic-vnf")
+    @Path("/{version:v16|v21}/network/pnfs/pnf/{pnfName}")
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces("application/json")
-    public String getByVnfName(@QueryParam("vnf-name") final String vnfName) {
-        if (GETFAIL.equals(vnfName)) {
-            return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not"
-                + " found for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"network/generic-vnfs/"
-                + "generic-vnf\",\"Node Not Found:No Node of type generic-vnf found at network/generic-vnfs"
-                + "/generic-vnf\",\"ERR.5.4.6114\"]}}}";
-        }
-        final boolean isDisabled = DISABLE_CLOSEDLOOP.equals(vnfName);
-        if (ERROR.equals(vnfName)) {
-            return "{ \"vnf-id\": \"error\", \"vnf-name\": \"" + vnfName
-                + "\", \"vnf-type\": \"RT\", \"service-id\": \"d7bb0a21-66f2-4e6d-87d9-9ef3ced63ae4\", \""
-                + "equipment-role\": \"UCPE\", \"orchestration-status\": \"created\", \"management-option\": \""
-                + "ATT\", \"ipv4-oam-address\": \"32.40.68.35\", \"ipv4-loopback0-address\": \"32.40.64.57\", \""
-                + "nm-lan-v6-address\": \"2001:1890:e00e:fffe::1345\", \"management-v6-address\": \""
-                + "2001:1890:e00e:fffd::36\", \"in-maint\": false, \"prov-status\":\"ACTIVE\", "
-                + "\"is-closed-loop-disabled\": " + isDisabled
-                + ", \"resource-version\": \"1493389458092\", \"relationship-list\": {\"relationship\":[{ \""
-                + "related-to\": \"service-instance\", \"related-link\": \"/aai/v11/business/customers/customer/"
-                + "1610_Func_Global_20160817084727/service-subscriptions/service-subscription/uCPE-VMS/"
-                + "service-instances/service-instance/USUCP0PCOIL0110UJZZ01\", \"relationship-data\":[{ \""
-                + "relationship-key\": \"customer.global-customer-id\", \"relationship-value\": \""
-                + "1610_Func_Global_20160817084727\"},{ \"relationship-key\": \"service-subscription.service-type"
-                + "\", \"relationship-value\": \"uCPE-VMS\"},{ \"relationship-key\": \""
-                + "service-instance.service-instance-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01"
-                + "\"} ], \"related-to-property\": [{\"property-key\": \"service-instance.service-instance-name"
-                + "\"}]},{ \"related-to\": \"vserver\", \"related-link\": \"/aai/v11/cloud-infrastructure/"
-                + "cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/"
-                + "USUCP0PCOIL0110UJZZ01%3A%3AuCPE-VMS/vservers/vserver/3b2558f4-39d8-40e7-bfc7-30660fb52c45"
-                + "\", \"relationship-data\":[{ \"relationship-key\": \"cloud-region.cloud-owner\", \""
-                + "relationship-value\": \"att-aic\"},{ \"relationship-key\": \"cloud-region.cloud-region-id"
-                + "\", \"relationship-value\": \"AAIAIC25\"},{ \"relationship-key\": \"tenant.tenant-id"
-                + "\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01::uCPE-VMS\"},{ \"relationship-key\": \""
-                + "vserver.vserver-id\", \"relationship-value\": \"3b2558f4-39d8-40e7-bfc7-30660fb52c45\"} ], \""
-                + "related-to-property\": [ {\"property-key\": \"vserver.vserver-name\",\"property-value\": \""
-                + "USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
+    public Response aaiGetPnfUsingPnfName(@PathParam("pnfName") final String pnfName) {
+        return getResponse(pnfName, INVALID_PNF_FILE_NAME);
+    }
 
-        }
-        final String vnfId = getUuidValue(vnfName, "5e49ca06-2972-4532-9ed4-6d071588d792");
-        return "{ \"vnf-id\": \"" + vnfId + "\", \"vnf-name\": \"" + vnfName
-            + "\", \"vnf-type\": \"RT\", \"service-id\": \"d7bb0a21-66f2-4e6d-87d9-9ef3ced63ae4\", \""
-            + "equipment-role\": \"UCPE\", \"orchestration-status\": \"created\", \"management-option\": \"ATT"
-            + "\", \"ipv4-oam-address\": \"32.40.68.35\", \"ipv4-loopback0-address\": \"32.40.64.57\", \""
-            + "nm-lan-v6-address\": \"2001:1890:e00e:fffe::1345\", \"management-v6-address\": \""
-            + "2001:1890:e00e:fffd::36\", \"in-maint\": false, \"prov-status\":\"ACTIVE\", "
-            + "\"is-closed-loop-disabled\": " + isDisabled
-            + ", \"resource-version\": \"1493389458092\", \"relationship-list\": {\"relationship\":[{ \""
-            + "related-to\": \"service-instance\", \"related-link\": \"/aai/v11/business/customers/customer"
-            + "/1610_Func_Global_20160817084727/service-subscriptions/service-subscription/uCPE-VMS/"
-            + "service-instances/service-instance/USUCP0PCOIL0110UJZZ01\", \"relationship-data\":[{ \""
-            + "relationship-key\": \"customer.global-customer-id\", \"relationship-value\": \""
-            + "1610_Func_Global_20160817084727\"},{ \"relationship-key\": \"service-subscription.service-type"
-            + "\", \"relationship-value\": \"uCPE-VMS\"},{ \"relationship-key\": \""
-            + "service-instance.service-instance-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01\"} ], \""
-            + "related-to-property\": [{\"property-key\": \"service-instance.service-instance-name\"}]},{ \""
-            + "related-to\": \"vserver\", \"related-link\": \"/aai/v11/cloud-infrastructure/cloud-regions/"
-            + "cloud-region/att-aic/AAIAIC25/tenants/tenant/USUCP0PCOIL0110UJZZ01%3A%3AuCPE-VMS/vservers/vserver"
-            + "/3b2558f4-39d8-40e7-bfc7-30660fb52c45\", \"relationship-data\":[{ \"relationship-key\": \""
-            + "cloud-region.cloud-owner\", \"relationship-value\": \"att-aic\"},{ \"relationship-key\": \""
-            + "cloud-region.cloud-region-id\", \"relationship-value\": \"AAIAIC25\"},{ \"relationship-key\": \""
-            + "tenant.tenant-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01::uCPE-VMS\"},{ \""
-            + "relationship-key\": \"vserver.vserver-id\", \"relationship-value\": \""
-            + "3b2558f4-39d8-40e7-bfc7-30660fb52c45\"} ], \"related-to-property\": [ {\"property-key\": \""
-            + "vserver.vserver-name\",\"property-value\": \"USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
+    /**
+     * A&AI get PNF query using pnf-id.
+     *
+     * @return the result
+     */
+    @GET
+    @Path("/{version:v16|v21}/network/pnfs/pnf")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces("application/json")
+    public Response aaiGetPnfUsingPnfId(@QueryParam("pnf-id") final String pnfId) {
+        return getResponse(pnfId, INVALID_PNF_FILE_NAME);
     }
 
     /**
-     * Get by VNF Id.
+     * A&AI get VNF query using vnf-id.
      *
-     * @param vnfId the VNF Id
-     * @return the response
+     * @return the result
      */
     @GET
-    @Path("/v11/network/generic-vnfs/generic-vnf/{vnfId}")
+    @Path("/{version:v16|v21}/network/generic-vnfs/generic-vnf/{vnfId}")
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces("application/json")
-    public String getByVnfId(@PathParam("vnfId") final String vnfId) {
-        if (GETFAIL.equals(vnfId)) {
-            return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not found"
-                + " for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"network/generic-vnfs/"
-                + "generic-vnf/getFail\",\"Node Not Found:No Node of type generic-vnf found at network/"
-                + "generic-vnfs/generic-vnf/getFail\",\"ERR.5.4.6114\"]}}}";
-        }
-        final boolean isDisabled = DISABLE_CLOSEDLOOP.equals(vnfId);
-        final String vnfName = getUuidValue(vnfId, "USUCP0PCOIL0110UJRT01");
-        return "{ \"vnf-id\": \"" + vnfId + "\", \"vnf-name\": \"" + vnfName
-            + "\", \"vnf-type\": \"RT\", \"service-id\": \""
-            + "d7bb0a21-66f2-4e6d-87d9-9ef3ced63ae4\", \"equipment-role\": \"UCPE\", \"orchestration-status"
-            + "\": \"created\", \"management-option\": \"ATT\", \"ipv4-oam-address\": \"32.40.68.35\", \""
-            + "ipv4-loopback0-address\": \"32.40.64.57\", \"nm-lan-v6-address\": \"2001:1890:e00e:fffe::1345"
-            + "\", \"management-v6-address\": \"2001:1890:e00e:fffd::36\", \"in-maint\": false, "
-            + "\"prov-status\":\"ACTIVE\", \"" + "" + "is-closed-loop-disabled\": " + isDisabled
-            + ", \"resource-version\": \"1493389458092\", \""
-            + "relationship-list\": {\"relationship\":[{ \"related-to\": \"service-instance\", \"related-link"
-            + "\": \"/aai/v11/business/customers/customer/1610_Func_Global_20160817084727/service-subscriptions"
-            + "/service-subscription/uCPE-VMS/service-instances/service-instance/USUCP0PCOIL0110UJZZ01\", \""
-            + "relationship-data\":[{ \"relationship-key\": \"customer.global-customer-id\", \""
-            + "relationship-value\": \"1610_Func_Global_20160817084727\"},{ \"relationship-key\": \""
-            + "service-subscription.service-type\", \"relationship-value\": \"uCPE-VMS\"},{ \"relationship-key"
-            + "\": \"service-instance.service-instance-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01\"} "
-            + "], \"related-to-property\": [{\"property-key\": \"service-instance.service-instance-name\"}]},"
-            + "{ \"related-to\": \"vserver\", \"related-link\": \"/aai/v11/cloud-infrastructure/cloud-regions/"
-            + "cloud-region/att-aic/AAIAIC25/tenants/tenant/USUCP0PCOIL0110UJZZ01%3A%3AuCPE-VMS/vservers/vserver"
-            + "/3b2558f4-39d8-40e7-bfc7-30660fb52c45\", \"relationship-data\":[{ \"relationship-key\": \""
-            + "cloud-region.cloud-owner\", \"relationship-value\": \"att-aic\"},{ \"relationship-key\": \""
-            + "cloud-region.cloud-region-id\", \"relationship-value\": \"AAIAIC25\"},{ \"relationship-key\": \""
-            + "tenant.tenant-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01::uCPE-VMS\"},{ \""
-            + "relationship-key\": \"vserver.vserver-id\", \"relationship-value\": \""
-            + "3b2558f4-39d8-40e7-bfc7-30660fb52c45\"} ], \"related-to-property\": [ {\"property-key\": \""
-            + "vserver.vserver-name\",\"property-value\": \"USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
+    public Response aaiGetVnfUsingVnfId(@PathParam("vnfId") final String vnfId) {
+        return getResponse(vnfId, INVALID_VNF_FILE_NAME);
     }
 
     /**
-     * Get by VServer name.
+     * A&AI get VNF query using vnf-name.
      *
-     * @param vserverName the VServer name
-     * @return the response
+     * @return the result
      */
     @GET
-    @Path("/v11/nodes/vservers")
+    @Path("/{version:v16|v21}/network/generic-vnfs/generic-vnf")
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces("application/json")
-    public String getByVserverName(@QueryParam("vserver-name") final String vserverName) {
-        if (GETFAIL.equals(vserverName)) {
-            return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not found"
-                + " for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"nodes/vservers\",\"Node Not"
-                + " Found:No Node of type generic-vnf found at nodes/vservers\",\"ERR.5.4.6114\"]}}}";
-        }
-        final boolean isDisabled = DISABLE_CLOSEDLOOP.equals(vserverName);
-        final String vserverId = getUuidValue(vserverName, "d0668d4f-c25e-4a1b-87c4-83845c01efd8");
-        return "{\"vserver\": [{ \"vserver-id\": \"" + vserverId + "\", \"vserver-name\": \""
-            + vserverName
-            + "\", \"vserver-name2\": \"vjunos0\", \"vserver-selflink\": \"https://aai-ext1.test.att.com:8443/aai/v7/cloud-infrastructure/cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/USMSO1SX7NJ0103UJZZ01%3A%3AuCPE-VMS/vservers/vserver/d0668d4f-c25e-4a1b-87c4-83845c01efd8\", \"in-maint\": false, \"is-closed-loop-disabled\": "
-            + isDisabled + ", \"prov-status\":\"ACTIVE\", \"resource-version\": \"1494001931513\", "
-            + "\"relationship-list\": {\"relationship\":[{ \"related-to"
-            + "\": \"generic-vnf\", \"related-link\": \"/aai/v11/network/generic-vnfs/generic-vnf/"
-            + "e1a41e99-4ede-409a-8f9d-b5e12984203a\", \"relationship-data\": [ {\"relationship-key\": \""
-            + "generic-vnf.vnf-id\",\"relationship-value\": \"e1a41e99-4ede-409a-8f9d-b5e12984203a\" }], \""
-            + "related-to-property\": [ {\"property-key\": \"generic-vnf.vnf-name\",\"property-value\": \""
-            + "USMSO1SX7NJ0103UJSW01\" }]},{ \"related-to\": \"pserver\", \"related-link\": \"/aai/v11/"
-            + "cloud-infrastructure/pservers/pserver/USMSO1SX7NJ0103UJZZ01\", \"relationship-data\": [ {\""
-            + "relationship-key\": \"pserver.hostname\",\"relationship-value\": \"USMSO1SX7NJ0103UJZZ01\" }], \""
-            + "related-to-property\": [{\"property-key\": \"pserver.pserver-name2\"}]} ]}}]}";
+    public Response aaiGetVnfUsingVnfName(@QueryParam("vnf-name") final String vnfName) {
+        return getResponse(vnfName, INVALID_VNF_FILE_NAME);
     }
 
-    private String getUuidValue(final String value, final String defaultValue) {
-        return value != null ? UUID.nameUUIDFromBytes(value.getBytes()).toString() : defaultValue;
+    private Response getResponse(final String expectedFileName, final String defaultFileName) {
+        String resourceLocation = getResourceLocation();
+        var responseString = ResourceUtils.getResourceAsString(resourceLocation + expectedFileName + DOT_JSON);
+        if (null == responseString) {
+            // if a response file is not found in expected location, look for it in default location
+            responseString = ResourceUtils.getResourceAsString(DEFAULT_RESOURCE_LOCATION + expectedFileName + DOT_JSON);
+        }
+        if (null != responseString) {
+            return Response.ok(responseString).build();
+        } else {
+            // if a response file is not available in expected or default location, return an appropriate 404 response
+            responseString = ResourceUtils.getResourceAsString(DEFAULT_RESOURCE_LOCATION + defaultFileName + DOT_JSON);
+            return Response.status(Response.Status.NOT_FOUND).entity(responseString).build();
+        }
     }
 
+    private String getResourceLocation() {
+        return Registry.getOrDefault(this.getClass().getName() + "_RESOURCE_LOCATION", String.class,
+            DEFAULT_RESOURCE_LOCATION);
+    }
 }