7e1ba98de590a84533adee3e0a7658d935433210
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.validation.impl.validators;
18
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.context.impl.MdcDataDebugMessage;
25 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
26 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
27 import org.openecomp.sdc.tosca.services.YamlUtil;
28 import org.openecomp.sdc.validation.Validator;
29 import org.openecomp.sdc.validation.impl.util.YamlValidatorUtil;
30
31 import java.io.InputStream;
32 import java.util.Collection;
33 import java.util.Map;
34 import java.util.Optional;
35
36 public class YamlValidator implements Validator {
37   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
38   private static final ErrorMessageCode ERROR_CODE_YML_1 = new ErrorMessageCode("YML1");
39   private static final ErrorMessageCode ERROR_CODE_YML_2 = new ErrorMessageCode("YML2");
40
41   @Override
42   public void validate(GlobalValidationContext globalContext) {
43     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(null, null);
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     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(null, null);
52   }
53
54   private void validate(String fileName, GlobalValidationContext globalContext) {
55     Optional<InputStream> rowContent = globalContext.getFileContent(fileName);
56     if (!rowContent.isPresent()) {
57       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
58               .getErrorWithParameters(ERROR_CODE_YML_1, Messages
59                       .INVALID_YAML_FORMAT_REASON.getErrorMessage(),
60                   Messages.EMPTY_YAML_FILE.getErrorMessage()),
61           LoggerTragetServiceName.VALIDATE_YAML_CONTENT,
62           LoggerErrorDescription.INVALID_YAML_FORMAT);
63       return; /* no need to continue validation */
64     }
65
66     try {
67       convert(rowContent.get(), Map.class);
68     } catch (Exception exception) {
69
70       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
71               .getErrorWithParameters(ERROR_CODE_YML_2, Messages
72                       .INVALID_YAML_FORMAT_REASON.getErrorMessage(),
73                   YamlValidatorUtil.getParserExceptionReason(exception)),
74           LoggerTragetServiceName.VALIDATE_YAML_CONTENT,
75           LoggerErrorDescription.INVALID_YAML_FORMAT);
76     }
77   }
78
79   private <T> T convert(InputStream content, Class<T> type) {
80     return new YamlUtil().yamlToObject(content, type);
81   }
82 }