VSE: Delete a JSON Object
[cps.git] / cps / cps-rest / src / main / java / org / onap / cps / rest / controller / RestController.java
index 68d101f..e86d329 100644 (file)
@@ -25,6 +25,7 @@ import java.io.File;
 import java.io.IOException;
 import javax.persistence.PersistenceException;
 import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
@@ -38,6 +39,7 @@ import org.onap.cps.api.CpService;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.EmptyResultDataAccessException;
 
 
 
@@ -111,6 +113,25 @@ public class RestController {
         }
     }
 
+    /**
+     * Delete a JSON Object using the object identifier.
+     *
+     * @param jsonObjectId the JSON object identifier.
+     * @return a HTTP response.
+     */
+    @DELETE
+    @Path("json-object/{id}")
+    public final Response deleteJsonObjectById(@PathParam("id") int jsonObjectId) {
+        try {
+            cpService.deleteJsonById(jsonObjectId);
+            return Response.status(Status.OK).entity(Status.OK.toString()).build();
+        } catch (final EmptyResultDataAccessException e) {
+            return Response.status(Status.NOT_FOUND).entity(Status.NOT_FOUND.toString()).build();
+        } catch (final Exception e) {
+            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
+        }
+    }
+
     private static final void validateJsonStructure(final String jsonFile) {
         final Gson gson = new Gson();
         gson.fromJson(jsonFile, Object.class);