From: niamhcore Date: Fri, 25 Sep 2020 13:53:25 +0000 (+0100) Subject: Store and Validate a JSON Object X-Git-Tag: 0.0.1~120^2~22 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=6612c79168d699190044ab1950400061c85d1be7;p=cps.git Store and Validate a JSON Object Issue-ID: CCSDK-2757 Jira Link: https://jira.onap.org/browse/CCSDK-2757 Change-Id: Icfdbaec1a853ba5ba4a22742fb3091fc71a11d71 Change-Id: I80777bb2692e6e7f7594f787ec480817e8df0a35 --- diff --git a/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java b/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java index dc6411009..a64cd6a04 100644 --- a/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java +++ b/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java @@ -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 index 000000000..413362e38 --- /dev/null +++ b/cps/cps-ri/src/main/java/org/onap/cps/spi/entities/JsonDataEntity.java @@ -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; + } +} diff --git a/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/entities/ModuleEntity.java b/cps/cps-ri/src/main/java/org/onap/cps/spi/entities/ModuleEntity.java similarity index 88% rename from cps/cps-ri/src/main/java/org/onap/cps/spi/impl/entities/ModuleEntity.java rename to cps/cps-ri/src/main/java/org/onap/cps/spi/entities/ModuleEntity.java index be3dfefef..f786c58f1 100644 --- a/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/entities/ModuleEntity.java +++ b/cps/cps-ri/src/main/java/org/onap/cps/spi/entities/ModuleEntity.java @@ -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 index 000000000..93746e06f --- /dev/null +++ b/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java @@ -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(); + } +} diff --git a/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java b/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java index 6e718c8c6..14085c7cf 100644 --- a/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java +++ b/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java @@ -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 index 000000000..f3dd58600 --- /dev/null +++ b/cps/cps-ri/src/main/java/org/onap/cps/spi/repository/DataRepository.java @@ -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 { +} \ No newline at end of file diff --git a/cps/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java b/cps/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java index 84441cbf7..0fe53b621 100644 --- a/cps/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java +++ b/cps/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java @@ -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; diff --git a/cps/cps-service/pom.xml b/cps/cps-service/pom.xml index 844539410..e0d7ebd8e 100644 --- a/cps/cps-service/pom.xml +++ b/cps/cps-service/pom.xml @@ -52,6 +52,12 @@ spring-context + + + com.google.code.gson + gson + + \ No newline at end of file diff --git a/cps/cps-service/src/main/java/org/onap/cps/api/CpService.java b/cps/cps-service/src/main/java/org/onap/cps/api/CpService.java index a5ab485f6..cdd1c4dca 100644 --- a/cps/cps-service/src/main/java/org/onap/cps/api/CpService.java +++ b/cps/cps-service/src/main/java/org/onap/cps/api/CpService.java @@ -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); } diff --git a/cps/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java b/cps/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java index b71e4714b..8d6b63bd5 100644 --- a/cps/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java +++ b/cps/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java @@ -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 index 000000000..4dcd31d95 --- /dev/null +++ b/cps/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java @@ -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); +}