From 5700275b3e147e0053ae7872271a9f2fbfa13e06 Mon Sep 17 00:00:00 2001 From: Adam Wudzinski Date: Wed, 2 Dec 2020 18:07:22 +0100 Subject: [SDC] Validate PMDictionary content in Deployment artifacts tab Validate PMDictionary file content when adding or updating PMDictionary in Deployment artifacts tab. Fix dependencies conflict. Issue-ID: SDC-3390 Signed-off-by: Adam Wudzinski Change-Id: I6f6bb196ef061419a309a8ded5fdbe116982a037 --- .../be/components/impl/ArtifactsBusinessLogic.java | 19 +++++- .../components/impl/artifact/PayloadTypeEnum.java | 4 +- .../impl/validation/PMDictionaryValidator.java | 73 ++++++++++++++++++++++ 3 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidator.java (limited to 'catalog-be/src/main/java') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java index b6efd3ef0b..374e98e61f 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= - * Modifications copyright (c) 2019 Nokia + * Modifications copyright (c) 2020 Nokia * ================================================================================ */ @@ -69,6 +69,7 @@ import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentEx import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.components.impl.utils.ComponentUtils; +import org.openecomp.sdc.be.components.impl.validation.PMDictionaryValidator; import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum; @@ -2630,6 +2631,12 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { String artifactType = artifactInfo.getArtifactType(); String fileExtension = GeneralUtility.getFilenameExtension(artifactInfo.getArtifactName()); PayloadTypeEnum payloadType = ArtifactTypeToPayloadTypeSelector.getPayloadType(artifactType, fileExtension); + + final Optional pmDictionaryError = validateIfPmDictionary(artifactType, decodedPayload); + if (pmDictionaryError.isPresent()) { + return Either.right(pmDictionaryError.get()); + } + Either isPayloadValid = payloadType.isValid(decodedPayload); if (isPayloadValid.isRight()) { ResponseFormat responseFormat = componentsUtils.getResponseFormat(isPayloadValid.right().value(), artifactType); @@ -2659,6 +2666,16 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { return Either.left(decodedPayload); } + private Optional validateIfPmDictionary(String artifactType, byte[] decodedPayload) { + return new PMDictionaryValidator() + .validateIfPmDictionary(artifactType, decodedPayload) + .map(this::preparePmDictionaryResponse); + } + + private ResponseFormat preparePmDictionaryResponse(String errorMessage) { + return componentsUtils.getResponseFormat(ActionStatus.INVALID_PM_DICTIONARY_FILE, errorMessage); + } + public Either deleteArtifactByInterface( String resourceId, String userUserId, String artifactId, boolean inTransaction) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnum.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnum.java index df6a552917..4a71d4bc86 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnum.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnum.java @@ -17,7 +17,7 @@ * * * * SPDX-License-Identifier: Apache-2.0 * * ============LICENSE_END========================================================= - * + * * Modifications copyright (c) 2020 Nokia */ package org.openecomp.sdc.be.components.impl.artifact; @@ -144,9 +144,9 @@ public enum PayloadTypeEnum { private static Either isValidYaml(byte[] payload) { YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); if (yamlToObjectConverter.isValidYaml(payload)) { - log.debug("Invalid YAML format"); return Either.left(true); } + log.debug("Invalid YAML format"); return Either.right(ActionStatus.INVALID_YAML); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidator.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidator.java new file mode 100644 index 0000000000..84bebe3bf6 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidator.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.openecomp.sdc.be.components.impl.validation; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import org.onap.validation.yaml.YamlContentValidator; +import org.onap.validation.yaml.error.YamlDocumentValidationError; +import org.openecomp.sdc.common.api.ArtifactTypeEnum; + +public class PMDictionaryValidator { + + private final YamlContentValidator yamlContentValidator; + + public PMDictionaryValidator() { + this(new YamlContentValidator()); + } + + PMDictionaryValidator(YamlContentValidator yamlContentValidator) { + this.yamlContentValidator = yamlContentValidator; + } + + public Optional validateIfPmDictionary(String artifactType, byte[] decodedPayload) { + if (isPmDictionary(artifactType)) { + return validate(decodedPayload).stream() + .reduce((a, b) -> a + "; " + b); + } + return Optional.empty(); + } + + private boolean isPmDictionary(String artifactType) { + return ArtifactTypeEnum.PM_DICTIONARY.name().equals(artifactType); + } + + private List validate(byte[] fileContent) { + List errors = new ArrayList<>(); + try { + List validationErrors = yamlContentValidator.validate(fileContent); + validationErrors.stream() + .map(this::formatErrorMessage) + .forEach(errors::add); + } catch (Exception e) { + errors.add(e.getMessage()); + } + return errors; + } + + private String formatErrorMessage(YamlDocumentValidationError error) { + return String.format("Line number: %d, Path: %s, Message: %s", + error.getYamlDocumentNumber(), + error.getPath(), + error.getMessage()); + } + +} -- cgit 1.2.3-korg