Store and Validate a JSON Object
authorniamhcore <niamh.core@est.tech>
Fri, 25 Sep 2020 13:53:25 +0000 (14:53 +0100)
committerniamhcore <niamh.core@est.tech>
Wed, 30 Sep 2020 10:57:50 +0000 (11:57 +0100)
Issue-ID: CCSDK-2757
Link: https://jira.onap.org/browse/CCSDK-2757
Change-Id: Icfdbaec1a853ba5ba4a22742fb3091fc71a11d71

Change-Id: I80777bb2692e6e7f7594f787ec480817e8df0a35

cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java
cps/cps-ri/src/main/java/org/onap/cps/spi/entities/JsonDataEntity.java [new file with mode: 0644]
cps/cps-ri/src/main/java/org/onap/cps/spi/entities/ModuleEntity.java [moved from cps/cps-ri/src/main/java/org/onap/cps/spi/impl/entities/ModuleEntity.java with 88% similarity]
cps/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java [new file with mode: 0644]
cps/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java
cps/cps-ri/src/main/java/org/onap/cps/spi/repository/DataRepository.java [new file with mode: 0644]
cps/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java
cps/cps-service/pom.xml
cps/cps-service/src/main/java/org/onap/cps/api/CpService.java
cps/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java
cps/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java [new file with mode: 0644]

index dc64110..a64cd6a 100644 (file)
@@ -19,6 +19,8 @@
 
 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.ws.rs.Consumes;
@@ -41,26 +43,62 @@ public class RestController {
     @Autowired
     private CpService cpService;
 
+    /**
+     * Upload a yang model file.
+     *
+     * @param uploadedFile the yang model file.
+     * @return a http response code.
+     */
     @POST
-    @Path("uploadYangFile")
+    @Path("upload-yang-model-file")
     @Produces(MediaType.APPLICATION_JSON)
     @Consumes(MediaType.MULTIPART_FORM_DATA)
-    public Response uploadFile(@FormDataParam("file") File uploadedFile) throws IOException {
+    public final Response uploadYangModelFile(@FormDataParam("file") File uploadedFile) throws IOException {
         try {
-            File fileToParse = renameFileIfNeeded(uploadedFile);
-            SchemaContext schemaContext = cpService.parseAndValidateModel(fileToParse);
+            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) {
             return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
+        } catch (Exception e) {
+            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
         }
     }
 
-    private static File renameFileIfNeeded(File originalFile) {
+    /**
+     * Upload a JSON file.
+     *
+     * @param uploadedFile the JSON file.
+     * @return a http response code.
+     */
+    @POST
+    @Path("upload-yang-json-data-file")
+    @Produces(MediaType.APPLICATION_JSON)
+    @Consumes(MediaType.MULTIPART_FORM_DATA)
+    public final Response uploadYangJsonDataFile(@FormDataParam("file") 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) {
+            return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
+        } catch (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);
+    }
+
+    private static final File renameFileIfNeeded(File originalFile) {
         if (originalFile.getName().endsWith(".yang")) {
             return originalFile;
         }
-        File renamedFile = new File(originalFile.getName() + ".yang");
+        final File renamedFile = new File(originalFile.getName() + ".yang");
         originalFile.renameTo(renamedFile);
         return renamedFile;
     }
diff --git a/cps/cps-ri/src/main/java/org/onap/cps/spi/entities/JsonDataEntity.java b/cps/cps-ri/src/main/java/org/onap/cps/spi/entities/JsonDataEntity.java
new file mode 100644 (file)
index 0000000..413362e
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.entities;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+/**
+ * Entity to store a JSON data structure.
+ */
+@Getter
+@Setter
+@Entity
+@AllArgsConstructor
+@NoArgsConstructor
+@Table(name = "JsonData")
+public class JsonDataEntity {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Integer id;
+
+    @Column
+    private String jsonStructure;
+
+    public JsonDataEntity(String jsonStructure) {
+        this.jsonStructure = jsonStructure;
+    }
+}
@@ -17,7 +17,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.spi.impl.entities;
+package org.onap.cps.spi.entities;
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
@@ -55,6 +55,13 @@ public class ModuleEntity {
     @Column
     private String revision;
 
+    /**
+     * Initialize a module entity.
+     *
+     * @param name the module name.
+     * @param moduleContent the module content.
+     * @param revision the revision number of the module.
+     */
     public ModuleEntity(String name, String moduleContent, String revision) {
         this.name = name;
         this.moduleContent = moduleContent;
diff --git a/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java b/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java
new file mode 100644 (file)
index 0000000..93746e0
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.impl;
+
+import org.onap.cps.spi.DataPersistencyService;
+import org.onap.cps.spi.entities.JsonDataEntity;
+import org.onap.cps.spi.repository.DataRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+
+@Component
+public class DataPersistencyServiceImpl implements DataPersistencyService {
+
+    @Autowired
+    private DataRepository dataRepository;
+
+    /**
+     * Method to store a JSON data structure in the database.
+     *
+     * @param jsonStructure the JSON data structure.
+     * @return
+     */
+    public final Integer storeJsonStructure(final String jsonStructure) {
+        final JsonDataEntity jsonDataEntity = new JsonDataEntity(jsonStructure);
+        dataRepository.save(jsonDataEntity);
+        return jsonDataEntity.getId();
+    }
+}
index 6e718c8..14085c7 100644 (file)
@@ -19,9 +19,8 @@
 
 package org.onap.cps.spi.impl;
 
-
-import org.onap.cps.spi.impl.entities.ModuleEntity;
 import org.onap.cps.spi.ModelPersistencyService;
+import org.onap.cps.spi.entities.ModuleEntity;
 import org.onap.cps.spi.repository.ModuleRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
diff --git a/cps/cps-ri/src/main/java/org/onap/cps/spi/repository/DataRepository.java b/cps/cps-ri/src/main/java/org/onap/cps/spi/repository/DataRepository.java
new file mode 100644 (file)
index 0000000..f3dd586
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.repository;
+
+import org.onap.cps.spi.entities.JsonDataEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface DataRepository extends JpaRepository<JsonDataEntity, Integer> {
+}
\ No newline at end of file
index 84441cb..0fe53b6 100644 (file)
@@ -20,7 +20,7 @@
 package org.onap.cps.spi.repository;
 
 
-import org.onap.cps.spi.impl.entities.ModuleEntity;
+import org.onap.cps.spi.entities.ModuleEntity;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Repository;
 
index 8445394..e0d7ebd 100644 (file)
       <artifactId>spring-context</artifactId>\r
     </dependency>\r
 \r
+    <dependency>\r
+      <!-- For parsing JSON object -->\r
+      <groupId>com.google.code.gson</groupId>\r
+      <artifactId>gson</artifactId>\r
+    </dependency>\r
+\r
   </dependencies>\r
 \r
 </project>
\ No newline at end of file
index a5ab485..cdd1c4d 100644 (file)
@@ -52,4 +52,11 @@ public interface CpService {
      */
     void storeSchemaContext(final SchemaContext schemaContext);
 
+    /**
+     * Store the JSON structure in the database.
+     *
+     * @param jsonStructure the JSON structure.
+     * @return entity ID.
+     */
+    Integer storeJsonStructure(final String jsonStructure);
 }
index b71e471..8d6b63b 100644 (file)
@@ -27,6 +27,7 @@ import java.io.IOException;
 import java.util.Iterator;
 import java.util.ServiceLoader;
 import org.onap.cps.api.CpService;
+import org.onap.cps.spi.DataPersistencyService;
 import org.onap.cps.spi.ModelPersistencyService;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
@@ -40,6 +41,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+
 @Component
 public class CpServiceImpl implements CpService {
 
@@ -59,8 +61,12 @@ public class CpServiceImpl implements CpService {
     @Autowired
     private ModelPersistencyService modelPersistencyService;
 
+    @Autowired
+    private DataPersistencyService dataPersistencyService;
+
+
     @Override
-    public SchemaContext parseAndValidateModel(final String yangModelContent)
+    public final SchemaContext parseAndValidateModel(final String yangModelContent)
         throws IOException, YangParserException {
         final File tempFile = File.createTempFile("yang", ".yang");
         try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) {
@@ -72,7 +78,7 @@ public class CpServiceImpl implements CpService {
     }
 
     @Override
-    public SchemaContext parseAndValidateModel(final File yangModelFile) throws IOException, YangParserException {
+    public final SchemaContext parseAndValidateModel(final File yangModelFile) throws IOException, YangParserException {
         final YangTextSchemaSource yangTextSchemaSource = YangTextSchemaSource.forFile(yangModelFile);
         final YangParser yangParser = PARSER_FACTORY.createParser(StatementParserMode.DEFAULT_MODE);
         yangParser.addSource(yangTextSchemaSource);
@@ -80,7 +86,12 @@ public class CpServiceImpl implements CpService {
     }
 
     @Override
-    public void storeSchemaContext(final SchemaContext schemaContext) {
+    public final Integer storeJsonStructure(final String jsonStructure) {
+        return dataPersistencyService.storeJsonStructure(jsonStructure);
+    }
+
+    @Override
+    public final void storeSchemaContext(final SchemaContext schemaContext) {
         for (final Module module : schemaContext.getModules()) {
             modelPersistencyService.storeModule(module.getName(), module.toString(),
                 module.getRevision().toString());
diff --git a/cps/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java b/cps/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java
new file mode 100644 (file)
index 0000000..4dcd31d
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi;
+
+/**
+ * Defines methods to access and manipulate data using the chosen database solution.
+ */
+public interface DataPersistencyService {
+
+    /**
+     * Store the JSON structure in the database.
+     *
+     * @param jsonStructure the JSON structure.
+     * @return jsonEntityID the ID of the JSON entity.
+     */
+    Integer storeJsonStructure(final String jsonStructure);
+}