[SDC] Validate PMDictionary content in Deployment artifacts tab 39/116039/5
authorAdam Wudzinski <adam.wudzinski@nokia.com>
Wed, 2 Dec 2020 17:07:22 +0000 (18:07 +0100)
committerMaciej Malewski <maciej.malewski@nokia.com>
Thu, 3 Dec 2020 15:23:32 +0000 (16:23 +0100)
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 <adam.wudzinski@nokia.com>
Change-Id: I6f6bb196ef061419a309a8ded5fdbe116982a037

catalog-be/pom.xml
catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml
catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java
catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnum.java
catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidator.java [new file with mode: 0644]
catalog-be/src/main/resources/config/error-configuration.yaml
catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidatorTest.java [new file with mode: 0644]
catalog-dao/src/main/java/org/openecomp/sdc/be/dao/api/ActionStatus.java
integration-tests/src/test/resources/Files/PNFs/pmDictionary.yml
openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/pom.xml
openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/pom.xml

index 399e45c..fd25bc2 100644 (file)
                 </exclusion>
             </exclusions>
         </dependency>
+        <dependency>
+            <groupId>org.onap.vnfsdk.validation</groupId>
+            <artifactId>validation-pmdictionary</artifactId>
+            <version>${onap.vnfsdk.validation.pmdictionary.version}</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.apache.logging.log4j</groupId>
+                    <artifactId>log4j-slf4j-impl</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
     </dependencies>
 
     <build>
index a4491de..d2a7990 100644 (file)
@@ -2398,3 +2398,10 @@ errors:
       message: 'Error: Invalid Content. %1 has invalid format.',
       messageId: "SVC4723"
     }
+#---------SVC4732------------------------------
+    # %1 - list of validation errors
+    INVALID_PM_DICTIONARY_FILE: {
+        code: 400,
+        message: 'Error: Invalid PM Dictionary File. %1',
+        messageId: "SVC4732"
+    }
index b6efd3e..374e98e 100644 (file)
@@ -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<ResponseFormat> pmDictionaryError = validateIfPmDictionary(artifactType, decodedPayload);
+                if (pmDictionaryError.isPresent()) {
+                    return Either.right(pmDictionaryError.get());
+                }
+
                 Either<Boolean, ActionStatus> 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<ResponseFormat> 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<ArtifactDefinition, ResponseFormat> deleteArtifactByInterface(
         String resourceId, String userUserId, String artifactId, boolean inTransaction) {
 
index df6a552..4a71d4b 100644 (file)
@@ -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<Boolean, ActionStatus> 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 (file)
index 0000000..84bebe3
--- /dev/null
@@ -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<String> 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<String> validate(byte[] fileContent) {
+        List<String> errors = new ArrayList<>();
+        try {
+            List<YamlDocumentValidationError> 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());
+    }
+
+}
index a4491de..14b4ece 100644 (file)
@@ -2398,3 +2398,10 @@ errors:
       message: 'Error: Invalid Content. %1 has invalid format.',
       messageId: "SVC4723"
     }
+#---------SVC4732------------------------------
+    # %1 - list of validation errors
+    INVALID_PM_DICTIONARY_FILE: {
+        code: 400,
+        message: 'Error: Invalid PM Dictionary File. %1',
+        messageId: "SVC4732"
+    }
\ No newline at end of file
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidatorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidatorTest.java
new file mode 100644 (file)
index 0000000..7e62e6d
--- /dev/null
@@ -0,0 +1,87 @@
+/*-
+ * ============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 static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+import java.util.Optional;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.onap.validation.yaml.YamlContentValidator;
+import org.onap.validation.yaml.error.YamlDocumentValidationError;
+import org.onap.validation.yaml.exception.YamlProcessingException;
+import org.openecomp.sdc.common.api.ArtifactTypeEnum;
+
+class PMDictionaryValidatorTest {
+
+    private YamlContentValidator yamlContentValidator;
+
+    @BeforeEach
+    void setUp() {
+        yamlContentValidator = mock(YamlContentValidator.class);
+    }
+
+    @Test
+    void shouldNotReturnErrors_whenArtifactTypeDoNotMatch() {
+        // when
+        Optional<String> errors = new PMDictionaryValidator(yamlContentValidator)
+            .validateIfPmDictionary(ArtifactTypeEnum.DCAE_INVENTORY_BLUEPRINT.name(), "".getBytes());
+
+        // then
+        assertTrue(errors.isEmpty());
+        verifyNoInteractions(yamlContentValidator);
+    }
+
+    @Test
+    void shouldReturnErrors_whenArtifactTypeIsPmDictionaryAndFileIsInvalid() throws YamlProcessingException {
+        // given
+        byte[] fileContent = "".getBytes();
+        YamlDocumentValidationError validationError = new YamlDocumentValidationError(1, "/", "error");
+        when(yamlContentValidator.validate(fileContent)).thenReturn(List.of(validationError));
+
+        // when
+        Optional<String> errors = new PMDictionaryValidator(yamlContentValidator)
+            .validateIfPmDictionary(ArtifactTypeEnum.PM_DICTIONARY.name(), fileContent);
+
+        // then
+        assertTrue(errors.isPresent());
+        assertThat(errors.get(), is("Line number: 1, Path: /, Message: error"));
+    }
+
+    @Test
+    void shouldNotReturnErrors_whenArtifactTypeIsPmDictionaryAndFileIsValid() throws YamlProcessingException {
+        // given
+        byte[] fileContent = "".getBytes();
+        when(yamlContentValidator.validate(fileContent)).thenReturn(List.of());
+
+        // when
+        Optional<String> errors = new PMDictionaryValidator(yamlContentValidator)
+            .validateIfPmDictionary(ArtifactTypeEnum.PM_DICTIONARY.name(), fileContent);
+
+        // then
+        assertTrue(errors.isEmpty());
+    }
+}
index cdb10a7..78d6c63 100644 (file)
@@ -16,6 +16,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END=========================================================
+ * Modifications copyright (c) 2020 Nokia
  */
 
 package org.openecomp.sdc.be.dao.api;
@@ -29,7 +30,7 @@ public enum ActionStatus {
     CAPABILITY_TYPE_ALREADY_EXIST, MISSING_CAPABILITY_TYPE, MISSING_CAPABILITIES, REQ_CAP_NOT_SATISFIED_BEFORE_CERTIFICATION, IMPORT_DUPLICATE_REQ_CAP_NAME, IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED,
     CAPABILITY_OF_INSTANCE_NOT_FOUND_ON_CONTAINER, REQUIREMENT_OF_INSTANCE_NOT_FOUND_ON_CONTAINER, RELATION_NOT_FOUND,
     // Resource related
-    RESOURCE_NOT_FOUND, MISSING_DERIVED_FROM_TEMPLATE, PARENT_RESOURCE_NOT_FOUND, PARENT_RESOURCE_DOES_NOT_EXTEND, INVALID_DEFAULT_VALUE, INVALID_COMPLEX_DEFAULT_VALUE, MULTIPLE_PARENT_RESOURCE_FOUND, INVALID_RESOURCE_PAYLOAD, INVALID_TOSCA_FILE_EXTENSION, INVALID_YAML_FILE, INVALID_TOSCA_TEMPLATE, NOT_RESOURCE_TOSCA_TEMPLATE, NOT_SINGLE_RESOURCE, INVALID_RESOURCE_NAMESPACE, RESOURCE_ALREADY_EXISTS, INVALID_RESOURCE_CHECKSUM, RESOURCE_CANNOT_CONTAIN_RESOURCE_INSTANCES, NO_ASSETS_FOUND, GENERIC_TYPE_NOT_FOUND, INVALID_RESOURCE_TYPE, TOSCA_PARSE_ERROR,
+    RESOURCE_NOT_FOUND, MISSING_DERIVED_FROM_TEMPLATE, PARENT_RESOURCE_NOT_FOUND, PARENT_RESOURCE_DOES_NOT_EXTEND, INVALID_DEFAULT_VALUE, INVALID_COMPLEX_DEFAULT_VALUE, MULTIPLE_PARENT_RESOURCE_FOUND, INVALID_RESOURCE_PAYLOAD, INVALID_TOSCA_FILE_EXTENSION, INVALID_YAML_FILE, INVALID_TOSCA_TEMPLATE, NOT_RESOURCE_TOSCA_TEMPLATE, NOT_SINGLE_RESOURCE, INVALID_RESOURCE_NAMESPACE, RESOURCE_ALREADY_EXISTS, INVALID_RESOURCE_CHECKSUM, RESOURCE_CANNOT_CONTAIN_RESOURCE_INSTANCES, NO_ASSETS_FOUND, GENERIC_TYPE_NOT_FOUND, INVALID_RESOURCE_TYPE, TOSCA_PARSE_ERROR, INVALID_PM_DICTIONARY_FILE,
     // Service related
     SERVICE_TYPE_EXCEEDS_LIMIT, INVALID_SERVICE_TYPE, SERVICE_ROLE_EXCEEDS_LIMIT, INVALID_SERVICE_ROLE, INVALID_INSTANTIATION_TYPE, NOT_SERVICE_TOSCA_TEMPLATE,NOT_SINGLE_SERVICE,INVALID_SERVICE_CHECKSUM,INVALID_SERVICE_PAYLOAD,INVALID_SERVICE_NAMESPACE,SERVICE_ALREADY_EXISTS,
     UNSUPPORTED_DISTRIBUTION_STATUS, INVALID_NAMING_POLICY, INVALID_ENVIRONMENT_CONTEXT, NAMING_POLICY_EXCEEDS_LIMIT, MISSING_ECOMP_GENERATED_NAMING, PROPERTY_EXCEEDS_LIMIT, INVALID_PROPERY,
@@ -180,8 +181,5 @@ public enum ActionStatus {
 
     //Abstract template related
     ABSTRACT, NORMAL
-
        ;
-
-
     }
index fa3ed53..3e3b3d1 100644 (file)
@@ -50,6 +50,8 @@ pmMetaData:
     measAdditionalFields:
       vendorField1: X
       vendorField2: B
+      measAggregationLevels: NRCellCU
+    measChangeType: added
 ---
 pmMetaData:
   pmHeader:
@@ -72,6 +74,8 @@ pmMetaData:
     measType: VS.NINFC.IntraFrPscelChFailTdcExp
     measAdditionalFields:
       vendorField1: Y
+      measAggregationLevels: NRCellCU
+    measChangeType: added
 ---
 pmMetaData:
   pmHeader:
@@ -95,4 +99,6 @@ pmMetaData:
     measAdditionalFields:
       vendorField1: Z
       vendorField2: A
+      measAggregationLevels: NRCellCU
+    measChangeType: added
 ...
index 98092c7..e185f78 100644 (file)
       <groupId>org.onap.vnfsdk.validation</groupId>
       <artifactId>validation-pmdictionary</artifactId>
       <version>${onap.vnfsdk.validation.pmdictionary.version}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>org.apache.logging.log4j</groupId>
+          <artifactId>log4j-slf4j-impl</artifactId>
+        </exclusion>
+      </exclusions>
     </dependency>
 
   </dependencies>
index 2119ee8..c8f0622 100644 (file)
       <groupId>org.onap.vnfsdk.validation</groupId>
       <artifactId>validation-pmdictionary</artifactId>
       <version>${onap.vnfsdk.validation.pmdictionary.version}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>org.apache.logging.log4j</groupId>
+          <artifactId>log4j-slf4j-impl</artifactId>
+        </exclusion>
+      </exclusions>
     </dependency>
     <dependency>
       <groupId>io.vavr</groupId>