From 48830f146f6776afa180fefa101788b169bc841a Mon Sep 17 00:00:00 2001 From: "Rishi.Chail" Date: Mon, 9 Nov 2020 03:28:44 +0000 Subject: VSE: Create an anchor in the given dataspace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue-ID: CPS-42 https://jira.onap.org/browse/CPS-42 Signed-off-by: Rishi Chail Change-Id: If67be6f13889808da4d9fe830595766af67e4fdf --- .../main/java/org/onap/cps/config/CpsConfig.java | 50 ++++++++++++++++++++++ .../cps/rest/controller/CpsRestController.java | 27 +++++++++--- .../onap/cps/swagger/config/SpringFoxConfig.java | 46 -------------------- 3 files changed, 71 insertions(+), 52 deletions(-) create mode 100755 cps-rest/src/main/java/org/onap/cps/config/CpsConfig.java mode change 100644 => 100755 cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java delete mode 100644 cps-rest/src/main/java/org/onap/cps/swagger/config/SpringFoxConfig.java (limited to 'cps-rest/src') diff --git a/cps-rest/src/main/java/org/onap/cps/config/CpsConfig.java b/cps-rest/src/main/java/org/onap/cps/config/CpsConfig.java new file mode 100755 index 000000000..cca5fe7d8 --- /dev/null +++ b/cps-rest/src/main/java/org/onap/cps/config/CpsConfig.java @@ -0,0 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. 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. + * 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.config; + +import org.modelmapper.ModelMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + +@Configuration +public class CpsConfig { + + /** + * Swagger configuration. + */ + @Bean + public Docket api() { + return new Docket(DocumentationType.OAS_30).select().apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()).build(); + } + + /** + * ModelMapper configuration. + */ + @Bean + public ModelMapper modelMapper() { + return new ModelMapper(); + } +} \ No newline at end of file diff --git a/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java b/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java old mode 100644 new mode 100755 index f0c5fcbc4..9e57408fb --- a/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java @@ -27,10 +27,13 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import javax.validation.Valid; +import org.modelmapper.ModelMapper; import org.onap.cps.api.CpService; +import org.onap.cps.api.model.AnchorDetails; import org.onap.cps.exceptions.CpsException; import org.onap.cps.exceptions.CpsValidationException; import org.onap.cps.rest.api.CpsRestApi; +import org.onap.cps.rest.model.Anchor; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -49,9 +52,22 @@ public class CpsRestController implements CpsRestApi { @Autowired private CpService cpService; + @Autowired + private ModelMapper modelMapper; + + /** + * Create a new anchor. + * + * @param anchor the anchor details object. + * @param dataspaceName the dataspace name. + * @return a ResponseEntity with the anchor name. + */ @Override - public ResponseEntity createAnchor(@Valid MultipartFile multipartFile, String dataspaceName) { - return null; + public final ResponseEntity createAnchor(@Valid Anchor anchor, String dataspaceName) { + final AnchorDetails anchorDetails = modelMapper.map(anchor, AnchorDetails.class); + anchorDetails.setDataspace(dataspaceName); + final String anchorName = cpService.createAnchor(anchorDetails); + return new ResponseEntity(anchorName, HttpStatus.CREATED); } @Override @@ -151,7 +167,7 @@ public class CpsRestController implements CpsRestApi { try { final Gson gson = new Gson(); gson.fromJson(getJsonString(multipartFile), Object.class); - } catch (JsonSyntaxException e) { + } catch (final JsonSyntaxException e) { throw new CpsValidationException("Not a valid JSON file.", e); } } @@ -160,13 +176,12 @@ public class CpsRestController implements CpsRestApi { try { final File file = File.createTempFile("tempFile", ".yang"); file.deleteOnExit(); - try (OutputStream outputStream = new FileOutputStream(file)) { outputStream.write(multipartFile.getBytes()); } return file; - } catch (IOException e) { + } catch (final IOException e) { throw new CpsException(e); } } @@ -174,7 +189,7 @@ public class CpsRestController implements CpsRestApi { private static String getJsonString(final MultipartFile multipartFile) { try { return new String(multipartFile.getBytes()); - } catch (IOException e) { + } catch (final IOException e) { throw new CpsException(e); } } diff --git a/cps-rest/src/main/java/org/onap/cps/swagger/config/SpringFoxConfig.java b/cps-rest/src/main/java/org/onap/cps/swagger/config/SpringFoxConfig.java deleted file mode 100644 index 73e179511..000000000 --- a/cps-rest/src/main/java/org/onap/cps/swagger/config/SpringFoxConfig.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * 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. - * 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.swagger.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; - -/** - * Swagger configuration. - */ -@Configuration -public class SpringFoxConfig { - - /** - * Define api configuration. - */ - @Bean - public Docket api() { - return new Docket(DocumentationType.OAS_30) - .select() - .apis(RequestHandlerSelectors.any()) - .paths(PathSelectors.any()) - .build(); - } -} -- cgit 1.2.3-korg