2 * Copyright © 2016-2017 European Support Limited
3 * Copyright © 2020-2021 Nokia
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.openecomp.sdc.validation.impl.validators;
20 import org.onap.sdc.tosca.services.MyPropertyUtils;
21 import org.onap.sdc.tosca.services.StrictMapAppenderConstructor;
22 import org.openecomp.core.validation.ErrorMessageCode;
23 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
24 import org.openecomp.core.validation.types.GlobalValidationContext;
25 import org.openecomp.sdc.common.errors.Messages;
26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.validation.Validator;
28 import org.openecomp.sdc.validation.impl.util.YamlValidatorUtil;
29 import org.yaml.snakeyaml.DumperOptions;
30 import org.yaml.snakeyaml.LoaderOptions;
31 import org.yaml.snakeyaml.TypeDescription;
32 import org.yaml.snakeyaml.Yaml;
33 import org.yaml.snakeyaml.constructor.Constructor;
34 import org.yaml.snakeyaml.representer.Representer;
36 import java.io.InputStream;
37 import java.util.Collection;
39 import java.util.Optional;
42 public class YamlValidator implements Validator {
43 private static final ErrorMessageCode ERROR_CODE_YML_1 = new ErrorMessageCode("YML1");
44 private static final ErrorMessageCode ERROR_CODE_YML_2 = new ErrorMessageCode("YML2");
47 public void validate(GlobalValidationContext globalContext) {
48 Set<String> pmDictionaryFiles = GlobalContextUtil.findPmDictionaryFiles(globalContext);
50 Collection<String> files = globalContext.files(
51 (fileName, globalValidationContext) -> FileExtensionUtils.isYaml(fileName)
52 && !pmDictionaryFiles.contains(fileName));
54 files.forEach(fileName -> validate(fileName, globalContext));
57 private void validate(String fileName, GlobalValidationContext globalContext) {
58 Optional<InputStream> rowContent = globalContext.getFileContent(fileName);
59 if (rowContent.isEmpty()) {
60 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
61 .getErrorWithParameters(ERROR_CODE_YML_1, Messages
62 .INVALID_YAML_FORMAT_REASON.getErrorMessage(),
63 Messages.EMPTY_YAML_FILE.getErrorMessage()));
64 return; /* no need to continue validation */
67 try (var yamlContent = rowContent.get()) {
68 Constructor constructor = new StrictMapAppenderConstructor(Map.class);
69 constructor.setAllowDuplicateKeys(false);
70 constructor.setPropertyUtils(new MyPropertyUtils());
71 TypeDescription yamlFileDescription = new TypeDescription(Map.class);
72 constructor.addTypeDescription(yamlFileDescription);
73 LoaderOptions options = new LoaderOptions();
74 options.setAllowDuplicateKeys(false);
75 //No Yaml Constructor takes only Constructor and LoaderOptions, that is why I had to pass anonymous Representer and DumperOptions objects
76 Object yamlObj = new Yaml(constructor, new Representer(), new DumperOptions(), options).load(yamlContent);
78 if (yamlObj == null) {
79 throw new Exception();
81 } catch (Exception exception) {
82 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
83 .getErrorWithParameters(ERROR_CODE_YML_2, Messages
84 .INVALID_YAML_FORMAT_REASON.getErrorMessage(),
85 YamlValidatorUtil.getParserExceptionReason(exception)));