[SDC] Validate PMDictionary content in Deployment artifacts tab
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / validation / PMDictionaryValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.components.impl.validation;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Optional;
25 import org.onap.validation.yaml.YamlContentValidator;
26 import org.onap.validation.yaml.error.YamlDocumentValidationError;
27 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
28
29 public class PMDictionaryValidator {
30
31     private final YamlContentValidator yamlContentValidator;
32
33     public PMDictionaryValidator() {
34         this(new YamlContentValidator());
35     }
36
37     PMDictionaryValidator(YamlContentValidator yamlContentValidator) {
38         this.yamlContentValidator = yamlContentValidator;
39     }
40
41     public Optional<String> validateIfPmDictionary(String artifactType, byte[] decodedPayload) {
42         if (isPmDictionary(artifactType)) {
43             return validate(decodedPayload).stream()
44                 .reduce((a, b) -> a + "; " + b);
45         }
46         return Optional.empty();
47     }
48
49     private boolean isPmDictionary(String artifactType) {
50         return ArtifactTypeEnum.PM_DICTIONARY.name().equals(artifactType);
51     }
52
53     private List<String> validate(byte[] fileContent) {
54         List<String> errors = new ArrayList<>();
55         try {
56             List<YamlDocumentValidationError> validationErrors = yamlContentValidator.validate(fileContent);
57             validationErrors.stream()
58                 .map(this::formatErrorMessage)
59                 .forEach(errors::add);
60         } catch (Exception e) {
61             errors.add(e.getMessage());
62         }
63         return errors;
64     }
65
66     private String formatErrorMessage(YamlDocumentValidationError error) {
67         return String.format("Line number: %d, Path: %s, Message: %s",
68             error.getYamlDocumentNumber(),
69             error.getPath(),
70             error.getMessage());
71     }
72
73 }