05021fdf4f9792d4d5a513ca17b73b9e4d379b8c
[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
21 package org.openecomp.sdc.validation.impl.validators;
22
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;
32
33 import java.io.InputStream;
34 import java.util.List;
35 import java.util.Set;
36
37 public class PmDictionaryValidator implements Validator {
38
39     private static final ErrorMessageCode PM_DICT_ERROR_CODE = new ErrorMessageCode("PM_DICT");
40
41     @Override
42     public void validate(GlobalValidationContext globalContext) {
43         Set<String> pmDictionaryFiles = GlobalContextUtil.findPmDictionaryFiles(globalContext);
44         validatePmDictionaryFiles(globalContext, pmDictionaryFiles);
45     }
46
47
48     private void validatePmDictionaryFiles(GlobalValidationContext globalContext, Set<String> pmDictionaryFiles) {
49         pmDictionaryFiles.stream()
50                 .map(fileName -> new ValidationHelper(globalContext, fileName))
51                 .forEach(ValidationHelper::validate);
52     }
53
54     private static class ValidationHelper {
55
56         private final GlobalValidationContext globalContext;
57         private final String fileName;
58
59         private ValidationHelper(GlobalValidationContext globalContext, String fileName) {
60             this.globalContext = globalContext;
61             this.fileName = fileName;
62         }
63
64         public void validate() {
65             Option.ofOptional(globalContext.getFileContent(fileName))
66                     .peek(this::validateFileContent)
67                     .onEmpty(() -> addErrorToContext(formatMessage("File is empty")));
68         }
69
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())));
75         }
76
77         private void reportValidationErrorsIfPresent(List<YamlDocumentValidationError> validationErrors) {
78             validationErrors.stream()
79                     .map(this::prepareValidationMessage)
80                     .forEach(this::addErrorToContext);
81         }
82
83         private String prepareValidationMessage(YamlDocumentValidationError error) {
84             final String errorMessage = String.format("Document Number: %s, Path: %s, Problem: %s",
85                     error.getYamlDocumentNumber(),
86                     error.getPath(),
87                     error.getMessage()
88             );
89             return formatMessage(errorMessage);
90         }
91
92         private String formatMessage(String message) {
93             return ErrorMessagesFormatBuilder
94                     .getErrorWithParameters(PM_DICT_ERROR_CODE, message);
95         }
96
97         private void addErrorToContext(String message) {
98             globalContext.addMessage(fileName, ErrorLevel.ERROR, message);
99         }
100     }
101 }