From a19a6d1683689ffcc95f160b0c2ed4718505e7f4 Mon Sep 17 00:00:00 2001 From: Steve Alphonse Siani Date: Fri, 21 Dec 2018 10:31:48 -0500 Subject: Applied comments from review: Change 74622 - Draft Change-Id: I8ec37a628af98a332f568dc254e499dfdcf886ce Issue-ID: CCSDK-418 Signed-off-by: Steve Alphonse Siani --- .../opt/app/onap/config/application-dev.properties | 5 +- .../opt/app/onap/config/application.properties | 5 +- .../src/test/resources/application.properties | 8 +- .../service/CBAContentService.java | 97 +++++++++++ .../service/CbaFileManagementService.java | 128 ++++++++++++++ .../controllerblueprints/service/CbaService.java | 192 +++++++++++++++++++++ .../service/CbaToDatabaseService.java | 132 ++++++++++++++ .../service/ConfigModelCreateService.java | 8 +- .../service/domain/CbaContent.java | 114 ++++++++++++ .../service/domain/ConfigModel.java | 12 ++ .../service/model/BlueprintModelResponse.java | 78 +++++++++ .../service/model/ItemCbaResponse.java | 58 +++++++ .../service/repository/CBAContentRepository.java | 59 +++++++ .../controllerblueprints/service/rs/CbaRest.java | 84 +++++++++ .../service/utils/CbaStateEnum.java | 15 ++ .../service/utils/CloseCondition.java | 36 ++++ .../service/CbaFileManagementServiceTest.java | 89 ++++++++++ .../src/test/resources/application.properties | 8 +- 18 files changed, 1121 insertions(+), 7 deletions(-) mode change 100644 => 100755 ms/controllerblueprints/application/opt/app/onap/config/application-dev.properties mode change 100644 => 100755 ms/controllerblueprints/application/opt/app/onap/config/application.properties mode change 100644 => 100755 ms/controllerblueprints/application/src/test/resources/application.properties create mode 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CBAContentService.java create mode 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaFileManagementService.java create mode 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaService.java create mode 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaToDatabaseService.java mode change 100644 => 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ConfigModelCreateService.java create mode 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/CbaContent.java mode change 100644 => 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/ConfigModel.java create mode 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/model/BlueprintModelResponse.java create mode 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/model/ItemCbaResponse.java create mode 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/repository/CBAContentRepository.java create mode 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/rs/CbaRest.java create mode 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/utils/CbaStateEnum.java create mode 100755 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/utils/CloseCondition.java create mode 100755 ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaFileManagementServiceTest.java mode change 100644 => 100755 ms/controllerblueprints/modules/service/src/test/resources/application.properties diff --git a/ms/controllerblueprints/application/opt/app/onap/config/application-dev.properties b/ms/controllerblueprints/application/opt/app/onap/config/application-dev.properties old mode 100644 new mode 100755 index 46aa2fa0e..46d853076 --- a/ms/controllerblueprints/application/opt/app/onap/config/application-dev.properties +++ b/ms/controllerblueprints/application/opt/app/onap/config/application-dev.properties @@ -61,4 +61,7 @@ controllerblueprints.loadBluePrintPaths=./../../../components/model-catalog/blue controllerblueprints.loadModelType=true controllerblueprints.loadModeTypePaths=./../../../components/model-catalog/definition-type/starter-type controllerblueprints.loadResourceDictionary=true -controllerblueprints.loadResourceDictionaryPaths=./../../../components/model-catalog/resource-dictionary/starter-dictionary \ No newline at end of file +controllerblueprints.loadResourceDictionaryPaths=./../../../components/model-catalog/resource-dictionary/starter-dictionary + +# CBA file extension +controllerblueprints.loadCbaExtension=zip \ No newline at end of file diff --git a/ms/controllerblueprints/application/opt/app/onap/config/application.properties b/ms/controllerblueprints/application/opt/app/onap/config/application.properties old mode 100644 new mode 100755 index 2ac4ad373..d618e015b --- a/ms/controllerblueprints/application/opt/app/onap/config/application.properties +++ b/ms/controllerblueprints/application/opt/app/onap/config/application.properties @@ -65,4 +65,7 @@ controllerblueprints.loadBluePrintPaths=model-catalog/blueprint-model/starter-bl controllerblueprints.loadModelType=true controllerblueprints.loadModeTypePaths=model-catalog/definition-type/starter-type controllerblueprints.loadResourceDictionary=true -controllerblueprints.loadResourceDictionaryPaths=model-catalog/resource-dictionary/starter-dictionary \ No newline at end of file +controllerblueprints.loadResourceDictionaryPaths=model-catalog/resource-dictionary/starter-dictionary + +# CBA file extension +controllerblueprints.loadCbaExtension=zip \ No newline at end of file diff --git a/ms/controllerblueprints/application/src/test/resources/application.properties b/ms/controllerblueprints/application/src/test/resources/application.properties old mode 100644 new mode 100755 index 48f70b0ea..9c8a96cba --- a/ms/controllerblueprints/application/src/test/resources/application.properties +++ b/ms/controllerblueprints/application/src/test/resources/application.properties @@ -46,4 +46,10 @@ controllerblueprints.loadBluePrintPaths=./../../../../components/model-catalog/b controllerblueprints.loadModelType=false controllerblueprints.loadModeTypePaths=./../../../../components/model-catalog/definition-type/starter-type controllerblueprints.loadResourceDictionary=false -controllerblueprints.loadResourceDictionaryPaths=./../../../../components/model-catalog/resource-dictionary/starter-dictionary \ No newline at end of file +controllerblueprints.loadResourceDictionaryPaths=./../../../../components/model-catalog/resource-dictionary/starter-dictionary + +# CBA file extension +controllerblueprints.loadCbaExtension=zip + +# CBA examples for tests cases +controllerblueprints.loadBlueprintsExamplesPath=./../../../../components/model-catalog/blueprint-model/test-blueprints \ No newline at end of file diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CBAContentService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CBAContentService.java new file mode 100755 index 000000000..66ef6a934 --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CBAContentService.java @@ -0,0 +1,97 @@ +/* + * Copyright © 2018 IBM Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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 com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.onap.ccsdk.apps.controllerblueprints.service.domain.CbaContent; +import org.onap.ccsdk.apps.controllerblueprints.service.repository.CBAContentRepository; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +/** + * CBAContentService.java Purpose: Provide CBAContent Template Service processing + * CBAContentService + * + * @author Ruben Chang + * @version 1.0 + */ + +@Service +public class CBAContentService { + + private static EELFLogger log = EELFManager.getInstance().getLogger(CBAContentService.class); + + private CBAContentRepository cbaContentRepository; + + /** + * Constructor of the class + * @param cbaContentRepository CRUD methods for entity CBAContentRepository + */ + public CBAContentService(CBAContentRepository cbaContentRepository) { + this.cbaContentRepository = cbaContentRepository; + log.info("CBAContentRepository sucessfully instantiated"); + } + + /** + * Save the CBAContent into the CBA_CONTENT table + * @param cbaName The name of the file + * @param cbaVersion version number of the CBA archive + * @param cbaState int that would represent the state. Refer to the CbaStateEnum + * @param cbaDescription Brief description that would help to identify and recognize the CBA archive + * @param file the file + * @return CbaContent the record saved into the table CBA_CONTENT + */ + public CbaContent saveCBAContent(String cbaName, String cbaVersion, int cbaState, String cbaDescription, byte[] file){ + CbaContent cbaContent = new CbaContent(); + cbaContent.setCbaName(cbaName); + cbaContent.setCbaVersion(cbaVersion); + cbaContent.setCbaState(cbaState); + cbaContent.setCbaDescription(cbaDescription); + cbaContent.setCbaFile(file); + cbaContentRepository.saveAndFlush(cbaContent); + return cbaContent; + } + + /** + * Get the list of Controller Blueprint archives + * @return List list with the controller blueprint archives + */ + public List getList(){ + return cbaContentRepository.findAll(); + } + + /** + * Get a single Controller Blueprint archive by uuID + * @param uuID the userID controller blueprint identifier + * @return Optional + */ + public Optional findByUUID(String uuID) { + return cbaContentRepository.findById(uuID); + } + + /** + * Method deleteCBAById: Delete a CBA in data base with it associated Blueprint Model + * @param uuid the uuid that identify the CBA + */ + public void deleteCBAById(String uuid) { + cbaContentRepository.deleteById(uuid); + } + +} diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaFileManagementService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaFileManagementService.java new file mode 100755 index 000000000..2011172dc --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaFileManagementService.java @@ -0,0 +1,128 @@ +/* + * Copyright © 2018 IBM Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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 com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException; +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException; +import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils; +import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.codec.multipart.FilePart; +import org.springframework.stereotype.Service; +import org.springframework.util.FileSystemUtils; +import org.springframework.util.StringUtils; +import reactor.core.publisher.Mono; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +/** + * CbaFileManagementService.java Purpose: Provide Service processing CBA file management + * + * @author Steve Siani + * @version 1.0 + */ +@Service +public class CbaFileManagementService { + private static EELFLogger log = EELFManager.getInstance().getLogger(CbaFileManagementService.class); + + @Value("${controllerblueprints.loadCbaExtension}") + private String cbaExtension; + + private static final String CBA_FILE_NAME_PATTERN = "CBA_{0}_{1}"; + + + /** + * cleanupSavedCBA: This method cleanup the Zip file and the unzip directory that was added. + * + * @param zipFileName zipFileName + * @param cbaFileLocation cbaFileLocation + * @return + * @throws BluePrintException BluePrintException + */ + public void cleanupSavedCBA(String zipFileName, Path cbaFileLocation) throws BluePrintException { + + String fileNameWithoutExtension = BluePrintFileUtils.Companion.stripFileExtension(zipFileName); + + //Delete the Zip file from the repository + FileSystemUtils.deleteRecursively(BluePrintFileUtils.Companion.getBluePrintFile(zipFileName,cbaFileLocation)); + + //Delete the CBA directory from the repository + FileSystemUtils.deleteRecursively(BluePrintFileUtils.Companion.getBluePrintFile(fileNameWithoutExtension,cbaFileLocation)); + } + + /** + * This is a saveCBAFile method + * take a {@link FilePart}, transfer it to disk using a Flux of FilePart and return a {@link Mono} representing the CBA file name + * + * @param (filePart, targetDirectory) - the request part containing the file to be saved and the default directory where to save + * @return a {@link Mono} String representing the result of the operation + * @throws (BluePrintException, IOException) BluePrintException, IOException + */ + public Mono saveCBAFile(FilePart filePart, Path targetDirectory) throws BluePrintException, IOException { + + // Normalize file name + final String fileName = StringUtils.cleanPath(filePart.filename()); + + // Check if the file's extension is "CBA" + if(!StringUtils.getFilenameExtension(fileName).equals(cbaExtension)) { + throw new BluePrintException("Invalid file extension required " + cbaExtension); + } + + // Change file name to match a pattern + String changedFileName = BluePrintFileUtils.Companion.getCBAGeneratedFileName(fileName, this.CBA_FILE_NAME_PATTERN); + + // Copy file to the target location (Replacing existing file with the same name) + Path targetLocation = targetDirectory.resolve(changedFileName); + + // if a file with the same name already exists in a repository, delete and recreate it + File file = new File(targetLocation.toString()); + if (file.exists()) + file.delete(); + file.createNewFile(); + + return filePart.transferTo(file).thenReturn(changedFileName); + } + + /** + * Decompress the file into the cbaFileLocation parameter + * @param zipFileName name of the zipped file + * @param cbaFileLocation path in which the zipped file will get decompressed + * @return String the path in which the file is decompressed + * @throws BluePrintException Exception in the process + */ + public String decompressCBAFile(final String zipFileName, Path cbaFileLocation) throws BluePrintException { + + File file = BluePrintFileUtils.Companion.getBluePrintFile(zipFileName, cbaFileLocation); + try { + Path directoryPath = Files.createDirectories(cbaFileLocation.resolve(BluePrintFileUtils.Companion.stripFileExtension(zipFileName))); + BluePrintArchiveUtils.Companion.deCompress(file, directoryPath.toString()); + return directoryPath.toString(); + + } catch (BluePrintProcessorException | IOException ex) { + throw new BluePrintException(" Fail to decompress " + zipFileName, ex); + } + + } + + +} diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaService.java new file mode 100755 index 000000000..7d616a76f --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaService.java @@ -0,0 +1,192 @@ +/* + * Copyright © 2018 IBM Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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 com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.jetbrains.annotations.NotNull; +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException; +import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils; +import org.onap.ccsdk.apps.controllerblueprints.service.domain.CbaContent; +import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel; +import org.onap.ccsdk.apps.controllerblueprints.service.model.BlueprintModelResponse; +import org.onap.ccsdk.apps.controllerblueprints.service.model.ItemCbaResponse; +import org.onap.ccsdk.apps.controllerblueprints.service.utils.CbaStateEnum; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.event.EventListener; +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 reactor.core.publisher.Mono; +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +/** + * CbaService.java Purpose: Provide Service Template Service processing CbaService + * + * @author Steve Siani + * @version 1.0 + */ + +@Service +public class CbaService { + + private static EELFLogger log = EELFManager.getInstance().getLogger(CbaService.class); + + @Value("${controllerblueprints.blueprintArchivePath}") + private String cbaArchivePath; + private Path cbaLocation; + + @Autowired + private CbaFileManagementService cbaFileManagementService; + + @Autowired + private CbaToDatabaseService cbaToDatabaseService; + + + /** + * This method would be used by SpringBoot to initialize the cba location + */ + @EventListener(ApplicationReadyEvent.class) + private void initCbaService() { + this.cbaLocation = BluePrintFileUtils.Companion.getCbaStorageDirectory(cbaArchivePath); + log.info("CBA service Initiated..."); + } + + /** + * This is a uploadCBAFile method + * take a {@link FilePart}, transfer it to disk using WebFlux and return a {@link Mono} representing the result + * + * @param filePart - the request part containing the file to be saved + * @return a {@link Mono< BlueprintModelResponse >} representing the result of the operation + */ + public Mono uploadCBAFile(FilePart filePart) { + + try { + return this.cbaFileManagementService.saveCBAFile(filePart, cbaLocation).map(fileName -> { + ConfigModel configModel; + BlueprintModelResponse blueprintModelResponse = null; + + try { + String cbaDirectory = this.cbaFileManagementService.decompressCBAFile(fileName, cbaLocation); + configModel = this.cbaToDatabaseService.storeBluePrints(cbaDirectory, fileName, cbaLocation.resolve(fileName)); + blueprintModelResponse = new BlueprintModelResponse(configModel.getId(), configModel.getArtifactName(), configModel.getArtifactVersion(), configModel.getArtifactDescription(), configModel.getConfigModelCBA().getCbaUUID()); + } catch (BluePrintException be) { + Mono.error(new BluePrintException("Error loading CBA in database.", be)); + } finally { + try { + this.cbaFileManagementService.cleanupSavedCBA(fileName, cbaLocation); + } catch (BluePrintException be) { + Mono.error(new BluePrintException("Error while cleaning up.", be)); + } + } + return blueprintModelResponse; + }); + } catch (IOException | BluePrintException e) { + return Mono.error(new BluePrintException("Error uploading the CBA file in channel.", e)); + } + } + + /** + * This is a deleteCba method + * + * @param id id + * @throws BluePrintException BluePrintException + */ + public void deleteCBA(@NotNull Long id) throws BluePrintException { + this.cbaToDatabaseService.deleteCBA(id); + } + + /** + * This is a downloadCBAFile method to find the target file to download and return a file ressource using MONO + * + * @param (id) + * @return ResponseEntity + */ + public ResponseEntity downloadCBAFile(@NotNull String id) { + Optional optionalContent = this.cbaToDatabaseService.findByUUID(id); + + CbaContent cbaContent = optionalContent.get(); + + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("text/plain")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + cbaContent.getCbaName() + "\"") + .body(new ByteArrayResource(cbaContent.getCbaFile())); + } + + /** + * This is a findCBAByID method to find a CBA By the UUID + * + * @param (id) + * @return ItemCbaResponse + */ + public ItemCbaResponse findCBAByID(@NotNull String id) { + ItemCbaResponse response = new ItemCbaResponse(); + Optional optionalContent = this.cbaToDatabaseService.findByUUID(id); + + CbaContent cbaContent = optionalContent.get(); + response.setName(cbaContent.getCbaName()); + response.setState(cbaContent.getCbaState()); + response.setId(cbaContent.getCbaUUID()); + response.setVersion(cbaContent.getCbaVersion()); + response.setDescription(cbaContent.getCbaDescription()); + return response; + } + + /** + * This is a findAllCBA method to retrieve all the CBAs in Database + * + * @return List list with the controller blueprint archives + */ + public List findAllCBA() { + List responseList = new ArrayList<>(); + List cbaContents = this.cbaToDatabaseService.listCBAFiles(); + + for(CbaContent content: cbaContents){ + ItemCbaResponse response = new ItemCbaResponse(); + response.setName(content.getCbaName()); + response.setState(content.getCbaState()); + response.setId(content.getCbaUUID()); + response.setVersion(content.getCbaVersion()); + response.setDescription(content.getCbaDescription()); + + responseList.add(response); + } + return responseList; + } + + /** + * This is a findCBAByNameAndVersion method to find a CBA by Name and version + * + * @param (name, version) + * @return + * @throws BluePrintException BluePrintException + */ + public ItemCbaResponse findCBAByNameAndVersion(@NotNull String name, @NotNull String version) throws BluePrintException { + return null; + } +} \ No newline at end of file diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaToDatabaseService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaToDatabaseService.java new file mode 100755 index 000000000..34204202d --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaToDatabaseService.java @@ -0,0 +1,132 @@ +/* + * Copyright © 2018 IBM Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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 com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.apache.commons.collections.CollectionUtils; +import org.jetbrains.annotations.NotNull; +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException; +import org.onap.ccsdk.apps.controllerblueprints.service.domain.CbaContent; +import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel; +import org.onap.ccsdk.apps.controllerblueprints.service.repository.ConfigModelContentRepository; +import org.onap.ccsdk.apps.controllerblueprints.service.repository.ConfigModelRepository; +import org.onap.ccsdk.apps.controllerblueprints.service.utils.CbaStateEnum; +import org.onap.ccsdk.apps.controllerblueprints.service.utils.ConfigModelUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Optional; + +/** + * This class acts as a Rest Service that would store in the Database the Blueprints. + * @author Ruben Chang + */ + +@Service +public class CbaToDatabaseService { + + //Log used to trace the transactions using the EELFLogger class + private static EELFLogger log = EELFManager.getInstance().getLogger(CbaToDatabaseService.class); + + @Autowired + private ConfigModelRepository configModelRepository; + @Autowired + private ConfigModelContentRepository configModelContentRepository; + @Autowired + private ConfigModelCreateService configModelCreateService; + @Autowired + private CBAContentService cbaContentService; + + /** + * This method will store the blueprints into the DB on the tables CONFIG_MODEL and CONFIG_MODEL_CONTENT + * @param cbaArchiveToSave Path in which the components are stored + * @return ConfigModel The Blueprint object stored in the DB + */ + public ConfigModel storeBluePrints(String cbaDirectory, String cbaFileName, Path cbaArchiveToSave) throws BluePrintException { + log.info("*************************** storeBluePrints **********************"); + ConfigModel configModel = null; + CbaContent cbaContent; + String version = "1.0";//TODO Read these information from metadata + String description = "Initial description for CBA archive " + cbaFileName;//TODO + + List serviceTemplateDirs = ConfigModelUtils.getBlueprintNames(cbaDirectory); + if (CollectionUtils.isNotEmpty(serviceTemplateDirs)) { + for (String fileName : serviceTemplateDirs) { + try { + String bluePrintPath = cbaDirectory.concat("/").concat(fileName); + log.debug("***** Loading service template : {}", bluePrintPath); + configModel = ConfigModelUtils.getConfigModel(bluePrintPath); + + configModel = this.configModelCreateService.saveConfigModel(configModel); + + log.info("Loaded service template successfully: {}", fileName); + } catch (Exception e) { + throw new BluePrintException("Load config model " + fileName + " error : "+e.getMessage()); + } + } + } else { + throw new BluePrintException("Invalid structure. The unzipped file does not contains Blueprints"); + } + + byte[] file; + try { + file = Files.readAllBytes(cbaArchiveToSave); + } catch (IOException e) { + throw new BluePrintException("Fail to read the CBA to save in database.", e); + } + + cbaContent = this.cbaContentService.saveCBAContent(cbaFileName, version, CbaStateEnum.DRAFT.getState(), description, file); + configModel.setConfigModelCBA(cbaContent); + + return configModel; + } + + /** + * This is a deleteConfigModel method + * + * @param id id + * @throws BluePrintException BluePrintException + */ + public void deleteCBA(@NotNull Long id) throws BluePrintException { + Optional dbConfigModel = configModelRepository.findById(id); + + //TODO: Delete CBA and COnfigModel + + } + + /** + * Get a list of the controller blueprint archives + * @return List List with the controller blueprint archives + */ + public List listCBAFiles() { + return this.cbaContentService.getList(); + } + + /** + * Find a Controller Blueprint Archive by UUID + * @param uuID the User Identifier Controller Blueprint archive + * @return Optional the Controller Blueprint archive + */ + public Optional findByUUID(String uuID) { + return this.cbaContentService.findByUUID(uuID); + } +} \ No newline at end of file diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ConfigModelCreateService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ConfigModelCreateService.java old mode 100644 new mode 100755 index fa8e32b69..f31a0ceb1 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ConfigModelCreateService.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/ConfigModelCreateService.java @@ -30,6 +30,7 @@ import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant; import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate; import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils; import org.onap.ccsdk.apps.controllerblueprints.service.common.ApplicationConstants; +import org.onap.ccsdk.apps.controllerblueprints.service.domain.CbaContent; import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel; import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModelContent; import org.onap.ccsdk.apps.controllerblueprints.service.repository.ConfigModelRepository; @@ -123,7 +124,7 @@ public class ConfigModelCreateService { String artifactName = configModel.getArtifactName(); String artifactVersion = configModel.getArtifactVersion(); String author = configModel.getUpdatedBy(); - + CbaContent configModelCBA = configModel.getConfigModelCBA(); if (StringUtils.isBlank(author)) { throw new BluePrintException("Artifact Author is missing in the Service Template"); @@ -181,7 +182,7 @@ public class ConfigModelCreateService { addConfigModelContent(dbConfigModelId, configModel); // Populate Content model types - updateConfigModel = updateConfigModel(dbConfigModelId, artifactName, artifactVersion, author); + updateConfigModel = updateConfigModel(dbConfigModelId, artifactName, artifactVersion, author, configModelCBA); return updateConfigModel; @@ -220,7 +221,7 @@ public class ConfigModelCreateService { } private ConfigModel updateConfigModel(Long dbConfigModelId, String artifactName, String artifactVersion, - String author) throws BluePrintException { + String author, CbaContent configModelCBA) throws BluePrintException { ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId); // Populate tags from metadata @@ -234,6 +235,7 @@ public class ConfigModelCreateService { dbConfigModel.setUpdatedBy(author); dbConfigModel.setPublished(ApplicationConstants.ACTIVE_N); dbConfigModel.setTags(tags); + dbConfigModel.setConfigModelCBA(configModelCBA); configModelRepository.saveAndFlush(dbConfigModel); log.info("Config model ({}) saved successfully.", dbConfigModel.getId()); return dbConfigModel; diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/CbaContent.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/CbaContent.java new file mode 100755 index 000000000..14ac6af1d --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/CbaContent.java @@ -0,0 +1,114 @@ +/* + * Copyright © 2018 IBM 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.domain; + +import com.fasterxml.jackson.annotation.JsonManagedReference; +import org.hibernate.annotations.Proxy; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +/** + * CbaContent.java Purpose: Provide Configuration Generator for CbaContent Entity + * + * @author Ruben Chang + * @version 1.0 + */ + +@EntityListeners({AuditingEntityListener.class}) +@Entity +@Table(name = "CBA_CONTENT") +@Proxy(lazy=false) +public class CbaContent implements Serializable { + + private static final long serialVersionUID = 1L; + + public CbaContent() { + this.cbaUUID = UUID.randomUUID().toString(); + } + + @Id + @Column(name = "cba_uuid", nullable = false) + private String cbaUUID; + + @Lob + @Column(name = "cba_file") + private byte[] cbaFile; + + @Column(name = "cba_name") + private String cbaName; + + @Column(name = "cba_version") + private String cbaVersion; + + @Column(name = "cba_state") + private int cbaState; + + @Column(name="cba_description") + private String cbaDescription; + + @OneToMany(mappedBy = "configModelCBA", fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL) + @JsonManagedReference + private List models = new ArrayList<>(); + + public String getCbaUUID() { + return cbaUUID; + } + + public void setCbaUUID(String cbaUUID) { + this.cbaUUID = cbaUUID; + } + + public String getCbaName() { + return cbaName; + } + + public void setCbaName(String cbaName) { + this.cbaName = cbaName; + } + + public String getCbaVersion() { + return cbaVersion; + } + + public void setCbaVersion(String cbaVersion) { + this.cbaVersion = cbaVersion; + } + + public List getModels() { + return models; + } + + public void setModels(List models) { this.models = models; } + + public int getCbaState() { return cbaState; } + + public void setCbaState(int cbaState) { this.cbaState = cbaState; } + + public String getCbaDescription() { return cbaDescription; } + + public void setCbaDescription(String cbaDescription) { this.cbaDescription = cbaDescription; } + + public byte[] getCbaFile() { return cbaFile; } + + public void setCbaFile(byte[] cbaFile) { this.cbaFile = cbaFile; } + +} diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/ConfigModel.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/ConfigModel.java old mode 100644 new mode 100755 index 51c9a7c6a..dea5757d2 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/ConfigModel.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/ConfigModel.java @@ -119,6 +119,10 @@ public class ConfigModel implements Serializable { @JsonManagedReference private List configModelContents = new ArrayList<>(); + @ManyToOne + @JoinColumn(name = "cba_content_uuid") + private CbaContent configModelCBA; + public Long getId() { return id; } @@ -287,4 +291,12 @@ public class ConfigModel implements Serializable { this.configModelContents = configModelContents; } + public CbaContent getConfigModelCBA() { + return configModelCBA; + } + + public void setConfigModelCBA(CbaContent configModelCBA) { + this.configModelCBA = configModelCBA; + } + } diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/model/BlueprintModelResponse.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/model/BlueprintModelResponse.java new file mode 100755 index 000000000..1b67ed823 --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/model/BlueprintModelResponse.java @@ -0,0 +1,78 @@ +/* + * Copyright © 2018 IBM Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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.model; + +/** + * BlueprintModelResponse.java Purpose: Model response for Upload CBA service + * + */ +public class BlueprintModelResponse { + private Long id; + private String name; + private String version; + private String description; + private String cbaUUID; + + public BlueprintModelResponse(Long id, String name, String version, String description, String cbaUUID) { + this.id = id; + this.name = name; + this.version = version; + this.description = description; + this.cbaUUID = cbaUUID; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getCbaUUID() { + return cbaUUID; + } + + public void setCbaUUID(String cbaUUID) { + this.cbaUUID = cbaUUID; + } +} \ No newline at end of file diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/model/ItemCbaResponse.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/model/ItemCbaResponse.java new file mode 100755 index 000000000..0752df9b6 --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/model/ItemCbaResponse.java @@ -0,0 +1,58 @@ +package org.onap.ccsdk.apps.controllerblueprints.service.model; + +/** + * CLass that would represent the response for the GET methods on the CBAService class + */ +public class ItemCbaResponse { + + private String id; + private String description; + private String name; + private int state; + private String version; + + public ItemCbaResponse() { + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getState() { + return state; + } + + public void setState(int state) { + this.state = state; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + +} diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/repository/CBAContentRepository.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/repository/CBAContentRepository.java new file mode 100755 index 000000000..273a19d66 --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/repository/CBAContentRepository.java @@ -0,0 +1,59 @@ +/* + * Copyright © 2018 IBM 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.repository; + +import org.jetbrains.annotations.NotNull; +import org.onap.ccsdk.apps.controllerblueprints.service.domain.CbaContent; +import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +/** + * CBAContentRepository.java Purpose: Provide Configuration Generator CRUD methods for CBAContent table + * + * @author Ruben Chang + * @version 1.0 + */ +@Repository +public interface CBAContentRepository extends JpaRepository { + + /** + * This is a findAll method + * @return List + */ + @Override + List findAll(); + + /** + * Returns a CbaContent based on the cbaUUID + * @param cbaUUID the CbaUUID + * @return Optional + */ + @Override + @NotNull + Optional findById(@NotNull String cbaUUID); + + /** + * This is a deleteById methid + * @param cbaUUID the user ID for a particular CBAFile + */ + @Override + void deleteById(@NotNull String cbaUUID); +} diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/rs/CbaRest.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/rs/CbaRest.java new file mode 100755 index 000000000..4608b1755 --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/rs/CbaRest.java @@ -0,0 +1,84 @@ +/* + * Copyright © 2018 IBM Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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.CbaService; +import org.onap.ccsdk.apps.controllerblueprints.service.model.BlueprintModelResponse; +import org.onap.ccsdk.apps.controllerblueprints.service.model.ItemCbaResponse; +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.http.codec.multipart.Part; +import org.springframework.web.bind.annotation.*; +import reactor.core.publisher.Flux; + +import java.util.List; + +/** + * CbaRest.java Purpose: Provide a REST API to upload single and multiple CBA + * + * @author Steve Siani + * @version 1.0 + */ +@RestController +@RequestMapping(value = "/api/v1/cba") +public class CbaRest { + + @Autowired + private CbaService cbaService; + + @PostMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public Flux uploadCBA(@RequestBody Flux parts) { + return parts.filter(part -> part instanceof FilePart) // only retain file parts + .ofType(FilePart.class) // convert the flux to FilePart + .flatMap(filePart -> cbaService.uploadCBAFile(filePart)); // save each file and flatmap it to a flux of results + } + + @DeleteMapping(path = "/{id}") + public void deleteCBA(@PathVariable(value = "id") Long id) throws BluePrintException { + this.cbaService.deleteCBA(id); + } + + @GetMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody + ItemCbaResponse getCBA(@PathVariable(value = "id") String id) { + return this.cbaService.findCBAByID(id); + } + + @GetMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody + List getAllCBA() { + return this.cbaService.findAllCBA(); + } + + @GetMapping(path = "/by-name/{name}/version/{version}", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody + ItemCbaResponse getCBAByNameAndVersion(@PathVariable(value = "name") String name, + @PathVariable(value = "version") String version) throws BluePrintException { + return this.cbaService.findCBAByNameAndVersion(name, version); + } + + @GetMapping(path = "/download/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody + ResponseEntity downloadCBA(@PathVariable(value = "id") String id) { + return this.cbaService.downloadCBAFile(id); + } +} diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/utils/CbaStateEnum.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/utils/CbaStateEnum.java new file mode 100755 index 000000000..57785dd8c --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/utils/CbaStateEnum.java @@ -0,0 +1,15 @@ +package org.onap.ccsdk.apps.controllerblueprints.service.utils; + +public enum CbaStateEnum { + + DRAFT(0), VALIDATED(1), APPROVED(2); + int state; + + CbaStateEnum(int state) { + this.state = state; + } + + public int getState() { + return state; + } +} diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/utils/CloseCondition.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/utils/CloseCondition.java new file mode 100755 index 000000000..d7b4aa938 --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/utils/CloseCondition.java @@ -0,0 +1,36 @@ +package org.onap.ccsdk.apps.controllerblueprints.service.utils; + +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +public class CloseCondition { + + AtomicInteger tasksSubmitted = new AtomicInteger(0); + AtomicInteger tasksCompleted = new AtomicInteger(0); + AtomicBoolean allTaskssubmitted = new AtomicBoolean(false); + + /** + * notify all tasks have been subitted, determine of the file channel can be closed + * @return true if the asynchronous file stream can be closed + */ + public boolean canCloseOnComplete() { + allTaskssubmitted.set(true); + return tasksCompleted.get() == tasksSubmitted.get(); + } + + /** + * notify a task has been submitted + */ + public void onTaskSubmitted() { + tasksSubmitted.incrementAndGet(); + } + + /** + * notify a task has been completed + * @return true if the asynchronous file stream can be closed + */ + public boolean onTaskCompleted() { + boolean allSubmittedClosed = tasksSubmitted.get() == tasksCompleted.incrementAndGet(); + return allSubmittedClosed && allTaskssubmitted.get(); + } +} diff --git a/ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaFileManagementServiceTest.java b/ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaFileManagementServiceTest.java new file mode 100755 index 000000000..e3cea3800 --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/CbaFileManagementServiceTest.java @@ -0,0 +1,89 @@ +/* + * Copyright © 2018 IBM Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * 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 org.junit.*; +import org.junit.runner.RunWith; +import org.onap.ccsdk.apps.controllerblueprints.TestApplication; +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException; +import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.FileSystemUtils; +import java.nio.file.Path; + + +/** + * CbaFileManagementServiceTest.java Purpose: Test the decompressing method of CbaCompressionService + * + * @author Vinal Patel + * @version 1.0 + */ + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ContextConfiguration(classes = {TestApplication.class}) +public class CbaFileManagementServiceTest { + + @Value("${controllerblueprints.loadBlueprintsExamplesPath}") + private String cbaPath; + private String zipfile; + private String directorypath; + private Path zipfilepath; + + @Autowired + CbaFileManagementService cbaCompressionService; + + + /** + * + */ + @Before + public void setUp() { + try { + zipfilepath = BluePrintFileUtils.Companion.getCbaStorageDirectory(cbaPath); + } catch (Exception e) { + e.printStackTrace(); + } + zipfile = "CBA_Zip_Test.zip"; + directorypath = zipfilepath.resolve(zipfile.substring(0,zipfile.lastIndexOf("."))).toAbsolutePath().toString(); + } + @After + public void clenup() throws BluePrintException { + + try { + //Delete the Zip file from the repository + FileSystemUtils.deleteRecursively(BluePrintFileUtils.Companion.getBluePrintFile(directorypath, zipfilepath)); + } + catch (Exception ex){ + throw new BluePrintException("Fail while cleaning up CBA saved!", ex); + } + } + + /** + * @throws BluePrintException + * Test will get success if it is able to decompress CBA file and returns the folder path + */ + @Test + public void testDecompressCBAFile_success() throws BluePrintException { + Assert.assertEquals(directorypath,cbaCompressionService.decompressCBAFile(zipfile,zipfilepath)); + } + +} diff --git a/ms/controllerblueprints/modules/service/src/test/resources/application.properties b/ms/controllerblueprints/modules/service/src/test/resources/application.properties old mode 100644 new mode 100755 index 718616bbd..4e1bedf23 --- a/ms/controllerblueprints/modules/service/src/test/resources/application.properties +++ b/ms/controllerblueprints/modules/service/src/test/resources/application.properties @@ -32,4 +32,10 @@ controllerblueprints.loadBluePrintPaths=./../../../../components/model-catalog/b controllerblueprints.loadModelType=false controllerblueprints.loadModeTypePaths=./../../../../components/model-catalog/definition-type/starter-type controllerblueprints.loadResourceDictionary=false -controllerblueprints.loadResourceDictionaryPaths=./../../../../components/model-catalog/resource-dictionary/starter-dictionary \ No newline at end of file +controllerblueprints.loadResourceDictionaryPaths=./../../../../components/model-catalog/resource-dictionary/starter-dictionary + +# CBA file extension +controllerblueprints.loadCbaExtension=zip + +# CBA examples for tests cases +controllerblueprints.loadBlueprintsExamplesPath=./../../../../components/model-catalog/blueprint-model/test-blueprints \ No newline at end of file -- cgit 1.2.3-korg