2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.validation.impl.validators;
23 import org.openecomp.core.utilities.yaml.YamlUtil;
24 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
25 import org.openecomp.core.validation.errors.Messages;
26 import org.openecomp.core.validation.interfaces.Validator;
27 import org.openecomp.core.validation.types.GlobalValidationContext;
28 import org.openecomp.sdc.datatypes.error.ErrorLevel;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.yaml.snakeyaml.error.MarkedYAMLException;
32 import org.yaml.snakeyaml.parser.ParserException;
34 import java.io.InputStream;
35 import java.util.Collection;
38 public class YamlValidator implements Validator {
40 private static final Logger logger = LoggerFactory.getLogger(YamlValidator.class);
43 public void validate(GlobalValidationContext globalContext) {
45 Collection<String> files = globalContext.files(
46 (fileName, globalValidationContext) -> (fileName.endsWith(".yaml")
47 || fileName.endsWith(".yml") || fileName.endsWith(".env")));
49 files.stream().forEach(fileName -> validate(fileName, globalContext));
52 private void validate(String fileName, GlobalValidationContext globalContext) {
53 InputStream rowContent = globalContext.getFileContent(fileName);
54 if (rowContent == null) {
55 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
56 .getErrorWithParameters(Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
57 Messages.EMPTY_YAML_FILE.getErrorMessage()));
58 return; /* no need to continue validation */
62 convert(rowContent, Map.class);
63 } catch (Exception exception) {
65 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
66 .getErrorWithParameters(Messages.INVALID_YAML_FORMAT_REASON.getErrorMessage(),
67 getParserExceptionReason(exception)));
68 logger.error("Exception in yaml parser. message:" + exception.getMessage());
72 private String getParserExceptionReason(Exception exception) {
75 if (exception.getCause() instanceof MarkedYAMLException) {
76 if (exception.getCause() != null) {
77 if (exception.getCause().getCause() instanceof ParserException) {
78 reason = exception.getCause().getCause().getMessage();
80 reason = exception.getCause().getMessage();
83 } else if (exception instanceof MarkedYAMLException) {
85 reason = exception.getMessage();
88 reason = Messages.GENERAL_YAML_PARSER_ERROR.getErrorMessage();
94 private <T> T convert(InputStream content, Class<T> type) {
95 return new YamlUtil().yamlToObject(content, type);