2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.validation.impl.validators;
19 import org.openecomp.core.validation.ErrorMessageCode;
20 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
21 import org.openecomp.core.validation.types.GlobalValidationContext;
22 import org.openecomp.sdc.common.errors.Messages;
23 import org.openecomp.sdc.datatypes.error.ErrorLevel;
24 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
25 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
26 import org.openecomp.sdc.tosca.services.YamlUtil;
27 import org.openecomp.sdc.validation.Validator;
28 import org.openecomp.sdc.validation.impl.util.YamlValidatorUtil;
30 import java.io.InputStream;
31 import java.util.Collection;
33 import java.util.Optional;
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");
40 public void validate(GlobalValidationContext globalContext) {
41 Collection<String> files = globalContext.files(
42 (fileName, globalValidationContext) -> fileName.endsWith(".yaml")
43 || fileName.endsWith(".yml") || fileName.endsWith(".env"));
45 files.stream().forEach(fileName -> validate(fileName, globalContext));
48 private void validate(String fileName, GlobalValidationContext globalContext) {
49 Optional<InputStream> rowContent = globalContext.getFileContent(fileName);
50 if (!rowContent.isPresent()) {
51 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
52 .getErrorWithParameters(ERROR_CODE_YML_1, Messages
53 .INVALID_YAML_FORMAT_REASON.getErrorMessage(),
54 Messages.EMPTY_YAML_FILE.getErrorMessage()),
55 LoggerTragetServiceName.VALIDATE_YAML_CONTENT,
56 LoggerErrorDescription.INVALID_YAML_FORMAT);
57 return; /* no need to continue validation */
61 convert(rowContent.get(), Map.class);
62 } catch (Exception exception) {
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 LoggerTragetServiceName.VALIDATE_YAML_CONTENT,
69 LoggerErrorDescription.INVALID_YAML_FORMAT);
73 private <T> T convert(InputStream content, Class<T> type) {
74 return new YamlUtil().yamlToObject(content, type);