2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.be.components.impl.validation;
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;
29 public class PMDictionaryValidator {
31 private final YamlContentValidator yamlContentValidator;
33 public PMDictionaryValidator() {
34 this(new YamlContentValidator());
37 PMDictionaryValidator(YamlContentValidator yamlContentValidator) {
38 this.yamlContentValidator = yamlContentValidator;
41 public Optional<String> validateIfPmDictionary(String artifactType, byte[] decodedPayload) {
42 if (isPmDictionary(artifactType)) {
43 return validate(decodedPayload).stream()
44 .reduce((a, b) -> a + "; " + b);
46 return Optional.empty();
49 private boolean isPmDictionary(String artifactType) {
50 return ArtifactTypeEnum.PM_DICTIONARY.name().equals(artifactType);
53 private List<String> validate(byte[] fileContent) {
54 List<String> errors = new ArrayList<>();
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());
66 private String formatErrorMessage(YamlDocumentValidationError error) {
67 return String.format("Line number: %d, Path: %s, Message: %s",
68 error.getYamlDocumentNumber(),