diff options
Diffstat (limited to 'openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessZipHandler.java')
1 files changed, 119 insertions, 144 deletions
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 index 2d18a62122..5f18cb2faa 100644 --- 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 @@ -14,9 +14,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +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 org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.openecomp.core.translator.datatypes.TranslatorOutput; @@ -37,7 +46,11 @@ import org.openecomp.sdc.logging.api.LoggerFactory; 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.*; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity; +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; @@ -47,157 +60,119 @@ import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStru import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils; import org.openecomp.sdc.versioning.dao.types.Version; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.util.*; - public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemplateProcessHandler { - private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessZipHandler.class); - private final CandidateService candidateService = - CandidateServiceFactory.getInstance().createInterface(); - - @Override - public OrchestrationTemplateActionResponse process(VspDetails vspDetails, - OrchestrationTemplateCandidateData candidateData) { - String vspId = vspDetails.getId(); - Version version = vspDetails.getVersion(); - 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()); - return response; - } - - Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors(); - FileContentHandler fileContentMap = fileContent.get(); - try (InputStream zipFileManifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) { - addDummyHeatBase(zipFileManifest ,fileContentMap); - } catch (Exception e) { - LOGGER.error("Invalid package content", e); - } - 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); - return response; - } - - - ManifestContent zipManifestFile = readManifestFromZip(fileContentMap); - String manifest = null; - if (zipManifestFile == null) { - manifest = candidateService.createManifest(vspDetails, structure); - } else { - manifest = candidateService.createManifestFromExisting(vspDetails, structure, zipManifestFile); + private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessZipHandler.class); + private final CandidateService candidateService = CandidateServiceFactory.getInstance().createInterface(); + + @Override + public OrchestrationTemplateActionResponse process(VspDetails vspDetails, OrchestrationTemplateCandidateData candidateData) { + String vspId = vspDetails.getId(); + Version version = vspDetails.getVersion(); + 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()); + return response; + } + Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors(); + FileContentHandler fileContentMap = fileContent.get(); + try (InputStream zipFileManifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) { + addDummyHeatBase(zipFileManifest, fileContentMap); + } catch (Exception e) { + LOGGER.error("Invalid package content", e); + } + 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); + return response; + } + ManifestContent zipManifestFile = readManifestFromZip(fileContentMap); + String manifest = null; + if (zipManifestFile == null) { + manifest = candidateService.createManifest(vspDetails, structure); + } else { + manifest = candidateService.createManifestFromExisting(vspDetails, structure, zipManifestFile); + } + fileContentMap.addFile(SdcCommon.MANIFEST_NAME, manifest.getBytes()); + Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService + .fetchZipFileByteArrayInputStream(vspId, candidateData, manifest, OnboardingTypesEnum.ZIP, uploadErrors); + if (!zipByteArrayInputStream.isPresent()) { + return response; + } + HeatStructureTree tree = createAndValidateHeatTree(response, fileContentMap); + Map<String, List<ErrorMessage>> errors = getErrors(response); + if (MapUtils.isNotEmpty(errors)) { + response.addStructureErrors(errors); + candidateService.updateValidationData(vspId, version, new ValidationStructureList(tree)); + return response; + } + 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 orchestrationUtil = new OrchestrationUtil(); + Map<String, String> vspComponentIdNameInfoBeforeProcess = orchestrationUtil.getVspComponentIdNameInfo(vspId, version); + Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete = orchestrationUtil + .getComponentDependenciesBeforeDelete(vspId, version); + orchestrationUtil + .backupComponentsQuestionnaireBeforeDelete(vspId, version, componentsQuestionnaire, componentNicsQuestionnaire, componentMibList, + processes, processArtifact); + orchestrationUtil.deleteUploadDataAndContent(vspId, version); + orchestrationUtil.saveUploadData(vspDetails, candidateData, zipByteArrayInputStream.get(), fileContentMap, tree); + 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); + orchestrationUtil.updateVspComponentDependencies(vspId, version, vspComponentIdNameInfoBeforeProcess, componentDependenciesBeforeDelete); + uploadFileResponse.addStructureErrors(uploadErrors); + candidateService.deleteOrchestrationTemplateCandidate(vspId, version); + return response; } - fileContentMap.addFile(SdcCommon.MANIFEST_NAME, manifest.getBytes()); - Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService - .fetchZipFileByteArrayInputStream( - vspId, candidateData, manifest, OnboardingTypesEnum.ZIP, uploadErrors); - if (!zipByteArrayInputStream.isPresent()) { - return response; + private ManifestContent readManifestFromZip(FileContentHandler fileContentMap) { + ManifestContent zipManifestFile = null; + try (InputStream zipFileManifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) { + zipManifestFile = JsonUtil.json2Object(zipFileManifest, ManifestContent.class); + } catch (Exception e) { + LOGGER.error("Invalid package content", e); + } + return zipManifestFile; } - HeatStructureTree tree = createAndValidateHeatTree(response, fileContentMap); - Map<String, List<ErrorMessage>> errors = getErrors(response); - if (MapUtils.isNotEmpty(errors)) { - response.addStructureErrors(errors); - candidateService.updateValidationData(vspId, version, new ValidationStructureList(tree)); - return response; + private FileContentHandler addDummyHeatBase(InputStream zipFileManifest, FileContentHandler fileContentMap) { + ManifestContent manifestContent = JsonUtil.json2Object(zipFileManifest, ManifestContent.class); + for (FileData fileData : manifestContent.getData()) { + if (Objects.nonNull(fileData.getType()) && fileData.getType().equals(FileData.Type.HELM) && fileData.getBase()) { + String filePath = new File("").getAbsolutePath() + "/resources"; + File envFilePath = new File(filePath + "/base_template.env"); + File baseFilePath = new File(filePath + "/base_template.yaml"); + try (InputStream envStream = new FileInputStream(envFilePath); InputStream baseStream = new FileInputStream(baseFilePath);) { + fileContentMap.addFile("base_template_dummy_ignore.env", envStream); + fileContentMap.addFile("base_template_dummy_ignore.yaml", baseStream); + } catch (Exception e) { + LOGGER.error("File not found error {}", e); + } + } + } + return 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 orchestrationUtil = new OrchestrationUtil(); - Map<String, String> vspComponentIdNameInfoBeforeProcess = - orchestrationUtil.getVspComponentIdNameInfo(vspId, version); - Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete = - orchestrationUtil.getComponentDependenciesBeforeDelete(vspId, version); - orchestrationUtil - .backupComponentsQuestionnaireBeforeDelete(vspId, version, componentsQuestionnaire, - componentNicsQuestionnaire, componentMibList, processes, processArtifact); - orchestrationUtil.deleteUploadDataAndContent(vspId, version); - orchestrationUtil - .saveUploadData(vspDetails, candidateData, zipByteArrayInputStream.get(), fileContentMap, - tree); - - 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); - orchestrationUtil.updateVspComponentDependencies(vspId, version, - vspComponentIdNameInfoBeforeProcess, componentDependenciesBeforeDelete); - - uploadFileResponse.addStructureErrors(uploadErrors); - candidateService.deleteOrchestrationTemplateCandidate(vspId, version); - return response; - } - - private ManifestContent readManifestFromZip(FileContentHandler fileContentMap) { - ManifestContent zipManifestFile = null; - try (InputStream zipFileManifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) { - zipManifestFile = JsonUtil.json2Object(zipFileManifest, ManifestContent.class); - } catch (Exception e) { - LOGGER.error("Invalid package content", e); + private Map<String, List<ErrorMessage>> getErrors(OrchestrationTemplateActionResponse orchestrationTemplateActionResponse) { + Map<String, List<ErrorMessage>> errors = MessageContainerUtil + .getMessageByLevel(ErrorLevel.ERROR, orchestrationTemplateActionResponse.getErrors()); + return MapUtils.isEmpty(errors) ? null : orchestrationTemplateActionResponse.getErrors(); } - return zipManifestFile; - } - private FileContentHandler addDummyHeatBase(InputStream zipFileManifest, FileContentHandler fileContentMap) { - ManifestContent manifestContent = - JsonUtil.json2Object(zipFileManifest, ManifestContent.class); - for (FileData fileData : manifestContent.getData()) { - if (Objects.nonNull(fileData.getType()) && - fileData.getType().equals(FileData.Type.HELM) && fileData.getBase()) { - String filePath = new File("").getAbsolutePath() + "/resources"; - File envFilePath = new File(filePath + "/base_template.env"); - File baseFilePath = new File(filePath + "/base_template.yaml"); - try ( - InputStream envStream = new FileInputStream(envFilePath); - InputStream baseStream = new FileInputStream(baseFilePath);) { - fileContentMap.addFile("base_template_dummy_ignore.env", envStream); - fileContentMap.addFile("base_template_dummy_ignore.yaml", baseStream); - } catch (Exception e) { - LOGGER.error("File not found error {}", e); - } - } + 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); } - return fileContentMap; - } - - private Map<String, List<ErrorMessage>> getErrors(OrchestrationTemplateActionResponse - orchestrationTemplateActionResponse) { - Map<String, List<ErrorMessage>> errors = - MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, - orchestrationTemplateActionResponse.getErrors()); - return MapUtils.isEmpty(errors) ? null : orchestrationTemplateActionResponse.getErrors(); - } - - 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); - } } |