diff options
author | andre.schmid <andre.schmid@est.tech> | 2021-08-23 11:59:28 +0100 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2021-08-25 18:13:00 +0000 |
commit | 0499f0cd3522202708c926754e45a6d5e9c3c080 (patch) | |
tree | 422e32666bc382f96d0590ee5dab4b9ec89e8eba /openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main | |
parent | f7776389f137cb881033d77d89cc3f1eb4974077 (diff) |
Define a CSAR validator API for models
Change-Id: Ibdbedcc7cfe3660221f35667902f2e4eadc19725
Issue-ID: SDC-3682
Signed-off-by: andre.schmid <andre.schmid@est.tech>
Diffstat (limited to 'openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main')
6 files changed, 186 insertions, 50 deletions
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 f1cab482f2..3005a2df2c 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,7 +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.Map; import java.util.Optional; +import org.apache.commons.collections4.CollectionUtils; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; import org.openecomp.sdc.be.csar.storage.ArtifactInfo; @@ -34,6 +36,7 @@ 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; @@ -64,7 +67,10 @@ public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateH final FileContentHandler fileContentHandler = onboardPackage.getFileContentHandler(); try { final Validator validator = ValidatorFactory.getValidator(fileContentHandler); - uploadFileResponse.addStructureErrors(validator.validateContent(fileContentHandler)); + final ValidationResult validationResult = validator.validate(fileContentHandler); + if (CollectionUtils.isNotEmpty(validationResult.getErrors())) { + uploadFileResponse.addStructureErrors(Map.of(SdcCommon.UPLOAD_FILE, validationResult.getErrors())); + } } catch (IOException exception) { logger.error(exception.getMessage(), exception); uploadFileResponse diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/CsarValidationResult.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/CsarValidationResult.java new file mode 100644 index 0000000000..ddb42cf873 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/CsarValidationResult.java @@ -0,0 +1,52 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; + +public class CsarValidationResult implements ValidationResult { + + private final List<ErrorMessage> errorMessages = new ArrayList<>(); + + @Override + public boolean isValid() { + return errorMessages.stream().noneMatch(errorMessage -> errorMessage.getLevel().equals(ErrorLevel.ERROR)); + } + + @Override + public List<ErrorMessage> getErrors() { + return errorMessages; + } + + @Override + public List<ErrorMessage> getErrors(final ErrorLevel errorLevel) { + return errorMessages.stream().filter(errorMessage -> errorMessage.getLevel().equals(errorLevel)).collect(Collectors.toList()); + } + + public void addError(final ErrorMessage errorMessage) { + errorMessages.add(errorMessage); + } +}
\ No newline at end of file diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ONAPCsarValidator.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ONAPCsarValidator.java index 486970451f..12901abde5 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ONAPCsarValidator.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ONAPCsarValidator.java @@ -30,14 +30,11 @@ import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import org.openecomp.core.utilities.file.FileContentHandler; 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; @@ -49,22 +46,8 @@ import org.openecomp.sdc.tosca.csar.ToscaMetadata; class ONAPCsarValidator implements Validator { - private static Logger logger = LoggerFactory.getLogger(ONAPCsarValidator.class); - private List<ErrorMessage> uploadFileErrors = new ArrayList<>(); - - @Override - public Map<String, List<ErrorMessage>> validateContent(final FileContentHandler contentHandler) { - Map<String, List<ErrorMessage>> errors = new HashMap<>(); - validateManifest(contentHandler); - validateMetadata(contentHandler); - validateNoExtraFiles(contentHandler); - validateFolders(contentHandler.getFolderList()); - if (uploadFileErrors == null || uploadFileErrors.isEmpty()) { - return errors; - } - errors.put(SdcCommon.UPLOAD_FILE, uploadFileErrors); - return errors; - } + private static final Logger LOGGER = LoggerFactory.getLogger(ONAPCsarValidator.class); + private final List<ErrorMessage> uploadFileErrors = new ArrayList<>(); private void validateMetadata(FileContentHandler contentMap) { if (!validateTOSCAYamlFileInRootExist(contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME)) { @@ -77,7 +60,7 @@ class ONAPCsarValidator implements Validator { uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, Messages.METADATA_NO_ENTRY_DEFINITIONS.getErrorMessage())); } } catch (IOException exception) { - logger.error(exception.getMessage(), exception); + LOGGER.error(exception.getMessage(), exception); uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, Messages.FAILED_TO_VALIDATE_METADATA.getErrorMessage())); } } else { @@ -98,7 +81,7 @@ class ONAPCsarValidator implements Validator { } catch (final IOException ex) { final String errorMessage = Messages.MANIFEST_UNEXPECTED_ERROR.formatMessage(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, ex.getMessage()); uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, errorMessage)); - logger.error(errorMessage, ex); + LOGGER.error(errorMessage, ex); } } @@ -139,4 +122,25 @@ class ONAPCsarValidator implements Validator { } return containsFile; } + + @Override + public ValidationResult validate(final FileContentHandler csarContent) { + validateManifest(csarContent); + validateMetadata(csarContent); + validateNoExtraFiles(csarContent); + validateFolders(csarContent.getFolderList()); + final var csarValidationResult = new CsarValidationResult(); + uploadFileErrors.forEach(csarValidationResult::addError); + return csarValidationResult; + } + + @Override + public boolean appliesTo(String model) { + return model == null; + } + + @Override + public int getOrder() { + return 0; + } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidator.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidator.java index d99848ddb8..7416339b87 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidator.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidator.java @@ -111,19 +111,6 @@ class SOL004MetaDirectoryValidator implements Validator { this.securityManager = securityManager; } - @Override - public Map<String, List<ErrorMessage>> validateContent(final FileContentHandler fileContentHandler) { - this.contentHandler = (OnboardingPackageContentHandler) fileContentHandler; - this.folderList = contentHandler.getFolderList(); - parseToscaMetadata(); - verifyMetadataFile(); - if (packageHasCertificate()) { - verifySignedFiles(); - } - validatePmDictionaryContentsAgainstSchema(); - return Collections.unmodifiableMap(getAnyValidationErrors()); - } - private boolean packageHasCertificate() { final String certificatePath = getCertificatePath().orElse(null); return contentHandler.containsFile(certificatePath); @@ -499,15 +486,6 @@ class SOL004MetaDirectoryValidator implements Validator { LOGGER.warn(Messages.METADATA_UNSUPPORTED_ENTRY.getErrorMessage(), entry.getKey()); } - private Map<String, List<ErrorMessage>> getAnyValidationErrors() { - if (errorsByFile.isEmpty()) { - return Collections.emptyMap(); - } - final Map<String, List<ErrorMessage>> errors = new HashMap<>(); - errors.put(SdcCommon.UPLOAD_FILE, errorsByFile); - return errors; - } - private void validatePmDictionaryContentsAgainstSchema() { final Stream<byte[]> pmDictionaryFiles = new FileExtractor(getEtsiEntryManifestPath(), contentHandler).findFiles(ONAP_PM_DICTIONARY); new PMDictionaryValidator().validate(pmDictionaryFiles, (String message) -> reportError(ErrorLevel.ERROR, message)); @@ -532,4 +510,29 @@ class SOL004MetaDirectoryValidator implements Validator { reportError(ErrorLevel.ERROR, Messages.UNIQUE_ONAP_CNF_HELM_NON_MANO_ERROR.formatMessage(formattedFileList)); } } + + @Override + public ValidationResult validate(final FileContentHandler csarContent) { + this.contentHandler = (OnboardingPackageContentHandler) csarContent; + this.folderList = contentHandler.getFolderList(); + parseToscaMetadata(); + verifyMetadataFile(); + if (packageHasCertificate()) { + verifySignedFiles(); + } + validatePmDictionaryContentsAgainstSchema(); + final var csarValidationResult = new CsarValidationResult(); + errorsByFile.forEach(csarValidationResult::addError); + return csarValidationResult; + } + + @Override + public boolean appliesTo(final String model) { + return model == null; + } + + @Override + public int getOrder() { + return 0; + } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidationResult.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidationResult.java new file mode 100644 index 0000000000..df4a91c85e --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidationResult.java @@ -0,0 +1,55 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; + +import java.util.List; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; + +/** + * Holds the result of a CSAR validation. + */ +public interface ValidationResult { + + /** + * Check if the CSAR is valid. + * + * @return {@code true} if there is no messages with error level, {@code false} otherwise. + */ + boolean isValid(); + + /** + * Gets all the error messages. + * + * @return the error messages. + */ + List<ErrorMessage> getErrors(); + + /** + * Gets the error messages based on the given error level. + * + * @param errorLevel the error level + * @return the existing messages for the given error level, or an empty List if no messages are found. + */ + List<ErrorMessage> getErrors(final ErrorLevel errorLevel); + +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/Validator.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/Validator.java index e8f88c7eba..bd099779f6 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/Validator.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/Validator.java @@ -19,10 +19,7 @@ */ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; -import java.util.List; -import java.util.Map; import org.openecomp.core.utilities.file.FileContentHandler; -import org.openecomp.sdc.datatypes.error.ErrorMessage; /** * Validates the contents of the CSAR package uploaded in SDC. @@ -30,8 +27,27 @@ import org.openecomp.sdc.datatypes.error.ErrorMessage; public interface Validator { /** - * @param contentHandler contains file and its data - * @return errors Map of errors that occur + * Validates the structure and content of a CSAR. + * + * @param csarContent the CSAR content + * @return the result of the validation */ - Map<String, List<ErrorMessage>> validateContent(final FileContentHandler contentHandler); + ValidationResult validate(final FileContentHandler csarContent); + + /** + * Checks if the validator applies to the given model. + * + * @param model the model to check + * @return {@code true} if the validator applies to the given model, {@code false} otherwise + */ + boolean appliesTo(final String model); + + /** + * Should return the execution order that the validator is intended to run in relation to other validators that applies to the same model ({@link + * #appliesTo(String)}). The lower the value, the higher the priority. If a validator happens to have the same order of others, the system will + * randomly decides the execution order. + * + * @return the execution order of the validator. + */ + int getOrder(); } |