diff options
Diffstat (limited to 'openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration')
12 files changed, 1184 insertions, 0 deletions
diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/BaseOrchestrationTemplateHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/BaseOrchestrationTemplateHandler.java new file mode 100644 index 0000000000..c7fb43770d --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/BaseOrchestrationTemplateHandler.java @@ -0,0 +1,100 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration; + +import org.apache.commons.collections4.MapUtils; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService; +import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; +import org.openecomp.sdc.versioning.dao.types.Version; + +import java.io.InputStream; +import java.util.Optional; +import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters; +public abstract class BaseOrchestrationTemplateHandler implements OrchestrationTemplateFileHandler { + protected static final Logger logger = + LoggerFactory.getLogger(BaseOrchestrationTemplateHandler.class); + protected static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage(); + + @Override + public UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload, String user, + CandidateService candidateService, VspDetails vspDetails) { + UploadFileResponse uploadFileResponse = new UploadFileResponse(); + uploadFileResponse.setOnboardingType(getHandlerType()); + if (isNotEmptyFileToUpload(fileToUpload, uploadFileResponse, candidateService)) { + return uploadFileResponse; + } + + byte[] uploadedFileData = FileUtils.toByteArray(fileToUpload); + if (isInvalidRawZipData(uploadFileResponse, uploadedFileData, candidateService)) { + return uploadFileResponse; + } + + Optional<FileContentHandler> optionalContentMap = + getFileContentMap(uploadFileResponse, uploadedFileData); + if (!optionalContentMap.isPresent()) { + logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(), getHandlerType().toString())); + uploadFileResponse + .addStructureError( + SdcCommon.UPLOAD_FILE, + new ErrorMessage(ErrorLevel.ERROR, + getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(), getHandlerType().toString()))); + + mdcDataDebugMessage.debugExitMessage("VSP id", vspId); + return uploadFileResponse; + } + + if (!MapUtils.isEmpty(uploadFileResponse.getErrors())) { + + mdcDataDebugMessage.debugExitMessage("VSP id", vspId); + return uploadFileResponse; + } + if (updateCandidateData(vspId, user, candidateService, vspDetails, uploadFileResponse, + uploadedFileData, optionalContentMap)) + return uploadFileResponse; + + mdcDataDebugMessage.debugExitMessage("VSP id", vspId); + return uploadFileResponse; + + } + + protected abstract boolean updateCandidateData(String vspId, String user, CandidateService candidateService, + VspDetails vspDetails, UploadFileResponse uploadFileResponse, + byte[] uploadedFileData, + Optional<FileContentHandler> optionalContentMap); + + + private boolean isNotEmptyFileToUpload(InputStream fileToUpload, + UploadFileResponse uploadFileResponse, CandidateService candidateService) { + Optional<ErrorMessage> errorMessage = + candidateService.validateNonEmptyFileToUpload(fileToUpload); + if (errorMessage.isPresent()) { + uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage.get()); + return true; + } + return false; + } + + protected boolean isInvalidRawZipData(UploadFileResponse uploadFileResponse, + byte[] uploadedFileData, CandidateService candidateService) { + Optional<ErrorMessage> errorMessage; + errorMessage = candidateService.validateRawZipData(uploadedFileData); + if (errorMessage.isPresent()) { + uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage.get()); + return true; + } + return false; + } + + public abstract Optional<FileContentHandler> getFileContentMap(UploadFileResponse uploadFileResponse, + byte[] uploadedFileData); + protected abstract OnboardingTypesEnum getHandlerType(); +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java new file mode 100644 index 0000000000..53d34749e7 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java @@ -0,0 +1,154 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration; + +import org.apache.commons.lang3.tuple.Pair; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.common.utils.CommonUtil; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.OnboardingManifest; +import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService; +import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.*; +public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler + implements OrchestrationTemplateFileHandler { + + + @Override + public Optional<FileContentHandler> getFileContentMap(UploadFileResponse uploadFileResponse, + byte[] uploadedFileData) { + FileContentHandler contentMap = null; + List<String> folderList = new ArrayList<>(); + try { + Pair<FileContentHandler, List<String>> fileContentMapFromOrchestrationCandidateZip = CommonUtil.getFileContentMapFromOrchestrationCandidateZip(uploadedFileData); + contentMap = fileContentMapFromOrchestrationCandidateZip.getKey(); + folderList = fileContentMapFromOrchestrationCandidateZip.getRight(); + } catch (IOException exception) { + uploadFileResponse.addStructureError( + SdcCommon.UPLOAD_FILE, + new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage())); + } catch (CoreException coreException) { + uploadFileResponse.addStructureError( + SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage())); + } + validateContent(uploadFileResponse, contentMap, folderList); + return Optional.ofNullable(contentMap); + } + + private void validateContent(UploadFileResponse uploadFileResponse, FileContentHandler contentMap, List<String> folderList) { + validateManifest(uploadFileResponse, contentMap); + validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME); + validateNoExtraFiles(uploadFileResponse, contentMap); + validateFolders(uploadFileResponse, folderList); + } + + private void validateManifest(UploadFileResponse uploadFileResponse, FileContentHandler contentMap) { + if (!validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)){ + return; + } + InputStream fileContent = contentMap.getFileContent(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME); + OnboardingManifest onboardingManifest = new OnboardingManifest(fileContent); + if (!onboardingManifest.isValid()){ + onboardingManifest.getErrors().forEach(error -> uploadFileResponse.addStructureError( + SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, error))); + } + } + + private void validateNoExtraFiles(UploadFileResponse uploadFileResponse, FileContentHandler contentMap) { + List<String> unwantedFiles = contentMap.getFileList().stream() + .filter(this::filterFiles).collect(Collectors.toList()); + if (!unwantedFiles.isEmpty()) { + unwantedFiles.stream().filter(this::filterFiles).forEach(unwantedFile -> + uploadFileResponse.addStructureError( + SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, + getErrorWithParameters(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage(), + unwantedFile)))); + + ; + } + } + + private void validateFolders(UploadFileResponse uploadFileResponse, List<String> folderList) { + List<String> filterResult = folderList.stream().filter(this::filterFolders).collect(Collectors.toList()); + if (!filterResult.isEmpty()) { + folderList.stream().filter(this::filterFolders).forEach( unwantedFolder -> + uploadFileResponse.addStructureError( + SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, + getErrorWithParameters(Messages.CSAR_DIRECTORIES_NOT_ALLOWED.getErrorMessage(), + unwantedFolder)))); + + } + } + private boolean filterFiles(String inFileName) { + boolean valid = ELIGIBLE_FILES.stream().anyMatch(fileName -> fileName.equals(inFileName)); + if (valid){ + return !valid; + } + return filterFolders(inFileName); + } + + private boolean filterFolders(String fileName) { + return !ELIGBLE_FOLDERS.stream().anyMatch(dirName -> fileName.startsWith(dirName)); + } + + private boolean validateFileExist(UploadFileResponse uploadFileResponse, FileContentHandler contentMap, String fileName) { + + boolean containsFile = contentMap.containsFile(fileName); + if (!containsFile) { + uploadFileResponse.addStructureError( + SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, + getErrorWithParameters(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage(), fileName))); + } + return containsFile; + } + + @Override + protected boolean updateCandidateData(String vspId, String user, CandidateService candidateService, + VspDetails vspDetails, UploadFileResponse uploadFileResponse, + byte[] uploadedFileData, Optional<FileContentHandler> optionalContentMap) { + try { + candidateService.updateCandidateUploadData(new OrchestrationTemplateCandidateData( + ByteBuffer.wrap(uploadedFileData), ""), vspDetails.getId()); + } catch (Exception exception) { + logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(), + getHandlerType().toString()), exception); + uploadFileResponse + .addStructureError( + SdcCommon.UPLOAD_FILE, + new ErrorMessage(ErrorLevel.ERROR, exception.getMessage())); + + mdcDataDebugMessage.debugExitMessage("VSP id", vspId); + return true; + } + return false; + } + + + @Override + protected OnboardingTypesEnum getHandlerType() { + return OnboardingTypesEnum.CSAR; + } + + @Override + protected boolean isInvalidRawZipData(UploadFileResponse uploadFileResponse, + byte[] uploadedFileData, CandidateService candidateService) { + return super.isInvalidRawZipData(uploadFileResponse, uploadedFileData, candidateService); + + } +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateFileHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateFileHandler.java new file mode 100644 index 0000000000..3f7d18de9f --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateFileHandler.java @@ -0,0 +1,12 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration; + +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService; +import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; +import org.openecomp.sdc.versioning.dao.types.Version; + +import java.io.InputStream; +public interface OrchestrationTemplateFileHandler { + UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload, String user, + CandidateService candidateService, VspDetails vspDetails); +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateZipHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateZipHandler.java new file mode 100644 index 0000000000..626d941911 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateZipHandler.java @@ -0,0 +1,55 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration; + +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService; +import org.openecomp.sdc.vendorsoftwareproduct.services.utils.CandidateEntityBuilder; +import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; + +import java.util.Optional; + +import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters; +public class OrchestrationTemplateZipHandler extends BaseOrchestrationTemplateHandler + implements OrchestrationTemplateFileHandler { + + @Override + public Optional<FileContentHandler> getFileContentMap(UploadFileResponse uploadFileResponse, + byte[] uploadedFileData) { + return OrchestrationUtil.getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse, uploadedFileData); + } + + @Override + protected boolean updateCandidateData(String vspId, String user, CandidateService candidateService, + VspDetails vspDetails, UploadFileResponse uploadFileResponse, + byte[] uploadedFileData, Optional<FileContentHandler> optionalContentMap) { + try { + OrchestrationTemplateCandidateData candidateData = + new CandidateEntityBuilder(candidateService) + .buildCandidateEntityFromZip(vspDetails, uploadedFileData, optionalContentMap.get(), + uploadFileResponse.getErrors(), user); + candidateService.updateCandidateUploadData(candidateData, vspDetails.getId()); + } catch (Exception exception) { + logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(), + getHandlerType().toString()),exception); + uploadFileResponse + .addStructureError( + SdcCommon.UPLOAD_FILE, + new ErrorMessage(ErrorLevel.ERROR, exception.getMessage())); + + mdcDataDebugMessage.debugExitMessage("VSP id", vspId); + return true; + } + return false; + } + + @Override + protected OnboardingTypesEnum getHandlerType() { + return OnboardingTypesEnum.ZIP; + } +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationUploadFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationUploadFactory.java new file mode 100644 index 0000000000..85f92662ea --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationUploadFactory.java @@ -0,0 +1,29 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration; + +import org.openecomp.config.api.Configuration; +import org.openecomp.config.api.ConfigurationManager; +import org.openecomp.core.utilities.CommonMethods; +import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil.ORCHESTRATION_CONFIG_NAMESPACE; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil.ORCHESTRATION_IMPL_KEY; + +public class OrchestrationUploadFactory { + private static Map<String, ImplementationConfiguration> fileHanlders; + + static { + Configuration config = ConfigurationManager.lookup(); + fileHanlders = new ConcurrentHashMap<>(config.populateMap(ORCHESTRATION_CONFIG_NAMESPACE, + ORCHESTRATION_IMPL_KEY, ImplementationConfiguration.class)); + + } + + public static final OrchestrationTemplateFileHandler createOrchestrationTemplateFileHandler(String filePrefix) { + ImplementationConfiguration orchestrationTemplateFileHandler = fileHanlders.get(filePrefix); + return CommonMethods.newInstance(orchestrationTemplateFileHandler.getImplementationClass(), + OrchestrationTemplateFileHandler.class); + } +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationUtil.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationUtil.java new file mode 100644 index 0000000000..9d7421f336 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationUtil.java @@ -0,0 +1,282 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration; + +import org.apache.commons.collections4.CollectionUtils; +import org.openecomp.core.model.dao.ServiceModelDao; +import org.openecomp.core.model.dao.ServiceModelDaoFactory; +import org.openecomp.core.util.UniqueValueUtil; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.common.utils.CommonUtil; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; +import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList; +import org.openecomp.sdc.heat.services.tree.HeatTreeManager; +import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.MonitoringUploadDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadData; +import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionDataExtractorFactory; +import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionEntityDataManagerFactory; +import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor; +import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager; +import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; +import org.openecomp.sdc.versioning.dao.types.Version; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.GENERAL_COMPONENT_ID; +import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME; + +public class OrchestrationUtil { + + public static final String ORCHESTRATION_CONFIG_NAMESPACE = "orchestration"; + public static final String ORCHESTRATION_IMPL_KEY = "orchestration_impl"; + + + private static VendorSoftwareProductDao vendorSoftwareProductDao = + VendorSoftwareProductDaoFactory.getInstance().createInterface(); + private static NicDao nicDao = NicDaoFactory.getInstance().createInterface(); + private static ComponentArtifactDao componentArtifactDao = MonitoringUploadDaoFactory.getInstance().createInterface(); + private static ProcessDao processDao = ProcessDaoFactory.getInstance().createInterface(); + private static OrchestrationTemplateDao orchestrationTemplateDataDao = + OrchestrationTemplateDaoFactory.getInstance().createInterface(); + private static ComponentDao componentDao = + ComponentDaoFactory.getInstance().createInterface(); + private static ServiceModelDao serviceModelDao = ServiceModelDaoFactory.getInstance() + .createInterface(); + private static CompositionEntityDataManager compositionEntityDataManager = + CompositionEntityDataManagerFactory.getInstance().createInterface(); + private static CompositionDataExtractor compositionDataExtractor = + CompositionDataExtractorFactory.getInstance().createInterface(); + + public static Optional<FileContentHandler> getFileContentMap(OnboardingTypesEnum type, + UploadFileResponse uploadFileResponse, + byte[] uploadedFileData) { + FileContentHandler contentMap = null; + try { + contentMap = CommonUtil.validateAndUploadFileContent(type, uploadedFileData); + } catch (IOException exception) { + uploadFileResponse.addStructureError( + SdcCommon.UPLOAD_FILE, + new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage())); + } catch (CoreException coreException) { + uploadFileResponse.addStructureError( + SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage())); + } + return Optional.ofNullable(contentMap); + } + + public static void backupComponentsQuestionnaireBeforeDelete(String vspId, Version activeVersion, + Map<String, String> componentsQustanniare, + Map<String, Map<String, String>> + componentNicsQustanniare, + Map<String, Collection<ComponentMonitoringUploadEntity>> + componentMibList, + Map<String, Collection<ProcessEntity>> + componentProcesses, + Map<String, ProcessEntity> processArtifact) { + //backup VSP processes + backupProcess(vspId, activeVersion, GENERAL_COMPONENT_ID, GENERAL_COMPONENT_ID, + componentProcesses, processArtifact); + Collection<ComponentEntity> componentsCompositionAndQuestionnaire = vendorSoftwareProductDao + .listComponentsCompositionAndQuestionnaire(vspId, + activeVersion); + componentsCompositionAndQuestionnaire.forEach(componentEntity -> { + String componentName = componentEntity.getComponentCompositionData().getName(); + componentsQustanniare.put(componentName, componentEntity + .getQuestionnaireData()); + Collection<NicEntity> + nics = nicDao.list(new NicEntity(vspId, activeVersion, componentEntity.getId(), null)); + //backup mib + Collection<ComponentMonitoringUploadEntity> componentMib = + componentArtifactDao.listArtifacts(new + ComponentMonitoringUploadEntity(vspId, activeVersion, componentEntity.getId(), + null)); + if (CollectionUtils.isNotEmpty(componentMib)) { + componentMibList.put(componentName,componentMib); + } + + //backup component processes + backupProcess(vspId, activeVersion, componentEntity.getId(), componentName, + componentProcesses, processArtifact); + if (CollectionUtils.isNotEmpty(nics)) { + Map<String, String> nicsQustanniare = new HashMap<>(); + nics.forEach(nicEntity -> { + NicEntity nic = nicDao.get(new NicEntity(vspId, activeVersion, componentEntity.getId(), + nicEntity.getId())); + NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, activeVersion, + componentEntity.getId(), nicEntity.getId()); + + nicsQustanniare + .put(nicEntity.getNicCompositionData().getName(), + nicQuestionnaire.getQuestionnaireData()); + }); + componentNicsQustanniare.put(componentName, nicsQustanniare); + } + }); + } + + private static void backupProcess(String vspId, Version activeVersion, String componentId, + String componentName, Map<String, + Collection<ProcessEntity>> processes, + Map<String, ProcessEntity> processArtifact) { + Collection<ProcessEntity> processList = vendorSoftwareProductDao.listProcesses(vspId, + activeVersion, componentId); + if (!processList.isEmpty()) { + processes.put(componentName, processList); + processList.forEach(process -> { + //ProcessArtifactEntity artifact = vendorSoftwareProductDao.getProcessArtifact(vspId, + // activeVersion, componentId, process.getId()); + ProcessEntity artifact = + processDao.get(new ProcessEntity(vspId, activeVersion, componentId, process.getId())); + if (artifact.getArtifact() != null) { + processArtifact.put(process.getId(), artifact); + } + }); + } + } + + public static void retainComponentQuestionnaireData(String vspId, Version activeVersion, + Map<String, String> componentsQustanniare, + Map<String, Map<String, String>> + componentNicsQustanniare, + Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList, + Map<String, Collection<ProcessEntity>> processes, + Map<String, ProcessEntity> processArtifact) { + //VSP processes + restoreProcess(vspId, activeVersion, GENERAL_COMPONENT_ID, GENERAL_COMPONENT_ID, processes, + processArtifact); + Collection<ComponentEntity> + components = vendorSoftwareProductDao.listComponents(vspId, activeVersion); + components.forEach(componentEntity -> { + String componentName = componentEntity.getComponentCompositionData().getName(); + if (componentsQustanniare.containsKey(componentName)) { + componentDao.updateQuestionnaireData(vspId, activeVersion, + componentEntity.getId(), + componentsQustanniare.get(componentEntity.getComponentCompositionData() + .getName())); + if (componentNicsQustanniare.containsKey(componentName)) { + Map<String, String> nicsQustanniare = componentNicsQustanniare.get(componentName); + Collection<NicEntity> + nics = + nicDao.list(new NicEntity(vspId, activeVersion, componentEntity.getId(), null)); + nics.forEach(nicEntity -> { + if (nicsQustanniare.containsKey(nicEntity.getNicCompositionData().getName())) { + nicDao.updateQuestionnaireData(vspId, activeVersion, + componentEntity.getId(), nicEntity.getId(), + nicsQustanniare.get(nicEntity.getNicCompositionData().getName())); + } + }); + } + //MIB //todo add for VES_EVENTS + if (componentMibList.containsKey(componentName)) { + Collection<ComponentMonitoringUploadEntity> mibList = + componentMibList.get(componentName); + mibList.forEach(mib -> { + mib.setComponentId(componentEntity.getId()); + componentArtifactDao.create(mib); + }); + } + //VFC processes + restoreProcess(vspId, activeVersion, componentEntity.getId(), componentName, processes, + processArtifact); + } + }); + } + + private static void restoreProcess(String vspId, Version activeVersion, String componentId, + String componentName, + Map<String, Collection<ProcessEntity>> processes, + Map<String, ProcessEntity> processArtifact) { + if (processes.containsKey(componentName)) { + Collection<ProcessEntity> processList = processes.get(componentName); + processList.forEach(process -> { + //Reatin VFC process + if (!GENERAL_COMPONENT_ID.equals(componentId) && processArtifact.containsKey(process.getId + ())) { + ProcessEntity artifact = processArtifact.get(process.getId()); + artifact.setComponentId(componentId); + UniqueValueUtil.createUniqueValue(PROCESS_NAME, vspId, activeVersion.toString(), + componentId, process.getName()); + vendorSoftwareProductDao.createProcess(artifact); + } + }); + } + } + + public static void deleteUploadDataAndContent(String vspId, Version version) { + //fixme change this when more tables are zusammenized + vendorSoftwareProductDao.deleteUploadData(vspId, version); + } + + public static void saveUploadData(String vspId, Version activeVersion, InputStream uploadedFileData, + FileContentHandler fileContentMap, HeatStructureTree tree) { + Map<String, Object> manifestAsMap = + fileContentMap.containsFile(SdcCommon.MANIFEST_NAME) ? + (Map<String, Object>) JsonUtil.json2Object(fileContentMap.getFileContent( + SdcCommon.MANIFEST_NAME), Map.class) + : new HashMap<>(); + + UploadData uploadData = new UploadData(); + uploadData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(uploadedFileData))); + uploadData.setValidationDataStructure(new ValidationStructureList(tree)); + uploadData.setPackageName(Objects.isNull(manifestAsMap.get("name")) ? null : + (String) manifestAsMap.get("name")); + uploadData.setPackageVersion(Objects.isNull(manifestAsMap.get("version")) ? null : + (String) manifestAsMap.get("version")); + orchestrationTemplateDataDao.updateOrchestrationTemplateData(vspId, uploadData); + } + + public static void saveServiceModel(String vspId, + Version version, + ToscaServiceModel serviceModelToExtract, + ToscaServiceModel serviceModelToStore){ + if (serviceModelToExtract != null) { + serviceModelDao.storeServiceModel(vspId, version, serviceModelToStore); + //Extracting the compostion data from the output service model of the first phase of + // translation + compositionEntityDataManager.saveCompositionData(vspId, version, + compositionDataExtractor.extractServiceCompositionData(serviceModelToExtract)); +// OrchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire, +// componentNicsQuestionnaire, componentMibList, processes, processArtifact); + } + } + + public static HeatStructureTree createHeatTree(FileContentHandler fileContentMap, + Map<String, List<ErrorMessage>> validationErrors){ + HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap); + heatTreeManager.createTree(); + heatTreeManager.addErrors(validationErrors); + return heatTreeManager.getTree(); + } + +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/CSARConstants.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/CSARConstants.java new file mode 100644 index 0000000000..0d204ef2c0 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/CSARConstants.java @@ -0,0 +1,18 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar; + +import com.google.common.collect.ImmutableSet; +import static com.google.common.collect.ImmutableSet.of; +public class CSARConstants { + + public static final ImmutableSet<String> ELIGBLE_FOLDERS = of("Artifacts/","Definitions/", + "Licenses/", "TOSCA-Metadata/"); + + public static final String MAIN_SERVICE_TEMPLATE_MF_FILE_NAME = "MainServiceTemplate.mf"; + public static final String MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME = "MainServiceTemplate.yaml"; + public static final ImmutableSet<String> ELIGIBLE_FILES = + of(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME,MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME); + + public static final String METADATA_MF_ATTRIBUTE = "metadata"; + public static final String SOURCE_MF_ATTRIBUTE = "source"; + public static final String SEPERATOR_MF_ATTRIBUTE = ":"; +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/OnboardingManifest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/OnboardingManifest.java new file mode 100644 index 0000000000..d88d883e67 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/OnboardingManifest.java @@ -0,0 +1,141 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.apache.commons.lang.StringUtils; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.util.*; + +import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.*; + +public class OnboardingManifest { + private static final Logger logger = LoggerFactory.getLogger(OnboardingManifest.class); + private Map<String, String> metadata; + private List<String> sources; + private List<String> errors; + private State state; + private enum State { + Start, ProcessMetadata, ProcessSources, Error + } + + public OnboardingManifest(InputStream is) { + errors = new ArrayList<>(); + sources = new ArrayList<>(); + metadata = new HashMap<>(); + parseManifest(is); + } + + private void parseManifest(InputStream is) { + try { + ImmutableList<String> lines = readAllLines(is); + state = State.Start; + + for (String line : lines) { + line = line.trim(); + if (!StringUtils.isEmpty(line.trim())) { + state = processLine(state, line); + } + } + if (errors.isEmpty()) { + if (metadata.isEmpty()) { + errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage()); + } + if (sources.isEmpty()) { + errors.add(Messages.MANIFEST_NO_SOURCES.getErrorMessage()); + } + } + } catch (IOException e){ + logger.error(e.getMessage(),e); + errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage()); + } + } + + private State processLine(State state, String line) { + switch (state) { + case Start: + if (line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)) { + state = State.ProcessMetadata; + } else { + reportError(line); + } + break; + case ProcessMetadata: + String[] metaSplit = line.split(SEPERATOR_MF_ATTRIBUTE); + if (metaSplit.length < 2){ + reportError(line); + break; + } + if (!metaSplit[0].equals(SOURCE_MF_ATTRIBUTE)){ + String value = line.replace(metaSplit[0] + SEPERATOR_MF_ATTRIBUTE, "").trim(); + metadata.put(metaSplit[0],value); + } else { + state = State.ProcessSources; + processSourceLine(line); + } + break; + case ProcessSources: + processSourceLine(line); + + break; + case Error: + break; + + } return state; + } + + private void processSourceLine(String line) { + if (line.startsWith(SOURCE_MF_ATTRIBUTE+SEPERATOR_MF_ATTRIBUTE)){ + String value = line.replaceAll(SOURCE_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE, "").trim(); + sources.add(value); + }else { + reportError(line); + } + } + + private void reportError(String line) { + errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line)); + state = State.Error; + } + + private ImmutableList<String> readAllLines(InputStream is) throws IOException { + ImmutableList.Builder<String> builder = ImmutableList.<String> builder(); + try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8.newDecoder()); + BufferedReader bufferedReader = new BufferedReader(reader);) { + for (; ; ) { + String line = bufferedReader.readLine(); + if (line == null) + break; + builder.add(line); + } + } + return builder.build(); + } + + public Map<String, String> getMetadata() { + if (!isValid()){ + return Collections.EMPTY_MAP; + } + return ImmutableMap.copyOf(metadata); + } + + public List<String> getSources() { + if (!isValid()){ + return Collections.EMPTY_LIST; + } + return ImmutableList.copyOf(sources); + } + + public List<String> getErrors() { + return ImmutableList.copyOf(errors); + } + + public boolean isValid() { + return errors.isEmpty(); + } +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationProcessFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationProcessFactory.java new file mode 100644 index 0000000000..db43e0084e --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationProcessFactory.java @@ -0,0 +1,64 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process; + +import org.openecomp.config.api.Configuration; +import org.openecomp.config.api.ConfigurationManager; +import org.openecomp.core.utilities.CommonMethods; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; +import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration; +import org.openecomp.sdc.vendorsoftwareproduct.types.ConfigConstants; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; + +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil.ORCHESTRATION_CONFIG_NAMESPACE; +public class OrchestrationProcessFactory { + + private static final String confFile = "config-orchestration.json"; + private static Map<String, ImplementationConfiguration> processImplMap; + + static { + Configuration config = ConfigurationManager.lookup(); + processImplMap = new ConcurrentHashMap<>(config.populateMap(ORCHESTRATION_CONFIG_NAMESPACE, + ConfigConstants.PROCESS_IMPL_KEY, ImplementationConfiguration.class)); + + } + + public static Optional<OrchestrationTemplateProcessHandler> getInstance(String filePrefix) { + filePrefix = filePrefix == null ? null : filePrefix.toLowerCase().trim(); + if (filePrefix == null) { + return Optional.empty(); + } + + OnboardingTypesEnum onboardingTypesEnum = OnboardingTypesEnum.getOnboardingTypesEnum(filePrefix); + if (onboardingTypesEnum == null) { + return Optional.empty(); + } + + try { + return Optional.of(createInstance(processImplMap.get(onboardingTypesEnum.toString()))); + }catch (Exception e){ + return Optional.empty(); + } + } + + private static Map<String, String> getOrchestrationImplMap(){ + try { + return FileUtils.readViaInputStream(confFile, + stream -> JsonUtil.json2Object(stream, Map.class)); + }catch (Exception e){ + return new HashMap<>(); + } + } + + private static OrchestrationTemplateProcessHandler createInstance(ImplementationConfiguration implClass) + throws Exception { + OrchestrationTemplateProcessHandler handler; + handler = + CommonMethods.newInstance(implClass.getImplementationClass(), OrchestrationTemplateProcessHandler.class); + return handler; + } +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessCsarHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessCsarHandler.java new file mode 100644 index 0000000000..4866d93411 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessCsarHandler.java @@ -0,0 +1,133 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process; + +import org.apache.commons.collections4.MapUtils; +import org.openecomp.core.converter.ToscaConverter; +import org.openecomp.core.impl.ToscaConverterImpl; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; +import org.openecomp.core.validation.util.MessageContainerUtil; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; +import org.openecomp.sdc.heat.services.tree.ToscaTreeManager; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.logging.messages.AuditMessages; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory; +import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil; +import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService; +import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse; +import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; +import org.openecomp.sdc.versioning.dao.types.Version; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class OrchestrationTemplateProcessCsarHandler implements OrchestrationTemplateProcessHandler { + + private static Logger logger = + LoggerFactory.getLogger(OrchestrationTemplateProcessCsarHandler.class); + private ToscaConverter toscaConverter = new ToscaConverterImpl(); + private CandidateService candidateService = + CandidateServiceFactory.getInstance().createInterface(); + ToscaTreeManager toscaTreeManager = new ToscaTreeManager(); + + @Override + public OrchestrationTemplateActionResponse process(VspDetails vspDetails, + OrchestrationTemplateCandidateData candidateData, + String user) { + String vspId = vspDetails.getId(); + Version version = vspDetails.getVersion(); + logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.CSAR_VALIDATION_STARTED + vspId); + OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse(); + UploadFileResponse uploadFileResponse = new UploadFileResponse(); + Optional<FileContentHandler> fileContent = + OrchestrationUtil + .getFileContentMap( + OnboardingTypesEnum.CSAR, uploadFileResponse, candidateData.getContentData().array()); + + if(fileContent.isPresent()){ + try { + FileContentHandler fileContentHandler = fileContent.get(); + processCsar(vspId, version, fileContentHandler, candidateData, response); + } catch (CoreException e){ + logger.error(e.getMessage()); + throw e; + } catch (Exception e){ + logger.error(e.getMessage()); + } + } + + return response; + } + + private void processCsar(String vspId, Version version, + FileContentHandler fileContentHandler, + OrchestrationTemplateCandidateData candidateData, + OrchestrationTemplateActionResponse response) throws IOException { + + + response.setFileNames(new ArrayList<>(fileContentHandler.getFileList())); + Map<String, List<ErrorMessage>> errors = validateCsar(fileContentHandler, response); + if(!isValid(errors)){ + return; + } + + HeatStructureTree tree = toscaTreeManager.getTree(); + + Map<String, String> componentsQuestionnaire = new HashMap<>(); + Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>(); + Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>(); + Map<String, Collection<ProcessEntity>> processes = new HashMap<>(); + Map<String, ProcessEntity> processArtifact = new HashMap<>(); + + OrchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspId, + version, componentsQuestionnaire, + componentNicsQuestionnaire, componentMibList, processes, processArtifact); + + Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService + .fetchZipFileByteArrayInputStream(vspId, candidateData, null, OnboardingTypesEnum.CSAR, errors); + + OrchestrationUtil.deleteUploadDataAndContent(vspId, version); + OrchestrationUtil.saveUploadData( + vspId, version, zipByteArrayInputStream.get(), fileContentHandler, tree); + + ToscaServiceModel toscaServiceModel = toscaConverter.convert(fileContentHandler); + OrchestrationUtil.saveServiceModel(vspId, version, toscaServiceModel, toscaServiceModel); + + } + + private void addFiles(FileContentHandler fileContentHandler){ + for(Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()){ + toscaTreeManager.addFile(fileEntry.getKey(), fileEntry.getValue()); + } + } + + private Map<String, List<ErrorMessage>> validateCsar(FileContentHandler fileContentHandler, + OrchestrationTemplateActionResponse response){ + + + Map<String, List<ErrorMessage>> errors = new HashMap<>(); + addFiles(fileContentHandler); + toscaTreeManager.createTree(); + toscaTreeManager.addErrors(errors); + //todo - add tosca validation here to the existing validation framework + return errors; + } + + private boolean isValid(Map<String, List<ErrorMessage>> errors){ + return MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors)); + } +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessHandler.java new file mode 100644 index 0000000000..8b513b4347 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessHandler.java @@ -0,0 +1,12 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process; + + +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse; + +public interface OrchestrationTemplateProcessHandler { + OrchestrationTemplateActionResponse process(VspDetails vspDetails, + OrchestrationTemplateCandidateData candidateData, + String user); +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessZipHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessZipHandler.java new file mode 100644 index 0000000000..cceacd47b3 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessZipHandler.java @@ -0,0 +1,184 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; +import org.openecomp.core.model.dao.ServiceModelDao; +import org.openecomp.core.model.dao.ServiceModelDaoFactory; +import org.openecomp.core.translator.datatypes.TranslatorOutput; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; +import org.openecomp.core.validation.util.MessageContainerUtil; +import org.openecomp.sdc.activityLog.ActivityLogManager; +import org.openecomp.sdc.activityLog.ActivityLogManagerFactory; +import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage; +import org.openecomp.sdc.logging.messages.AuditMessages; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil; +import org.openecomp.sdc.validation.util.ValidationManagerUtil; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory; +import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionDataExtractorFactory; +import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionEntityDataManagerFactory; +import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil; +import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor; +import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager; +import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService; +import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse; +import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; +import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure; +import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdcrests.activitylog.types.ActivityType; + +import java.io.ByteArrayInputStream; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.openecomp.sdc.logging.messages.AuditMessages.HEAT_VALIDATION_ERROR; + +public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemplateProcessHandler { + Logger logger = LoggerFactory.getLogger(OrchestrationTemplateProcessZipHandler.class); + private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage(); + private CandidateService candidateService = + CandidateServiceFactory.getInstance().createInterface(); + private ServiceModelDao serviceModelDao = ServiceModelDaoFactory.getInstance().createInterface(); + private CompositionEntityDataManager compositionEntityDataManager = + CompositionEntityDataManagerFactory.getInstance().createInterface(); + private CompositionDataExtractor compositionDataExtractor = + CompositionDataExtractorFactory.getInstance().createInterface(); + private ActivityLogManager activityLogManager = + ActivityLogManagerFactory.getInstance().createInterface(); + + + public OrchestrationTemplateProcessZipHandler(){} + + public OrchestrationTemplateActionResponse process(VspDetails vspDetails, + OrchestrationTemplateCandidateData candidateData, + String user) { + String vspId = vspDetails.getId(); + Version version = vspDetails.getVersion(); + logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_STARTED + + vspId); + OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse(); + UploadFileResponse uploadFileResponse = new UploadFileResponse(); + Optional<FileContentHandler> fileContent = + OrchestrationUtil + .getFileContentMap( + OnboardingTypesEnum.ZIP, uploadFileResponse, candidateData.getContentData().array()); + if (!fileContent.isPresent()) { + response.addStructureErrors(uploadFileResponse.getErrors()); + mdcDataDebugMessage.debugExitMessage("VSP id", vspId); + response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList,vspId, + HEAT_VALIDATION_ERROR)); + return response; + } + + Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors(); + FileContentHandler fileContentMap = fileContent.get(); + FilesDataStructure structure = + JsonUtil.json2Object(candidateData.getFilesDataStructure(), FilesDataStructure.class); + + if (CollectionUtils.isNotEmpty(structure.getUnassigned())) { + response.addErrorMessageToMap(SdcCommon.UPLOAD_FILE, + Messages.FOUND_UNASSIGNED_FILES.getErrorMessage(), ErrorLevel.ERROR); + + mdcDataDebugMessage.debugExitMessage("VSP id", vspId); + response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList,vspId, + HEAT_VALIDATION_ERROR)); + return response; + } + + + String manifest = candidateService.createManifest(vspDetails, structure); + fileContentMap.addFile(SdcCommon.MANIFEST_NAME, manifest.getBytes()); + + Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService + .fetchZipFileByteArrayInputStream( + vspId, candidateData, manifest, OnboardingTypesEnum.ZIP, uploadErrors); + if (!zipByteArrayInputStream.isPresent()) { + response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList,vspId, + HEAT_VALIDATION_ERROR)); + return response; + } + + HeatStructureTree tree = createAndValidateHeatTree(response, fileContentMap); + + Map<String, String> componentsQuestionnaire = new HashMap<>(); + Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>(); + Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>(); + Map<String, Collection<ProcessEntity>> processes = new HashMap<>(); + Map<String, ProcessEntity> processArtifact = new HashMap<>(); + + OrchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspId, + version, componentsQuestionnaire, + componentNicsQuestionnaire, componentMibList, processes, processArtifact); + + OrchestrationUtil.deleteUploadDataAndContent(vspId, version); + OrchestrationUtil.saveUploadData(vspId, version, zipByteArrayInputStream.get(), fileContentMap, + tree); + + response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList,vspId, + HEAT_VALIDATION_ERROR)); + if ( MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, response.getErrors + ()))) { + logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_COMPLETED + vspId); + } + + logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_STARTED + vspId); + + TranslatorOutput translatorOutput = + HeatToToscaUtil.loadAndTranslateTemplateData(fileContentMap); + + ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel(); + OrchestrationUtil.saveServiceModel(vspId, version, translatorOutput + .getNonUnifiedToscaServiceModel(), toscaServiceModel); + OrchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire, + componentNicsQuestionnaire, componentMibList, processes, processArtifact); + + logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_COMPLETED + vspId); + uploadFileResponse.addStructureErrors(uploadErrors); + + ActivityLogEntity activityLogEntity = + new ActivityLogEntity(vspId, String.valueOf(version.getMajor() + 1), + ActivityType.UPLOAD_HEAT.toString(), user, true, "", ""); + activityLogManager.addActionLog(activityLogEntity, user); + + mdcDataDebugMessage.debugExitMessage("VSP id", vspId); + return response; + } + + private HeatStructureTree createAndValidateHeatTree(OrchestrationTemplateActionResponse response, + FileContentHandler fileContentMap) { + VendorSoftwareProductUtils.addFileNamesToUploadFileResponse(fileContentMap, response); + Map<String, List<ErrorMessage>> validationErrors = + ValidationManagerUtil.initValidationManager(fileContentMap).validate(); + response.getErrors().putAll(validationErrors); + + return OrchestrationUtil.createHeatTree(fileContentMap, validationErrors); + } + + private void printAuditForErrors(List<ErrorMessage> errorList, String vspId, String auditType) { + + errorList.forEach(errorMessage -> { + if (errorMessage.getLevel().equals(ErrorLevel.ERROR)) { + logger.audit(AuditMessages.AUDIT_MSG + String.format(auditType, errorMessage.getMessage(), + vspId)); + } + }); + } +} |