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