push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / YamlValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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 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;
33
34 import java.io.InputStream;
35 import java.util.Collection;
36 import java.util.Map;
37
38 public class YamlValidator implements Validator {
39
40   private static final Logger logger = LoggerFactory.getLogger(YamlValidator.class);
41
42   @Override
43   public void validate(GlobalValidationContext globalContext) {
44
45     Collection<String> files = globalContext.files(
46         (fileName, globalValidationContext) -> (fileName.endsWith(".yaml")
47             || fileName.endsWith(".yml") || fileName.endsWith(".env")));
48
49     files.stream().forEach(fileName -> validate(fileName, globalContext));
50   }
51
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 */
59     }
60
61     try {
62       convert(rowContent, Map.class);
63     } catch (Exception exception) {
64
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());
69     }
70   }
71
72   private String getParserExceptionReason(Exception exception) {
73     String reason = null;
74
75     if (exception.getCause() instanceof MarkedYAMLException) {
76       if (exception.getCause() != null) {
77         if (exception.getCause().getCause() instanceof ParserException) {
78           reason = exception.getCause().getCause().getMessage();
79         } else {
80           reason = exception.getCause().getMessage();
81         }
82       }
83     } else if (exception instanceof MarkedYAMLException) {
84
85       reason = exception.getMessage();
86
87     } else {
88       reason = Messages.GENERAL_YAML_PARSER_ERROR.getErrorMessage();
89     }
90     return reason;
91   }
92
93
94   private <T> T convert(InputStream content, Class<T> type) {
95     return new YamlUtil().yamlToObject(content, type);
96   }
97 }