b11ff5e341b7e5e2d5eade13232129b777ce06d7
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  * Copyright © 2020-2021 Nokia
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.openecomp.sdc.validation.impl.validators;
19
20 import org.onap.sdc.tosca.services.YamlUtil;
21 import org.openecomp.core.validation.ErrorMessageCode;
22 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
23 import org.openecomp.core.validation.types.GlobalValidationContext;
24 import org.openecomp.sdc.common.errors.Messages;
25 import org.openecomp.sdc.datatypes.error.ErrorLevel;
26 import org.openecomp.sdc.validation.Validator;
27 import org.openecomp.sdc.validation.impl.util.YamlValidatorUtil;
28
29 import java.io.InputStream;
30 import java.util.Collection;
31 import java.util.Map;
32 import java.util.Optional;
33 import java.util.Set;
34
35 public class YamlValidator implements Validator {
36   private static final ErrorMessageCode ERROR_CODE_YML_1 = new ErrorMessageCode("YML1");
37   private static final ErrorMessageCode ERROR_CODE_YML_2 = new ErrorMessageCode("YML2");
38
39   @Override
40   public void validate(GlobalValidationContext globalContext) {
41     Set<String> pmDictionaryFiles = GlobalContextUtil.findPmDictionaryFiles(globalContext);
42
43     Collection<String> files = globalContext.files(
44         (fileName, globalValidationContext) -> FileExtensionUtils.isYaml(fileName)
45             && !pmDictionaryFiles.contains(fileName));
46
47     files.forEach(fileName -> validate(fileName, globalContext));
48   }
49
50   private void validate(String fileName, GlobalValidationContext globalContext) {
51     Optional<InputStream> rowContent = globalContext.getFileContent(fileName);
52     if (rowContent.isEmpty()) {
53       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
54               .getErrorWithParameters(ERROR_CODE_YML_1, Messages
55                       .INVALID_YAML_FORMAT_REASON.getErrorMessage(),
56                   Messages.EMPTY_YAML_FILE.getErrorMessage()));
57       return; /* no need to continue validation */
58     }
59
60     try {
61       convert(rowContent.get(), Map.class);
62     } catch (Exception exception) {
63
64       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
65               .getErrorWithParameters(ERROR_CODE_YML_2, Messages
66                       .INVALID_YAML_FORMAT_REASON.getErrorMessage(),
67                   YamlValidatorUtil.getParserExceptionReason(exception)));
68     }
69   }
70
71   private <T> T convert(InputStream content, Class<T> type) {
72     return new YamlUtil().yamlToObject(content, type);
73   }
74 }