Change to not exception on delete when object 00/116600/1
authorBenjamin, Max (mb388a) <mb388a@att.com>
Tue, 5 Jan 2021 16:03:15 +0000 (11:03 -0500)
committerBenjamin, Max (mb388a) <mb388a@att.com>
Tue, 5 Jan 2021 16:03:15 +0000 (11:03 -0500)
Change to not exception on delete when object doesnt exist
Added a new deleteIfExist method to the graph client and unit test

Issue-ID: SO-3464
Signed-off-by: Benjamin, Max (mb388a) <mb388a@att.com>
Change-Id: If73721cd83b94bd5a1db1af26aba212b12236fe1

adapters/mso-openstack-adapters/src/main/java/org/onap/so/heatbridge/HeatBridgeImpl.java
graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryResourcesClient.java
graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIResourcesClientTest.java

index eb0529c..9b20139 100644 (file)
@@ -765,7 +765,7 @@ public class HeatBridgeImpl implements HeatBridgeApi {
                         if (env.getProperty("heatBridgeDryrun", Boolean.class, false)) {
                             logger.debug("Would delete Vserver: {}", vserverUri.build().toString());
                         } else {
-                            resourcesClient.delete(vserverUri);
+                            resourcesClient.deleteIfExists(vserverUri);
                         }
                     }
                 }
index 343e888..5dbe915 100644 (file)
@@ -42,9 +42,13 @@ import org.onap.aaiclient.client.graphinventory.entities.uri.HttpAwareUri;
 import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryMultipleItemsException;
 import org.onap.so.client.RestClient;
 import org.onap.so.client.RestProperties;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public abstract class GraphInventoryResourcesClient<Self, Uri extends GraphInventoryResourceUri<?, ?>, SingleUri extends GraphInventorySingleResourceUri<?, ?, ?, ?, ?, ?>, PluralUri extends GraphInventoryPluralResourceUri<?, ?>, EdgeLabel extends GraphInventoryEdgeLabel, Wrapper extends GraphInventoryResultWrapper, TransactionalClient, SingleTransactionClient> {
 
+    private static final Logger logger = LoggerFactory.getLogger(GraphInventoryResourcesClient.class);
+
     protected GraphInventoryClient client;
 
     protected GraphInventoryResourcesClient(GraphInventoryClient client) {
@@ -147,6 +151,27 @@ public abstract class GraphInventoryResourcesClient<Self, Uri extends GraphInven
         String resourceVersion = (String) result.get("resource-version");
         giRC = client.createClient(clone.resourceVersion(resourceVersion));
         giRC.delete();
+
+    }
+
+    /**
+     * Deletes object from GraphInventory only if exists. Automatically handles resource-version.
+     * 
+     * @param uri
+     * @return
+     */
+    public void deleteIfExists(SingleUri uri) {
+        GraphInventorySingleResourceUri<?, ?, ?, ?, ?, ?> clone = (SingleUri) uri.clone();
+        RestClient giRC = client.createClient(clone);
+        Optional<Map<String, Object>> result = giRC.get(new GenericType<Map<String, Object>>() {});
+        if (result.isPresent()) {
+            String resourceVersion = (String) result.get().get("resource-version");
+            giRC = client.createClient(clone.resourceVersion(resourceVersion));
+            giRC.delete();
+        } else {
+            logger.warn(clone.build() + " already does not exist in " + client.getGraphDBName()
+                    + " therefore delete call not executed");
+        }
     }
 
     /**
index 36ba1f3..03fd0ac 100644 (file)
@@ -107,6 +107,27 @@ public class AAIResourcesClientTest {
         client.delete(path);
     }
 
+    @Test
+    public void verifyDeleteIfExists() {
+        AAIResourceUri path = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf("test2"));
+        wireMockRule.stubFor(get(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
+                .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(404)));
+        AAIResourcesClient client = aaiClient;
+        client.deleteIfExists(path);
+    }
+
+    @Test
+    public void verifyDeleteIfExists_exists() {
+        AAIResourceUri path = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf("test2"));
+        wireMockRule.stubFor(get(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
+                .willReturn(aResponse().withHeader("Content-Type", "application/json")
+                        .withBodyFile("aai/resources/mockObject.json").withStatus(200)));
+        wireMockRule.stubFor(delete(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
+                .withQueryParam("resource-version", equalTo("1234")).willReturn(aResponse().withStatus(204)));
+        AAIResourcesClient client = aaiClient;
+        client.deleteIfExists(path);
+    }
+
     @Test
     public void verifyBasicAuth() {
         AAIResourceUri path = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf("test3"));