diff options
author | Vinal <vinal.narendrabhai.patel@ibm.com> | 2019-01-16 11:09:27 -0500 |
---|---|---|
committer | Vinal <vinal.narendrabhai.patel@ibm.com> | 2019-01-24 13:54:09 -0500 |
commit | f47c36f44fc04a50f7a9f7da5d845d854dea0d14 (patch) | |
tree | db2943b04849b582a5ad32e6d803786032a49182 /ms/controllerblueprints/modules/service/src/main | |
parent | 3cbb9eb57218a94075a0836d76860475d3b99a0c (diff) |
Integration test at API level
Change-Id: I6f15b5693f494e39bf8d135a96730f2379f5e3fd
Issue-ID: CCSDK-954
Signed-off-by: Vinal <vinal.narendrabhai.patel@ibm.com>
Diffstat (limited to 'ms/controllerblueprints/modules/service/src/main')
6 files changed, 386 insertions, 391 deletions
diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BlueprintModelService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BlueprintModelService.java deleted file mode 100644 index 3cf144f17..000000000 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BlueprintModelService.java +++ /dev/null @@ -1,293 +0,0 @@ -/*
- * Copyright © 2017-2018 AT&T Intellectual Property.
- * Modifications Copyright © 2019 Bell Canada.
- *
- * 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.
- */
-
-package org.onap.ccsdk.apps.controllerblueprints.service;
-
-import java.io.IOException; -import java.nio.file.Path; -import java.util.List; -import java.util.Optional; -import org.jetbrains.annotations.NotNull;
-import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
-import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants;
-import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration;
-import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode;
-import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService;
-import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils;
-import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel;
-import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelSearch;
-import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelContentRepository;
-import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelRepository;
-import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelSearchRepository;
-import org.onap.ccsdk.apps.controllerblueprints.service.utils.BluePrintEnhancerUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.io.ByteArrayResource;
-import org.springframework.core.io.Resource;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
-import org.springframework.http.codec.multipart.FilePart;
-import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
-import reactor.core.publisher.Mono;
-
-/**
- * BlueprintModelService.java Purpose: Provide Service Template Service processing BlueprintModelService
- *
- * @author Brinda Santh
- * @version 1.0
- */
-
-@Service
-public class BlueprintModelService {
-
- @Autowired
- private BluePrintLoadConfiguration bluePrintLoadConfiguration;
-
- @Autowired
- private BluePrintCatalogService bluePrintCatalogService;
-
- @Autowired
- private ControllerBlueprintModelSearchRepository blueprintModelSearchRepository;
-
- @Autowired
- private ControllerBlueprintModelRepository blueprintModelRepository;
-
- @Autowired
- private ControllerBlueprintModelContentRepository blueprintModelContentRepository;
-
- private static final String BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%s) from repo";
- private static final String BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG = "failed to get blueprint model by name(%s)" +
- " and version(%s) from repo"; -
- /**
- * This is a saveBlueprintModel method
- *
- * @param filePart filePart
- * @return Mono<BlueprintModelSearch>
- * @throws BluePrintException BluePrintException
- */
- public Mono<BlueprintModelSearch> saveBlueprintModel(FilePart filePart) throws BluePrintException {
- try {
- Path cbaLocation = BluePrintFileUtils.Companion
- .getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath); - return BluePrintEnhancerUtils.Companion.saveCBAFile(filePart, cbaLocation).map(fileName -> {
- String blueprintId = null; - try { - blueprintId = bluePrintCatalogService - .saveToDatabase(cbaLocation.toFile(), false); - } catch (BluePrintException e) { - // FIXME handle expection - } - return blueprintModelSearchRepository.findById(blueprintId).get();
- });
- } catch (IOException e) {
- throw new BluePrintException(ErrorCode.IO_FILE_INTERRUPT.getValue(),
- String.format("I/O Error while uploading the CBA file: %s", e.getMessage()), e); - }
- }
-
- /**
- * This is a publishBlueprintModel method to change the status published to YES
- *
- * @param id id
- * @return BlueprintModelSearch
- * @throws BluePrintException BluePrintException
- */
- public BlueprintModelSearch publishBlueprintModel(String id) throws BluePrintException {
- BlueprintModelSearch blueprintModelSearch;
- Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository.findById(id);
- if (dbBlueprintModel.isPresent()) {
- blueprintModelSearch = dbBlueprintModel.get();
- } else {
- String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);
- throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
- }
- blueprintModelSearch.setPublished(ApplicationConstants.ACTIVE_Y);
- return blueprintModelSearchRepository.saveAndFlush(blueprintModelSearch);
- }
-
- /**
- * This is a searchBlueprintModels method
- *
- * @param tags tags
- * @return List<BlueprintModelSearch>
- */
- public List<BlueprintModelSearch> searchBlueprintModels(String tags) {
- return blueprintModelSearchRepository.findByTagsContainingIgnoreCase(tags);
- }
-
- /**
- * This is a getBlueprintModelSearchByNameAndVersion method
- *
- * @param name name
- * @param version version
- * @return BlueprintModelSearch
- * @throws BluePrintException BluePrintException
- */
- public BlueprintModelSearch getBlueprintModelSearchByNameAndVersion(@NotNull String name, @NotNull String version)
- throws BluePrintException { - BlueprintModelSearch blueprintModelSearch;
- Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository
- .findByArtifactNameAndArtifactVersion(name, version); - if (dbBlueprintModel.isPresent()) {
- blueprintModelSearch = dbBlueprintModel.get();
- } else {
- throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(),
- String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version)); - }
- return blueprintModelSearch;
- }
-
- /**
- * This is a downloadBlueprintModelFileByNameAndVersion method to download a Blueprint by Name and Version
- *
- * @param name name - * @param version version
- * @return ResponseEntity<Resource>
- * @throws BluePrintException BluePrintException
- */
- public ResponseEntity<Resource> downloadBlueprintModelFileByNameAndVersion(@NotNull String name, - @NotNull String version) - throws BluePrintException { - BlueprintModel blueprintModel;
- try {
- blueprintModel = getBlueprintModelByNameAndVersion(name, version);
- } catch (BluePrintException e) {
- throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), String.format("Error while " +
- "downloading the CBA file: %s", e.getMessage()), e); - }
- String fileName = blueprintModel.getId() + ".zip";
- byte[] file = blueprintModel.getBlueprintModelContent().getContent();
- return prepareResourceEntity(fileName, file);
- }
-
- /**
- * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource
- *
- * @return ResponseEntity<Resource>
- * @throws BluePrintException BluePrintException
- */
- public ResponseEntity<Resource> downloadBlueprintModelFile(@NotNull String id) throws BluePrintException {
- BlueprintModel blueprintModel;
- try {
- blueprintModel = getBlueprintModel(id);
- } catch (BluePrintException e) {
- throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), String.format("Error while " +
- "downloading the CBA file: %s", e.getMessage()), e); - }
- String fileName = blueprintModel.getId() + ".zip";
- byte[] file = blueprintModel.getBlueprintModelContent().getContent();
- return prepareResourceEntity(fileName, file);
- }
-
- /**
- * @return ResponseEntity<Resource>
- */
- private ResponseEntity<Resource> prepareResourceEntity(String fileName, byte[] file) {
- return ResponseEntity.ok()
- .contentType(MediaType.parseMediaType("text/plain")) - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"") - .body(new ByteArrayResource(file)); - }
-
- /**
- * This is a getBlueprintModel method
- *
- * @param id id
- * @return BlueprintModel
- * @throws BluePrintException BluePrintException
- */
- private BlueprintModel getBlueprintModel(@NotNull String id) throws BluePrintException {
- BlueprintModel blueprintModel;
- Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findById(id);
- if (dbBlueprintModel.isPresent()) {
- blueprintModel = dbBlueprintModel.get();
- } else {
- String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);
- throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
- }
- return blueprintModel;
- }
-
- /**
- * This is a getBlueprintModelByNameAndVersion method
- *
- * @param name name - * @param version version
- * @return BlueprintModel
- * @throws BluePrintException BluePrintException
- */
- private BlueprintModel getBlueprintModelByNameAndVersion(@NotNull String name, @NotNull String version)
- throws BluePrintException { - BlueprintModel blueprintModel = blueprintModelRepository - .findByArtifactNameAndArtifactVersion(name, version); - if (blueprintModel != null) { - return blueprintModel; - } else {
- String msg = String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version);
- throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
- }
- }
-
- /**
- * This is a getBlueprintModelSearch method
- *
- * @param id id
- * @return BlueprintModelSearch
- * @throws BluePrintException BluePrintException
- */
- public BlueprintModelSearch getBlueprintModelSearch(@NotNull String id) throws BluePrintException {
- BlueprintModelSearch blueprintModelSearch;
- Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository.findById(id);
- if (dbBlueprintModel.isPresent()) {
- blueprintModelSearch = dbBlueprintModel.get();
- } else {
- String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);
- throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
- }
-
- return blueprintModelSearch;
- }
-
- /**
- * This is a deleteBlueprintModel method
- *
- * @param id id
- * @throws BluePrintException BluePrintException
- */
- @Transactional
- public void deleteBlueprintModel(@NotNull String id) throws BluePrintException {
- Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findById(id);
- if (dbBlueprintModel.isPresent()) {
- blueprintModelContentRepository.deleteByBlueprintModel(dbBlueprintModel.get());
- blueprintModelRepository.delete(dbBlueprintModel.get());
- } else {
- String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);
- throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
- }
- }
-
- /**
- * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database
- *
- * @return List<BlueprintModelSearch> list of the controller blueprint archives
- */
- public List<BlueprintModelSearch> getAllBlueprintModel() {
- return blueprintModelSearchRepository.findAll();
- }
-}
diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/rs/BlueprintModelRest.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/rs/BlueprintModelRest.java deleted file mode 100644 index 255137bf5..000000000 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/rs/BlueprintModelRest.java +++ /dev/null @@ -1,97 +0,0 @@ -/*
- * Copyright © 2017-2018 AT&T Intellectual Property.
- * Modifications Copyright © 2019 Bell Canada.
- *
- * 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.
- */
-
-package org.onap.ccsdk.apps.controllerblueprints.service.rs;
-
-import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
-import org.onap.ccsdk.apps.controllerblueprints.service.BlueprintModelService;
-import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelSearch;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.io.Resource;
-import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
-import org.springframework.http.codec.multipart.FilePart;
-import org.springframework.web.bind.annotation.*;
-import reactor.core.publisher.Mono;
-
-import java.util.List;
-
-/**
- * {@inheritDoc}
- */
-@RestController
-@RequestMapping(value = "/api/v1/blueprint-model")
-public class BlueprintModelRest {
-
- @Autowired
- private BlueprintModelService blueprintModelService;
-
- @PostMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
- public @ResponseBody
- Mono<BlueprintModelSearch> saveBlueprint(@RequestPart("file") FilePart file) throws BluePrintException{
- return blueprintModelService.saveBlueprintModel(file);
- }
-
- @DeleteMapping(path = "/{id}")
- public void deleteBlueprint(@PathVariable(value = "id") String id) throws BluePrintException {
- this.blueprintModelService.deleteBlueprintModel(id);
- }
-
- @GetMapping(path = "/by-name/{name}/version/{version}", produces = MediaType.APPLICATION_JSON_VALUE)
- public @ResponseBody
- BlueprintModelSearch getBlueprintByNameAndVersion(@PathVariable(value = "name") String name,
- @PathVariable(value = "version") String version) throws BluePrintException {
- return this.blueprintModelService.getBlueprintModelSearchByNameAndVersion(name, version);
- }
-
- @GetMapping(path = "/download/by-name/{name}/version/{version}", produces = MediaType.APPLICATION_JSON_VALUE)
- public @ResponseBody
- ResponseEntity<Resource> downloadBlueprintByNameAndVersion(@PathVariable(value = "name") String name,
- @PathVariable(value = "version") String version) throws BluePrintException {
- return this.blueprintModelService.downloadBlueprintModelFileByNameAndVersion(name, version);
- }
-
- @GetMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
- public @ResponseBody
- BlueprintModelSearch getBlueprintModel(@PathVariable(value = "id") String id) throws BluePrintException {
- return this.blueprintModelService.getBlueprintModelSearch(id);
- }
-
- @GetMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE)
- public @ResponseBody
- List<BlueprintModelSearch> getAllBlueprintModel() {
- return this.blueprintModelService.getAllBlueprintModel();
- }
-
- @GetMapping(path = "/download/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
- public @ResponseBody
- ResponseEntity<Resource> downloadBluePrint(@PathVariable(value = "id") String id) throws BluePrintException {
- return this.blueprintModelService.downloadBlueprintModelFile(id);
- }
-
- @PutMapping(path = "/publish/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
- public @ResponseBody
- BlueprintModelSearch publishBlueprintModel(@PathVariable(value = "id") String id) throws BluePrintException {
- return this.blueprintModelService.publishBlueprintModel(id);
- }
-
- @GetMapping(path = "/search/{tags}", produces = MediaType.APPLICATION_JSON_VALUE)
- public @ResponseBody
- List<BlueprintModelSearch> searchBlueprintModels(@PathVariable(value = "tags") String tags) {
- return this.blueprintModelService.searchBlueprintModels(tags);
- }
-}
diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/BlueprintModelRest.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/BlueprintModelRest.kt new file mode 100644 index 000000000..0fca07b04 --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/BlueprintModelRest.kt @@ -0,0 +1,100 @@ +/* + * Copyright © 2019 Bell Canada Intellectual Property. + * + * 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. + */ + +package org.onap.ccsdk.apps.controllerblueprints.service.controller + +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException +import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelSearch +import org.onap.ccsdk.apps.controllerblueprints.service.handler.BluePrintModelHandler +import org.springframework.core.io.Resource +import org.springframework.http.MediaType +import org.springframework.http.ResponseEntity +import org.springframework.http.codec.multipart.FilePart +import org.springframework.web.bind.annotation.* +import reactor.core.publisher.Mono + +/** + * BlueprintModelRest Purpose: Handle controllerBlueprint API request + * + * @author Vinal Patel + * @version 1.0 + */ +@RestController +@RequestMapping("/api/v1/blueprint-model") +open class BlueprintModelRest(private val bluePrintModelHandler: BluePrintModelHandler) { + + @PostMapping("", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE]) + @ResponseBody + @Throws(BluePrintException::class) + fun saveBlueprint(@RequestPart("file") file: FilePart): Mono<BlueprintModelSearch> { + return bluePrintModelHandler.saveBlueprintModel(file) + } + + @GetMapping("", produces = [MediaType.APPLICATION_JSON_VALUE]) + @ResponseBody + fun allBlueprintModel(): List<BlueprintModelSearch> { + return this.bluePrintModelHandler.allBlueprintModel() + } + + @DeleteMapping("/{id}") + @Throws(BluePrintException::class) + fun deleteBlueprint(@PathVariable(value = "id") id: String) { + this.bluePrintModelHandler.deleteBlueprintModel(id) + } + + @GetMapping("/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE]) + @ResponseBody + @Throws(BluePrintException::class) + fun getBlueprintByNameAndVersion(@PathVariable(value = "name") name: String, + @PathVariable(value = "version") version: String): BlueprintModelSearch { + return this.bluePrintModelHandler.getBlueprintModelSearchByNameAndVersion(name, version) + } + + @GetMapping("/download/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE]) + @ResponseBody + @Throws(BluePrintException::class) + fun downloadBlueprintByNameAndVersion(@PathVariable(value = "name") name: String, + @PathVariable(value = "version") version: String): ResponseEntity<Resource> { + return this.bluePrintModelHandler.downloadBlueprintModelFileByNameAndVersion(name, version) + } + + @GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE]) + @ResponseBody + @Throws(BluePrintException::class) + fun getBlueprintModel(@PathVariable(value = "id") id: String): BlueprintModelSearch { + return this.bluePrintModelHandler.getBlueprintModelSearch(id) + } + + @GetMapping("/download/{id}", produces = [MediaType.APPLICATION_JSON_VALUE]) + @ResponseBody + @Throws(BluePrintException::class) + fun downloadBluePrint(@PathVariable(value = "id") id: String): ResponseEntity<Resource> { + return this.bluePrintModelHandler.downloadBlueprintModelFile(id) + } + + @PutMapping("/publish/{id}", produces = [MediaType.APPLICATION_JSON_VALUE]) + @ResponseBody + @Throws(BluePrintException::class) + fun publishBlueprintModel(@PathVariable(value = "id") id: String): BlueprintModelSearch { + return this.bluePrintModelHandler.publishBlueprintModel(id) + } + + @GetMapping("/search/{tags}", produces = [MediaType.APPLICATION_JSON_VALUE]) + @ResponseBody + fun searchBlueprintModels(@PathVariable(value = "tags") tags: String): List<BlueprintModelSearch> { + return this.bluePrintModelHandler.searchBlueprintModels(tags) + } +} diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/ControllerBlueprintExeptionHandler.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/ControllerBlueprintExeptionHandler.kt index a0e47d720..04753391f 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/ControllerBlueprintExeptionHandler.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/controller/ControllerBlueprintExeptionHandler.kt @@ -25,7 +25,7 @@ import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.ExceptionHandler /** - * ControllerBlueprintExceptionHandler.java Purpose: Handle exceptions in controllerBlueprint API and provide the wright + * ControllerBlueprintExceptionHandler Purpose: Handle exceptions in controllerBlueprint API and provide the right * HTTP code status * * @author Vinal Patel diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/handler/BluePrintModelHandler.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/handler/BluePrintModelHandler.kt new file mode 100644 index 000000000..907566c32 --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/handler/BluePrintModelHandler.kt @@ -0,0 +1,283 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2019 Bell Canada. + * + * 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. + */ + +package org.onap.ccsdk.apps.controllerblueprints.service.handler + +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException +import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants +import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration +import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode +import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService +import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils +import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel +import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelSearch +import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelContentRepository +import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelRepository +import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelSearchRepository +import org.onap.ccsdk.apps.controllerblueprints.service.utils.BluePrintEnhancerUtils +import org.springframework.core.io.ByteArrayResource +import org.springframework.core.io.Resource +import org.springframework.http.HttpHeaders +import org.springframework.http.MediaType +import org.springframework.http.ResponseEntity +import org.springframework.http.codec.multipart.FilePart +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional +import reactor.core.publisher.Mono +import java.io.IOException + +/** + * BlueprintModelHandler Purpose: Handler service to handle the request from BlurPrintModelRest + * + * @author Brinda Santh + * @version 1.0 + */ + +@Service +open class BluePrintModelHandler(private val bluePrintCatalogService: BluePrintCatalogService, private val bluePrintLoadConfiguration: BluePrintLoadConfiguration, + private val blueprintModelSearchRepository: ControllerBlueprintModelSearchRepository, + private val blueprintModelRepository: ControllerBlueprintModelRepository, + private val blueprintModelContentRepository: ControllerBlueprintModelContentRepository) { + + /** + * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database + * + * @return List<BlueprintModelSearch> list of the controller blueprint archives + </BlueprintModelSearch> */ + open fun allBlueprintModel(): List<BlueprintModelSearch> { + return blueprintModelSearchRepository.findAll() + } + + /** + * This is a saveBlueprintModel method + * + * @param filePart filePart + * @return Mono<BlueprintModelSearch> + * @throws BluePrintException BluePrintException + </BlueprintModelSearch> */ + @Throws(BluePrintException::class) + open fun saveBlueprintModel(filePart: FilePart): Mono<BlueprintModelSearch> { + try { + val cbaLocation = BluePrintFileUtils.getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath) + return BluePrintEnhancerUtils.saveCBAFile(filePart, cbaLocation).map { fileName -> + var blueprintId: String? = null + try { + blueprintId = bluePrintCatalogService.saveToDatabase(cbaLocation.resolve(fileName).toFile(), false) + } catch (e: BluePrintException) { + // FIXME handle expection + } + blueprintModelSearchRepository.findById(blueprintId!!).get() + } + } catch (e: IOException) { + throw BluePrintException(ErrorCode.IO_FILE_INTERRUPT.value, + String.format("I/O Error while uploading the CBA file: %s", e.message), e) + } + + } + + /** + * This is a publishBlueprintModel method to change the status published to YES + * + * @param id id + * @return BlueprintModelSearch + * @throws BluePrintException BluePrintException + */ + @Throws(BluePrintException::class) + open fun publishBlueprintModel(id: String): BlueprintModelSearch { + val blueprintModelSearch: BlueprintModelSearch + val dbBlueprintModel = blueprintModelSearchRepository.findById(id) + if (dbBlueprintModel.isPresent) { + blueprintModelSearch = dbBlueprintModel.get() + } else { + val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id) + throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg) + } + blueprintModelSearch.published = ApplicationConstants.ACTIVE_Y + return blueprintModelSearchRepository.saveAndFlush(blueprintModelSearch) + } + + /** + * This is a searchBlueprintModels method + * + * @param tags tags + * @return List<BlueprintModelSearch> + </BlueprintModelSearch> */ + open fun searchBlueprintModels(tags: String): List<BlueprintModelSearch> { + return blueprintModelSearchRepository.findByTagsContainingIgnoreCase(tags) + } + + /** + * This is a getBlueprintModelSearchByNameAndVersion method + * + * @param name name + * @param version version + * @return BlueprintModelSearch + * @throws BluePrintException BluePrintException + */ + @Throws(BluePrintException::class) + open fun getBlueprintModelSearchByNameAndVersion(name: String, version: String): BlueprintModelSearch { + val blueprintModelSearch: BlueprintModelSearch + val dbBlueprintModel = blueprintModelSearchRepository + .findByArtifactNameAndArtifactVersion(name, version) + if (dbBlueprintModel.isPresent) { + blueprintModelSearch = dbBlueprintModel.get() + } else { + throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, + String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version)) + } + return blueprintModelSearch + } + + /** + * This is a downloadBlueprintModelFileByNameAndVersion method to download a Blueprint by Name and Version + * + * @param name name + * @param version version + * @return ResponseEntity<Resource> + * @throws BluePrintException BluePrintException + </Resource> */ + @Throws(BluePrintException::class) + open fun downloadBlueprintModelFileByNameAndVersion(name: String, + version: String): ResponseEntity<Resource> { + val blueprintModel: BlueprintModel + try { + blueprintModel = getBlueprintModelByNameAndVersion(name, version) + } catch (e: BluePrintException) { + throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, String.format("Error while " + "downloading the CBA file: %s", e.message), e) + } + + val fileName = blueprintModel.id + ".zip" + val file = blueprintModel.blueprintModelContent.content + return prepareResourceEntity(fileName, file) + } + + /** + * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource + * + * @return ResponseEntity<Resource> + * @throws BluePrintException BluePrintException + </Resource> */ + @Throws(BluePrintException::class) + open fun downloadBlueprintModelFile(id: String): ResponseEntity<Resource> { + val blueprintModel: BlueprintModel + try { + blueprintModel = getBlueprintModel(id) + } catch (e: BluePrintException) { + throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, String.format("Error while " + "downloading the CBA file: %s", e.message), e) + } + + val fileName = blueprintModel.id + ".zip" + val file = blueprintModel.blueprintModelContent.content + return prepareResourceEntity(fileName, file) + } + + /** + * @return ResponseEntity<Resource> + </Resource> */ + private fun prepareResourceEntity(fileName: String, file: ByteArray): ResponseEntity<Resource> { + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("text/plain")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"$fileName\"") + .body(ByteArrayResource(file)) + } + + /** + * This is a getBlueprintModel method + * + * @param id id + * @return BlueprintModel + * @throws BluePrintException BluePrintException + */ + @Throws(BluePrintException::class) + open fun getBlueprintModel(id: String): BlueprintModel { + val blueprintModel: BlueprintModel + val dbBlueprintModel = blueprintModelRepository.findById(id) + if (dbBlueprintModel.isPresent) { + blueprintModel = dbBlueprintModel.get() + } else { + val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id) + throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg) + } + return blueprintModel + } + + /** + * This is a getBlueprintModelByNameAndVersion method + * + * @param name name + * @param version version + * @return BlueprintModel + * @throws BluePrintException BluePrintException + */ + @Throws(BluePrintException::class) + open fun getBlueprintModelByNameAndVersion(name: String, version: String): BlueprintModel { + val blueprintModel = blueprintModelRepository + .findByArtifactNameAndArtifactVersion(name, version) + if (blueprintModel != null) { + return blueprintModel + } else { + val msg = String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version) + throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg) + } + } + + /** + * This is a getBlueprintModelSearch method + * + * @param id id + * @return BlueprintModelSearch + * @throws BluePrintException BluePrintException + */ + @Throws(BluePrintException::class) + open fun getBlueprintModelSearch(id: String): BlueprintModelSearch { + val blueprintModelSearch: BlueprintModelSearch + val dbBlueprintModel = blueprintModelSearchRepository.findById(id) + if (dbBlueprintModel.isPresent) { + blueprintModelSearch = dbBlueprintModel.get() + } else { + val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id) + throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg) + } + + return blueprintModelSearch + } + + /** + * This is a deleteBlueprintModel method + * + * @param id id + * @throws BluePrintException BluePrintException + */ + @Transactional + @Throws(BluePrintException::class) + open fun deleteBlueprintModel(id: String) { + val dbBlueprintModel = blueprintModelRepository.findById(id) + if (dbBlueprintModel.isPresent) { + blueprintModelContentRepository.deleteByBlueprintModel(dbBlueprintModel.get()) + blueprintModelRepository.delete(dbBlueprintModel.get()) + } else { + val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id) + throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg) + } + } + + companion object { + + private const val BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%s) from repo" + private const val BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG = "failed to get blueprint model by name(%s)" + " and version(%s) from repo" + } +} diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt index 04071dd20..779be65d9 100755 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/load/ControllerBlueprintCatalogServiceImpl.kt @@ -97,6 +97,8 @@ class ControllerBlueprintCatalogServiceImpl(bluePrintValidatorService: BluePrint blueprintModelContent.description = "$artifactName:$artifactVersion CBA Zip Content" blueprintModelContent.content = Files.readAllBytes(archiveFile.toPath()) blueprintModelContent.blueprintModel = blueprintModel + // Set the Blueprint Model Content into blueprintModel + blueprintModel.blueprintModelContent = blueprintModelContent try { blueprintModelRepository.saveAndFlush(blueprintModel) |