From 47598117ee0b411cfc6fad323598f5644b51c9c9 Mon Sep 17 00:00:00 2001 From: "puthuparambil.aditya" Date: Mon, 2 Nov 2020 11:31:39 +0000 Subject: [PATCH] Swagger codegen using Spring framework 1. Pom.xml updated to generate API code using Swagger codegen. 2. Moved the createModule code to the method implemented by Swagger. JIRA: https://jira.onap.org/browse/CCSDK-2897 Issue-ID: CCSDK-2897 Signed-off-by: puthuparambil.aditya Change-Id: Iadd3dc0402f06a597a179148fd5f43e0fe7c48f6 Signed-off-by: puthuparambil.aditya --- cps/cps-dependencies/pom.xml | 11 ++++ cps/cps-parent/pom.xml | 28 ++++++++ cps/cps-rest/docs/api/swagger/openapi.yml | 34 +++++----- cps/cps-rest/pom.xml | 13 ++++ .../cps/rest/controller/CpsRestController.java | 77 ++++++++++++++++------ 5 files changed, 127 insertions(+), 36 deletions(-) diff --git a/cps/cps-dependencies/pom.xml b/cps/cps-dependencies/pom.xml index 6f50cd0fc1..ad71b11583 100644 --- a/cps/cps-dependencies/pom.xml +++ b/cps/cps-dependencies/pom.xml @@ -17,6 +17,7 @@ 2.10.0 2.0-M2-groovy-3.0 2.3.3.RELEASE + 2.6.1 2.1.4 5.0.6 @@ -47,6 +48,16 @@ swagger-jaxrs2 ${swagger.version} + + io.springfox + springfox-swagger2 + ${springfox-swagger2.version} + + + io.springfox + springfox-swagger-ui + ${springfox-swagger2.version} + com.vladmihalcea hibernate-types-52 diff --git a/cps/cps-parent/pom.xml b/cps/cps-parent/pom.xml index 2ddb2b6683..db80970c53 100644 --- a/cps/cps-parent/pom.xml +++ b/cps/cps-parent/pom.xml @@ -22,6 +22,7 @@ 1.5.3 3.1.0 2.3.3.RELEASE + 3.0.18 3.35.0 @@ -78,6 +79,33 @@ + + + io.swagger.codegen.v3 + swagger-codegen-maven-plugin + ${swagger-codegen-maven-plugin.version} + + + + generate + + + ${project.basedir}/docs/api/swagger/openapi.yml + org.onap.cps.rest.controller + org.onap.cps.rest.model + org.onap.cps.rest.api + spring + false + + src/gen/java + java11 + true + true + + + + + org.apache.maven.plugins diff --git a/cps/cps-rest/docs/api/swagger/openapi.yml b/cps/cps-rest/docs/api/swagger/openapi.yml index f116c26cec..9b2ac1ec5d 100644 --- a/cps/cps-rest/docs/api/swagger/openapi.yml +++ b/cps/cps-rest/docs/api/swagger/openapi.yml @@ -6,13 +6,13 @@ info: servers: - url: //localhost:8088/ tags: - - name: cps-resource + - name: cps-rest description: cps Resource paths: /v1/dataspaces/{dataspace-name}/: delete: tags: - - cps-resource + - cps-rest summary: Delete the given dataspace operationId: deleteDataspace parameters: @@ -41,7 +41,7 @@ paths: /v1/dataspaces/{dataspace-name}/anchors: get: tags: - - cps-resource + - cps-rest summary: Read all anchors, given a dataspace operationId: getAnchors parameters: @@ -69,7 +69,7 @@ paths: content: {} post: tags: - - cps-resource + - cps-rest summary: Create a new anchor in the given dataspace operationId: createAnchor parameters: @@ -86,9 +86,9 @@ paths: required: - file properties: - file: + multipartFile: type: string - description: file + description: multipartFile format: binary required: true responses: @@ -113,7 +113,7 @@ paths: /v1/dataspaces/{dataspace-name}/anchors/{anchor-name}: get: tags: - - cps-resource + - cps-rest summary: Read an anchor given a anchor and a dataspace operationId: getAnchor parameters: @@ -147,7 +147,7 @@ paths: content: {} delete: tags: - - cps-resource + - cps-rest summary: Delete an anchor given a anchor and a dataspace operationId: deleteAnchor parameters: @@ -182,7 +182,7 @@ paths: /v1/dataspaces/{dataspace-name}/anchors/{anchor-name}/nodes: get: tags: - - cps-resource + - cps-rest summary: Get a node given an anchor for the given dataspace operationId: getNodeByDataspaceAndAnchor parameters: @@ -225,7 +225,7 @@ paths: /v1/dataspaces/{dataspace-name}/modules: get: tags: - - cps-resource + - cps-rest summary: Read all yang modules in the store operationId: getModule parameters: @@ -263,7 +263,7 @@ paths: content: {} post: tags: - - cps-resource + - cps-rest summary: Create modules for the given dataspace operationId: createModules parameters: @@ -280,9 +280,9 @@ paths: required: - file properties: - file: + multipartFile: type: string - description: file + description: multipartFile format: binary required: true responses: @@ -307,7 +307,7 @@ paths: /v1/dataspaces/{dataspace-name}/nodes: get: tags: - - cps-resource + - cps-rest summary: Get all nodes for a given dataspace using an xpath or schema node identifier operationId: getNode parameters: @@ -343,7 +343,7 @@ paths: x-codegen-request-body-name: requestBody post: tags: - - cps-resource + - cps-rest summary: Create a node for a given anchor for the given dataspace operationId: createNode parameters: @@ -360,9 +360,9 @@ paths: required: - file properties: - file: + multipartFile: type: string - description: file + description: multipartFile format: binary required: true responses: diff --git a/cps/cps-rest/pom.xml b/cps/cps-rest/pom.xml index f9be75b7e3..fcd97dcf77 100644 --- a/cps/cps-rest/pom.xml +++ b/cps/cps-rest/pom.xml @@ -42,6 +42,14 @@ io.swagger.core.v3 swagger-jaxrs2 + + io.springfox + springfox-swagger2 + + + io.springfox + springfox-swagger-ui + org.springframework.boot spring-boot-starter-test @@ -61,6 +69,11 @@ org.springframework.boot spring-boot-maven-plugin + + + io.swagger.codegen.v3 + swagger-codegen-maven-plugin + org.apache.maven.plugins diff --git a/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java b/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java index a577af70ec..9a31b6d8ea 100644 --- a/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java +++ b/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java @@ -27,8 +27,10 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import javax.persistence.PersistenceException; +import javax.validation.Valid; import org.hibernate.exception.ConstraintViolationException; import org.onap.cps.api.CpService; +import org.onap.cps.rest.api.CpsRestApi; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.parser.api.YangParserException; import org.springframework.beans.factory.annotation.Autowired; @@ -44,38 +46,75 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController -public class CpsRestController { +public class CpsRestController implements CpsRestApi { @Autowired private CpService cpService; - /* - Old rest endpoints before contract first approach (Need to be removed). - */ + @Override + public ResponseEntity createAnchor(@Valid MultipartFile multipartFile, String dataspaceName) { + return null; + } - /** - * Upload a yang model file. - * - * @param uploadedFile the yang model Multipart File. - * @param dataspaceName the dataspace name linked to the model. - * @return a ResponseEntity. - */ - @PostMapping("/dataspaces/{dataspace_name}/modules") - public final ResponseEntity uploadYangModelFile( - @RequestParam("file") MultipartFile uploadedFile, - @PathVariable("dataspace_name") String dataspaceName) { + @Override + public ResponseEntity createModules(@Valid MultipartFile multipartFile, String dataspaceName) { try { - final File fileToParse = saveToFile(uploadedFile); + final File fileToParse = saveToFile(multipartFile); final SchemaContext schemaContext = cpService.parseAndValidateModel(fileToParse); cpService.storeSchemaContext(schemaContext, dataspaceName); - return new ResponseEntity("Resource successfully created", HttpStatus.CREATED); + return new ResponseEntity<>("Resource successfully created", HttpStatus.CREATED); } catch (final YangParserException | ConstraintViolationException e) { - return new ResponseEntity(e.getMessage(), HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); } catch (final Exception e) { - return new ResponseEntity(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } + @Override + public ResponseEntity createNode(@Valid MultipartFile multipartFile, String dataspaceName) { + return null; + } + + @Override + public ResponseEntity deleteAnchor(String dataspaceName, String anchorName) { + return null; + } + + @Override + public ResponseEntity deleteDataspace(String dataspaceName) { + return null; + } + + @Override + public ResponseEntity getAnchor(String dataspaceName, String anchorName) { + return null; + } + + @Override + public ResponseEntity getAnchors(String dataspaceName) { + return null; + } + + @Override + public ResponseEntity getModule(String dataspaceName, @Valid String namespaceName, @Valid String revision) { + return null; + } + + @Override + public ResponseEntity getNode(@Valid String body, String dataspaceName) { + return null; + } + + @Override + public ResponseEntity getNodeByDataspaceAndAnchor(@Valid String body, String dataspaceName, + String anchorName) { + return null; + } + + /* + Old rest endpoints before contract first approach (Need to be removed). + */ + /** * Upload a JSON file. * -- 2.16.6