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=========================================================
20 package org.openecomp.sdc.validation.impl.validators;
22 import io.vavr.control.Option;
23 import io.vavr.control.Try;
24 import java.io.InputStream;
25 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 Set<String> pmDictionaryFiles = GlobalContextUtil.findPmDictionaryFiles(globalContext);
42 validatePmDictionaryFiles(globalContext, pmDictionaryFiles);
45 private void validatePmDictionaryFiles(GlobalValidationContext globalContext, Set<String> pmDictionaryFiles) {
46 pmDictionaryFiles.stream().map(fileName -> new ValidationHelper(globalContext, fileName)).forEach(ValidationHelper::validate);
49 private static class ValidationHelper {
51 private final GlobalValidationContext globalContext;
52 private final String fileName;
54 private ValidationHelper(GlobalValidationContext globalContext, String fileName) {
55 this.globalContext = globalContext;
56 this.fileName = fileName;
59 public void validate() {
60 Option.ofOptional(globalContext.getFileContent(fileName)).peek(this::validateFileContent)
61 .onEmpty(() -> addErrorToContext(formatMessage("File is empty")));
64 private void validateFileContent(InputStream inputStream) {
65 Try.of(inputStream::readAllBytes).mapTry(fileContent -> new YamlContentValidator().validate(fileContent))
66 .onSuccess(this::reportValidationErrorsIfPresent).onFailure(e -> addErrorToContext(formatMessage(e.getMessage())));
69 private void reportValidationErrorsIfPresent(List<YamlDocumentValidationError> validationErrors) {
70 validationErrors.stream().map(this::prepareValidationMessage).forEach(this::addErrorToContext);
73 private String prepareValidationMessage(YamlDocumentValidationError error) {
74 final String errorMessage = String
75 .format("Document Number: %s, Path: %s, Problem: %s", error.getYamlDocumentNumber(), error.getPath(), error.getMessage());
76 return formatMessage(errorMessage);
79 private String formatMessage(String message) {
80 return ErrorMessagesFormatBuilder.getErrorWithParameters(PM_DICT_ERROR_CODE, message);
83 private void addErrorToContext(String message) {
84 globalContext.addMessage(fileName, ErrorLevel.ERROR, message);