Add collaboration feature
[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.validation.ErrorMessageCode;
24 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
25 import org.openecomp.core.validation.types.GlobalValidationContext;
26 import org.openecomp.sdc.common.errors.Messages;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
31 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
32 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
33 import org.openecomp.sdc.tosca.services.YamlUtil;
34 import org.openecomp.sdc.validation.Validator;
35 import org.openecomp.sdc.validation.impl.util.YamlValidatorUtil;
36
37 import java.io.InputStream;
38 import java.util.Collection;
39 import java.util.Map;
40 import java.util.Optional;
41
42 public class YamlValidator implements Validator {
43   public static final MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
44   private static final Logger logger = (Logger) LoggerFactory.getLogger(YamlValidator.class);
45   private static final ErrorMessageCode ERROR_CODE_YML_1 = new ErrorMessageCode("YML1");
46   private static final ErrorMessageCode ERROR_CODE_YML_2 = new ErrorMessageCode("YML2");
47
48   @Override
49   public void validate(GlobalValidationContext globalContext) {
50     mdcDataDebugMessage.debugEntryMessage(null, null);
51
52     Collection<String> files = globalContext.files(
53         (fileName, globalValidationContext) -> (fileName.endsWith(".yaml")
54             || fileName.endsWith(".yml") || fileName.endsWith(".env")));
55
56     files.stream().forEach(fileName -> validate(fileName, globalContext));
57
58     mdcDataDebugMessage.debugExitMessage(null, null);
59   }
60
61   private void validate(String fileName, GlobalValidationContext globalContext) {
62     Optional<InputStream> rowContent = globalContext.getFileContent(fileName);
63     if (!rowContent.isPresent()) {
64       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
65               .getErrorWithParameters(ERROR_CODE_YML_1, Messages
66                       .INVALID_YAML_FORMAT_REASON.getErrorMessage(),
67                   Messages.EMPTY_YAML_FILE.getErrorMessage()),
68           LoggerTragetServiceName.VALIDATE_YAML_CONTENT,
69           LoggerErrorDescription.INVALID_YAML_FORMAT);
70       return; /* no need to continue validation */
71     }
72
73     try {
74       convert(rowContent.get(), Map.class);
75     } catch (Exception exception) {
76
77       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
78               .getErrorWithParameters(ERROR_CODE_YML_2, Messages
79                       .INVALID_YAML_FORMAT_REASON.getErrorMessage(),
80                   YamlValidatorUtil.getParserExceptionReason(exception)),
81           LoggerTragetServiceName.VALIDATE_YAML_CONTENT,
82           LoggerErrorDescription.INVALID_YAML_FORMAT);
83     }
84   }
85
86   private <T> T convert(InputStream content, Class<T> type) {
87     return new YamlUtil().yamlToObject(content, type);
88   }
89 }