2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 Nokia Intellectual Property. 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=========================================================
21 package org.openecomp.sdc.validation.impl.validators;
23 import io.vavr.control.Option;
24 import io.vavr.control.Try;
25 import org.onap.validation.yaml.YamlContentValidator;
26 import org.onap.validation.yaml.error.YamlDocumentValidationError;
27 import org.openecomp.core.validation.ErrorMessageCode;
28 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
29 import org.openecomp.core.validation.types.GlobalValidationContext;
30 import org.openecomp.sdc.datatypes.error.ErrorLevel;
31 import org.openecomp.sdc.validation.Validator;
33 import java.io.InputStream;
34 import java.util.List;
37 public class PmDictionaryValidator implements Validator {
39 private static final ErrorMessageCode PM_DICT_ERROR_CODE = new ErrorMessageCode("PM_DICT");
42 public void validate(GlobalValidationContext globalContext) {
43 Set<String> pmDictionaryFiles = GlobalContextUtil.findPmDictionaryFiles(globalContext);
44 validatePmDictionaryFiles(globalContext, pmDictionaryFiles);
48 private void validatePmDictionaryFiles(GlobalValidationContext globalContext, Set<String> pmDictionaryFiles) {
49 pmDictionaryFiles.stream()
50 .map(fileName -> new ValidationHelper(globalContext, fileName))
51 .forEach(ValidationHelper::validate);
54 private static class ValidationHelper {
56 private final GlobalValidationContext globalContext;
57 private final String fileName;
59 private ValidationHelper(GlobalValidationContext globalContext, String fileName) {
60 this.globalContext = globalContext;
61 this.fileName = fileName;
64 public void validate() {
65 Option.ofOptional(globalContext.getFileContent(fileName))
66 .peek(this::validateFileContent)
67 .onEmpty(() -> addErrorToContext(formatMessage("File is empty")));
70 private void validateFileContent(InputStream inputStream) {
71 Try.of(inputStream::readAllBytes)
72 .mapTry(fileContent -> new YamlContentValidator().validate(fileContent))
73 .onSuccess(this::reportValidationErrorsIfPresent)
74 .onFailure(e -> addErrorToContext(formatMessage(e.getMessage())));
77 private void reportValidationErrorsIfPresent(List<YamlDocumentValidationError> validationErrors) {
78 validationErrors.stream()
79 .map(this::prepareValidationMessage)
80 .forEach(this::addErrorToContext);
83 private String prepareValidationMessage(YamlDocumentValidationError error) {
84 final String errorMessage = String.format("Document Number: %s, Path: %s, Problem: %s",
85 error.getYamlDocumentNumber(),
89 return formatMessage(errorMessage);
92 private String formatMessage(String message) {
93 return ErrorMessagesFormatBuilder
94 .getErrorWithParameters(PM_DICT_ERROR_CODE, message);
97 private void addErrorToContext(String message) {
98 globalContext.addMessage(fileName, ErrorLevel.ERROR, message);