2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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 java.io.InputStream;
26 import java.util.List;
27 import org.onap.validation.yaml.YamlContentValidator;
28 import org.onap.validation.yaml.error.YamlDocumentValidationError;
29 import org.openecomp.core.validation.ErrorMessageCode;
30 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
31 import org.openecomp.core.validation.types.GlobalValidationContext;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.validation.Validator;
35 public class PmDictionaryValidator implements Validator {
37 private static final ErrorMessageCode PM_DICT_ERROR_CODE = new ErrorMessageCode("PM_DICT");
40 public void validate(GlobalValidationContext globalContext) {
41 globalContext.getFiles().stream()
42 .filter(FileExtensionUtils::isPmDictionary)
43 .map(fileName -> new ValidationHelper(globalContext, fileName))
44 .forEach(ValidationHelper::validate);
47 private static class ValidationHelper {
49 private final GlobalValidationContext globalContext;
50 private final String fileName;
52 private ValidationHelper(GlobalValidationContext globalContext, String fileName) {
53 this.globalContext = globalContext;
54 this.fileName = fileName;
57 public void validate() {
58 Option.ofOptional(globalContext.getFileContent(fileName))
59 .peek(this::validateFileContent)
60 .onEmpty(() -> addErrorToContext(formatMessage("File is empty")));
63 private void validateFileContent(InputStream inputStream) {
64 Try.of(inputStream::readAllBytes)
65 .mapTry(fileContent -> new YamlContentValidator().validate(fileContent))
66 .onSuccess(this::reportValidationErrorsIfPresent)
67 .onFailure(e -> addErrorToContext(formatMessage(e.getMessage())));
70 private void reportValidationErrorsIfPresent(List<YamlDocumentValidationError> validationErrors) {
71 validationErrors.stream()
72 .map(this::prepareValidationMessage)
73 .forEach(this::addErrorToContext);
76 private String prepareValidationMessage(YamlDocumentValidationError error) {
77 final String errorMessage = String.format("Document Number: %s, Path: %s, Problem: %s",
78 error.getYamlDocumentNumber(),
82 return formatMessage(errorMessage);
85 private String formatMessage(String message) {
86 return ErrorMessagesFormatBuilder
87 .getErrorWithParameters(PM_DICT_ERROR_CODE, message);
90 private void addErrorToContext(String message) {
91 globalContext.addMessage(fileName, ErrorLevel.ERROR, message);