f5338c5d3b7b955e63ef8e656d3c0af1a0018c5d
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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 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;
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         globalContext.getFiles().stream()
42             .filter(FileExtensionUtils::isPmDictionary)
43             .map(fileName -> new ValidationHelper(globalContext, fileName))
44             .forEach(ValidationHelper::validate);
45     }
46
47     private static class ValidationHelper {
48
49         private final GlobalValidationContext globalContext;
50         private final String fileName;
51
52         private ValidationHelper(GlobalValidationContext globalContext, String fileName) {
53             this.globalContext = globalContext;
54             this.fileName = fileName;
55         }
56
57         public void validate() {
58             Option.ofOptional(globalContext.getFileContent(fileName))
59                 .peek(this::validateFileContent)
60                 .onEmpty(() -> addErrorToContext(formatMessage("File is empty")));
61         }
62
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())));
68         }
69
70         private void reportValidationErrorsIfPresent(List<YamlDocumentValidationError> validationErrors) {
71             validationErrors.stream()
72                 .map(this::prepareValidationMessage)
73                 .forEach(this::addErrorToContext);
74         }
75
76         private String prepareValidationMessage(YamlDocumentValidationError error) {
77             final String errorMessage = String.format("Document Number: %s, Path: %s, Problem: %s",
78                 error.getYamlDocumentNumber(),
79                 error.getPath(),
80                 error.getMessage()
81             );
82             return formatMessage(errorMessage);
83         }
84
85         private String formatMessage(String message) {
86             return ErrorMessagesFormatBuilder
87                 .getErrorWithParameters(PM_DICT_ERROR_CODE, message);
88         }
89
90         private void addErrorToContext(String message) {
91             globalContext.addMessage(fileName, ErrorLevel.ERROR, message);
92         }
93     }
94 }