diff options
author | andre.schmid <andre.schmid@est.tech> | 2021-08-23 19:41:50 +0100 |
---|---|---|
committer | andre.schmid <andre.schmid@est.tech> | 2021-08-26 09:55:34 +0100 |
commit | 5a0703ffad1492ec6b6c78143f63dca83ee030d2 (patch) | |
tree | 202acfe75f4975051bd0b18d55b962ecd148fe7c /openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main | |
parent | 0c2661a7dfd1de644c7a3f9f42fc1883d3ceff78 (diff) |
Dynamically load CSAR validators for models
Change-Id: I88ece0936e8a2814ef13dfa23eecda56de3dc6fe
Issue-ID: SDC-3683
Signed-off-by: andre.schmid <andre.schmid@est.tech>
Diffstat (limited to 'openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main')
4 files changed, 78 insertions, 13 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 index 6e810eefbd..ea36e5389f 100644 --- 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 @@ -50,7 +50,7 @@ public abstract class BaseOrchestrationTemplateHandler implements OrchestrationT if (isInvalidRawZipData(onboardPackage.getFileExtension(), uploadFileResponse, fileContentByteArray, candidateService)) { return uploadFileResponse; } - final UploadFileResponse validateResponse = validate(onboardPackageInfo); + final UploadFileResponse validateResponse = validate(vspDetails, onboardPackageInfo); if (!MapUtils.isEmpty(validateResponse.getErrors())) { uploadFileResponse.addStructureErrors(validateResponse.getErrors()); return uploadFileResponse; @@ -87,7 +87,7 @@ public abstract class BaseOrchestrationTemplateHandler implements OrchestrationT return false; } - public abstract UploadFileResponse validate(final OnboardPackageInfo onboardPackageInfo); + public abstract UploadFileResponse validate(final VspDetails vspDetails, final OnboardPackageInfo onboardPackageInfo); 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 index 3005a2df2c..5a3518135a 100644 --- 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 @@ -22,6 +22,9 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration; import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters; import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import org.apache.commons.collections4.CollectionUtils; @@ -36,7 +39,6 @@ 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.validation.CsarSecurityValidator; -import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidationResult; import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.Validator; import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidatorFactory; import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException; @@ -48,8 +50,14 @@ import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler { + private final ValidatorFactory validatorFactory; + + public OrchestrationTemplateCSARHandler() { + this.validatorFactory = new ValidatorFactory(); + } + @Override - public UploadFileResponse validate(final OnboardPackageInfo onboardPackageInfo) { + public UploadFileResponse validate(final VspDetails vspDetails, final OnboardPackageInfo onboardPackageInfo) { final UploadFileResponse uploadFileResponse = new UploadFileResponse(); if (onboardPackageInfo.getPackageType() == OnboardingTypesEnum.SIGNED_CSAR) { final OnboardSignedPackage originalOnboardPackage = (OnboardSignedPackage) onboardPackageInfo.getOriginalOnboardPackage(); @@ -66,22 +74,57 @@ public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateH final OnboardPackage onboardPackage = onboardPackageInfo.getOnboardPackage(); final FileContentHandler fileContentHandler = onboardPackage.getFileContentHandler(); try { - final Validator validator = ValidatorFactory.getValidator(fileContentHandler); - final ValidationResult validationResult = validator.validate(fileContentHandler); - if (CollectionUtils.isNotEmpty(validationResult.getErrors())) { - uploadFileResponse.addStructureErrors(Map.of(SdcCommon.UPLOAD_FILE, validationResult.getErrors())); + final List<String> modelIdList = vspDetails.getModelIdList(); + if (CollectionUtils.isEmpty(modelIdList)) { + final Map<String, List<ErrorMessage>> errorResponseMap = validateSdcModel(fileContentHandler); + if (!errorResponseMap.isEmpty()) { + uploadFileResponse.addStructureErrors(errorResponseMap); + } + } else { + final Map<String, List<ErrorMessage>> errorResponseMap = validateModels(modelIdList, fileContentHandler); + if (!errorResponseMap.isEmpty()) { + uploadFileResponse.addStructureErrors(errorResponseMap); + } } - } catch (IOException exception) { + } catch (final IOException exception) { logger.error(exception.getMessage(), exception); uploadFileResponse .addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage())); - } catch (CoreException coreException) { + } catch (final CoreException coreException) { logger.error(coreException.getMessage(), coreException); uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage())); } return uploadFileResponse; } + private Map<String, List<ErrorMessage>> validateModels(final List<String> modelIdList, final FileContentHandler fileContentHandler) { + final Map<String, List<ErrorMessage>> errorResponseMap = new HashMap<>(); + modelIdList.forEach(model -> { + final List<Validator> validators = validatorFactory.getValidators(model); + validators.forEach(validator -> { + final var validationResult = validator.validate(fileContentHandler); + if (CollectionUtils.isNotEmpty(validationResult.getErrors())) { + if (errorResponseMap.containsKey(SdcCommon.UPLOAD_FILE)) { + errorResponseMap.get(SdcCommon.UPLOAD_FILE).addAll(validationResult.getErrors()); + } else { + errorResponseMap.put(SdcCommon.UPLOAD_FILE, validationResult.getErrors()); + } + } + }); + }); + return errorResponseMap; + } + + private Map<String, List<ErrorMessage>> validateSdcModel(final FileContentHandler fileContentHandler) throws IOException { + final var validator = validatorFactory.getValidator(fileContentHandler); + final var validationResult = validator.validate(fileContentHandler); + if (CollectionUtils.isNotEmpty(validationResult.getErrors())) { + return Map.of(SdcCommon.UPLOAD_FILE, validationResult.getErrors()); + } + + return Collections.emptyMap(); + } + private Optional<UploadFileResponse> validatePackageSecurity(final OnboardSignedPackage signedPackage, final ArtifactInfo artifactInfo) { final UploadFileResponse uploadFileResponseDto = new UploadFileResponse(); try { 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 index e534eded02..c4d7053870 100644 --- 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 @@ -37,7 +37,7 @@ import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; public class OrchestrationTemplateZipHandler extends BaseOrchestrationTemplateHandler { @Override - public UploadFileResponse validate(final OnboardPackageInfo onboardPackageInfo) { + public UploadFileResponse validate(final VspDetails vspDetails, final OnboardPackageInfo onboardPackageInfo) { final UploadFileResponse uploadFileResponse = new UploadFileResponse(); final OnboardPackage onboardPackage = onboardPackageInfo.getOnboardPackage(); OrchestrationUtil.getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse, onboardPackage.getFileContent().array()); diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactory.java index 27a1852644..91d2705693 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactory.java @@ -22,13 +22,21 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validati import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_7_1; import java.io.IOException; +import java.util.Comparator; +import java.util.List; +import java.util.ServiceLoader; +import java.util.ServiceLoader.Provider; +import java.util.stream.Collectors; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIService; import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIServiceImpl; public class ValidatorFactory { - private ValidatorFactory() { + private final ServiceLoader<Validator> validatorLoader; + + public ValidatorFactory() { + this.validatorLoader = ServiceLoader.load(Validator.class); } /** @@ -38,7 +46,7 @@ public class ValidatorFactory { * @return Validator based on the contents of the csar package provided * @throws IOException when metafile is invalid */ - public static Validator getValidator(final FileContentHandler fileContentHandler) throws IOException { + public Validator getValidator(final FileContentHandler fileContentHandler) throws IOException { final ETSIService etsiService = new ETSIServiceImpl(null); if (!etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)) { return new ONAPCsarValidator(); @@ -51,4 +59,18 @@ public class ValidatorFactory { } return new SOL004MetaDirectoryValidator(); } + + /** + * Get validators based on the given model. + * + * @param model the model + * @return a list containing all validators for the given model, empty otherwise. + */ + public List<Validator> getValidators(final String model) { + return validatorLoader.stream() + .map(Provider::get) + .filter(validator -> validator.appliesTo(model)) + .sorted(Comparator.comparingInt(Validator::getOrder)) + .collect(Collectors.toList()); + } } |