[ONOS-7491] Change deletion API let app use hostname as input param

Change-Id: Iba2d929a0d308bd39ae9ba14ac39c235d3bc56c5
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeWebResource.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeWebResource.java
index e0f6e53..1c7d3eb 100644
--- a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeWebResource.java
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeWebResource.java
@@ -31,6 +31,7 @@
 import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
@@ -42,6 +43,7 @@
 
 import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
 import static javax.ws.rs.core.Response.created;
+import static org.onlab.util.Tools.nullIsIllegal;
 
 /**
  * Handles REST API call of openstack node config.
@@ -58,6 +60,9 @@
     private static final String NODE_ID = "NODE_ID";
     private static final String DELETE = "DELETE";
 
+    private static final String HOST_NAME = "hostname";
+    private static final String ERROR_MESSAGE = " cannot be null";
+
     private final OpenstackNodeAdminService osNodeAdminService =
                                             get(OpenstackNodeAdminService.class);
     private final OpenstackNodeService osNodeService =
@@ -99,7 +104,7 @@
      *
      * @param input openstack nodes JSON input stream
      * @return 200 OK with the updated openstack node's config, 400 BAD_REQUEST
-     * if the JSON is malformed
+     * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
      * @onos.rsModel OpenstackNode
      */
     @PUT
@@ -125,28 +130,29 @@
     /**
      * Removes a set of openstack nodes' config from the JSON input stream.
      *
-     * @param input openstack nodes JSON input stream
-     * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed
+     * @param hostname host name contained in openstack nodes configuration
+     * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and
+     * 304 NOT_MODIFIED without the updated config
      * @onos.rsModel OpenstackNode
      */
     @DELETE
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.APPLICATION_JSON)
-    public Response deleteNodes(InputStream input) {
+    @Path("{hostname}")
+    public Response deleteNodes(@PathParam("hostname") String hostname) {
         log.trace(String.format(MESSAGE_NODE, DELETE));
 
-        Set<OpenstackNode> nodes = readNodeConfiguration(input);
-        for (OpenstackNode osNode: nodes) {
-            OpenstackNode existing = osNodeService.node(osNode.hostname());
-            if (existing == null) {
-                log.warn("There is no node configuration to delete : {}", osNode.hostname());
-                return Response.notModified().build();
-            } else {
-                osNodeAdminService.removeNode(osNode.hostname());
-            }
+        OpenstackNode existing =
+                osNodeService.node(nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE));
+
+        if (existing == null) {
+            log.warn("There is no node configuration to delete : {}", hostname);
+            return Response.notModified().build();
+        } else {
+            osNodeAdminService.removeNode(hostname);
         }
 
-        return Response.ok().build();
+        return Response.noContent().build();
     }
 
     private Set<OpenstackNode> readNodeConfiguration(InputStream input) {
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/web/OpenstackNodeWebResourceTest.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/web/OpenstackNodeWebResourceTest.java
index ed8cd9a..5ed0f1d 100644
--- a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/web/OpenstackNodeWebResourceTest.java
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/web/OpenstackNodeWebResourceTest.java
@@ -94,7 +94,7 @@
     }
 
     /**
-     * Tests the results of the REST API POST with creating new nodes operation.
+     * Tests the results of the REST API POST method with creating new nodes operation.
      */
     @Test
     public void testCreateNodesWithCreateOperation() {
@@ -118,7 +118,7 @@
     }
 
     /**
-     * Tests the results of the REST API POST without creating new nodes operation.
+     * Tests the results of the REST API POST method without creating new nodes operation.
      */
     @Test
     public void testCreateNodesWithoutCreateOperation() {
@@ -138,7 +138,7 @@
     }
 
     /**
-     * Tests the results of the REST API PUT with modifying the nodes.
+     * Tests the results of the REST API PUT method with modifying the nodes.
      */
     @Test
     public void testUpdateNodesWithoutModifyOperation() {
@@ -162,7 +162,7 @@
     }
 
     /**
-     * Tests the results of the REST API PUT without modifying the nodes.
+     * Tests the results of the REST API PUT method without modifying the nodes.
      */
     @Test
     public void testUpdateNodesWithModifyOperation() {
@@ -180,4 +180,50 @@
 
         verify(mockOpenstackNodeService);
     }
+
+    /**
+     * Tests the results of the REST API DELETE method with deleting the nodes.
+     */
+    @Test
+    public void testDeleteNodesWithDeletionOperation() {
+        expect(mockOpenstackNodeService.node(anyString())).andReturn(openstackNode).once();
+        replay(mockOpenstackNodeService);
+
+        expect(mockOpenstackNodeAdminService.removeNode(anyString())).andReturn(openstackNode).once();
+        replay(mockOpenstackNodeAdminService);
+
+        String location = PATH + "/gateway-node";
+
+        final WebTarget wt = target();
+        Response response = wt.path(location).request(
+                            MediaType.APPLICATION_JSON_TYPE).delete();
+
+        final int status = response.getStatus();
+
+        assertThat(status, is(204));
+
+        verify(mockOpenstackNodeService);
+        verify(mockOpenstackNodeAdminService);
+    }
+
+    /**
+     * Tests the results of the REST API DELETE method without deleting the nodes.
+     */
+    @Test
+    public void testDeleteNodesWithoutDeletionOperation() {
+        expect(mockOpenstackNodeService.node(anyString())).andReturn(null).once();
+        replay(mockOpenstackNodeService);
+
+        String location = PATH + "/gateway-node";
+
+        final WebTarget wt = target();
+        Response response = wt.path(location).request(
+                MediaType.APPLICATION_JSON_TYPE).delete();
+
+        final int status = response.getStatus();
+
+        assertThat(status, is(304));
+
+        verify(mockOpenstackNodeService);
+    }
 }