4dad4afc16370e0ccd5150ec3a01ee6a8885a65a
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.validation.impl.validators;
21
22 import io.vavr.control.Option;
23 import io.vavr.control.Try;
24 import java.io.InputStream;
25 import java.util.List;
26 import java.util.Set;
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;
34
35 public class PmDictionaryValidator implements Validator {
36
37     private static final ErrorMessageCode PM_DICT_ERROR_CODE = new ErrorMessageCode("PM_DICT");
38
39     @Override
40     public void validate(GlobalValidationContext globalContext) {
41         Set<String> pmDictionaryFiles = GlobalContextUtil.findPmDictionaryFiles(globalContext);
42         validatePmDictionaryFiles(globalContext, pmDictionaryFiles);
43     }
44
45     private void validatePmDictionaryFiles(GlobalValidationContext globalContext, Set<String> pmDictionaryFiles) {
46         pmDictionaryFiles.stream().map(fileName -> new ValidationHelper(globalContext, fileName)).forEach(ValidationHelper::validate);
47     }
48
49     private static class ValidationHelper {
50
51         private final GlobalValidationContext globalContext;
52         private final String fileName;
53
54         private ValidationHelper(GlobalValidationContext globalContext, String fileName) {
55             this.globalContext = globalContext;
56             this.fileName = fileName;
57         }
58
59         public void validate() {
60             Option.ofOptional(globalContext.getFileContent(fileName)).peek(this::validateFileContent)
61                 .onEmpty(() -> addErrorToContext(formatMessage("File is empty")));
62         }
63
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())));
67         }
68
69         private void reportValidationErrorsIfPresent(List<YamlDocumentValidationError> validationErrors) {
70             validationErrors.stream().map(this::prepareValidationMessage).forEach(this::addErrorToContext);
71         }
72
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);
77         }
78
79         private String formatMessage(String message) {
80             return ErrorMessagesFormatBuilder.getErrorWithParameters(PM_DICT_ERROR_CODE, message);
81         }
82
83         private void addErrorToContext(String message) {
84             globalContext.addMessage(fileName, ErrorLevel.ERROR, message);
85         }
86     }
87 }