Changing variable types for api
[cps.git] / cps / cps-rest / src / main / java / org / onap / cps / rest / controller / RestController.java
index a64cd6a..d3ff91b 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Nordix Foundation
+ *  Modifications Copyright (C) 2020 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.
@@ -22,46 +23,109 @@ package org.onap.cps.rest.controller;
 import com.google.gson.Gson;
 import com.google.gson.JsonSyntaxException;
 import java.io.File;
-import java.io.IOException;
+import javax.persistence.PersistenceException;
+import javax.validation.Valid;
 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;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
+import org.apache.cxf.jaxrs.ext.multipart.Attachment;
 import org.glassfish.jersey.media.multipart.FormDataParam;
+import org.hibernate.exception.ConstraintViolationException;
 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;
 
 @Path("cps")
-public class RestController {
+public class RestController implements CpsResourceApi {
 
     @Autowired
     private CpService cpService;
 
+    @Override
+    public Object createAnchor(final Attachment fileDetail, final String dataspaceName) {
+        return null;
+    }
+
+    @Override
+    public Object createModules(final Attachment fileDetail, final String dataspaceName) {
+        return null;
+    }
+
+    @Override
+    public Object createNode(final Attachment fileDetail, final String dataspaceName) {
+        return null;
+    }
+
+    @Override
+    public Object deleteAnchor(final String dataspaceName, final String anchorName) {
+        return null;
+    }
+
+    @Override
+    public Object deleteDataspace(final String dataspaceName) {
+        return null;
+    }
+
+    @Override
+    public Object getAnchor(final String dataspaceName, final String anchorName) {
+        return null;
+    }
+
+    @Override
+    public Object getAnchors(final String dataspaceName) {
+        return null;
+    }
+
+    @Override
+    public Object getModule(final String dataspaceName, final String namespaceName, final String revision) {
+        return null;
+    }
+
+    @Override
+    public Object getNode(@Valid final String body, final String dataspaceName) {
+        return null;
+    }
+
+    @Override
+    public Object getNodeByDataspaceAndAnchor(@Valid final String body, final String dataspaceName,
+        final String anchorName) {
+        return null;
+    }
+
+    /*
+    Old rest endpoints before contract first approach (Need to be removed).
+     */
+
     /**
      * Upload a yang model file.
      *
      * @param uploadedFile the yang model file.
+     * @param dataspaceName the dataspace name linked to the model.
      * @return a http response code.
      */
     @POST
-    @Path("upload-yang-model-file")
+    @Path("/dataspaces/{dataspace_name}/modules")
     @Produces(MediaType.APPLICATION_JSON)
     @Consumes(MediaType.MULTIPART_FORM_DATA)
-    public final Response uploadYangModelFile(@FormDataParam("file") File uploadedFile) throws IOException {
+    public final Response uploadYangModelFile(@FormDataParam("file") File uploadedFile,
+        @PathParam("dataspace_name") String dataspaceName) {
         try {
             final File fileToParse = renameFileIfNeeded(uploadedFile);
             final SchemaContext schemaContext = cpService.parseAndValidateModel(fileToParse);
-            cpService.storeSchemaContext(schemaContext);
-            return Response.status(Status.OK).entity("Yang File Parsed").build();
-        } catch (YangParserException e) {
+            cpService.storeSchemaContext(schemaContext, dataspaceName);
+            return Response.status(Status.CREATED).entity("Resource successfully created").build();
+        } catch (final YangParserException | ConstraintViolationException e) {
             return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
-        } catch (Exception e) {
+        } catch (final Exception e) {
             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
         }
     }
@@ -73,18 +137,55 @@ public class RestController {
      * @return a http response code.
      */
     @POST
-    @Path("upload-yang-json-data-file")
+    @Path("/upload-yang-json-data-file")
     @Produces(MediaType.APPLICATION_JSON)
     @Consumes(MediaType.MULTIPART_FORM_DATA)
-    public final Response uploadYangJsonDataFile(@FormDataParam("file") String uploadedFile) {
+    public final Response uploadYangJsonDataFile(@FormDataParam("file") final String uploadedFile) {
         try {
             validateJsonStructure(uploadedFile);
             final int persistenceObjectId = cpService.storeJsonStructure(uploadedFile);
             return Response.status(Status.OK).entity("Object stored in CPS with identity: " + persistenceObjectId)
                 .build();
-        } catch (JsonSyntaxException e) {
+        } catch (final JsonSyntaxException e) {
             return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
-        } catch (Exception e) {
+        } catch (final Exception e) {
+            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
+        }
+    }
+
+    /**
+     * Read a JSON Object using the object identifier.
+     *
+     * @param jsonObjectId the JSON object identifier.
+     * @return a HTTP response.
+     */
+    @GET
+    @Path("/json-object/{id}")
+    public final Response getJsonObjectById(@PathParam("id") final int jsonObjectId) {
+        try {
+            return Response.status(Status.OK).entity(cpService.getJsonById(jsonObjectId)).build();
+        } catch (final PersistenceException e) {
+            return Response.status(Status.NOT_FOUND).entity(e.getMessage()).build();
+        } catch (final Exception e) {
+            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
+        }
+    }
+
+    /**
+     * 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") final 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();
         }
     }
@@ -94,7 +195,7 @@ public class RestController {
         gson.fromJson(jsonFile, Object.class);
     }
 
-    private static final File renameFileIfNeeded(File originalFile) {
+    private static final File renameFileIfNeeded(final File originalFile) {
         if (originalFile.getName().endsWith(".yang")) {
             return originalFile;
         }
@@ -102,8 +203,4 @@ public class RestController {
         originalFile.renameTo(renamedFile);
         return renamedFile;
     }
-}
-
-
-
-
+}
\ No newline at end of file