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.utilities.json.JsonUtil;
20 import org.openecomp.core.validation.ErrorMessageCode;
21 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
22 import org.openecomp.core.validation.types.GlobalValidationContext;
23 import org.openecomp.sdc.common.errors.Messages;
24 import org.openecomp.sdc.common.utils.SdcCommon;
25 import org.openecomp.sdc.datatypes.error.ErrorLevel;
26 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
27 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.validation.Validator;
32 import java.io.InputStream;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Optional;
38 public class ManifestValidator implements Validator {
39 private static final Logger LOGGER = LoggerFactory.getLogger(YamlValidator.class);
40 private static final ErrorMessageCode ERROR_CODE_MNF_1 = new ErrorMessageCode("MNF1");
41 private static final ErrorMessageCode ERROR_CODE_MNF_2 = new ErrorMessageCode("MNF2");
42 private static final ErrorMessageCode ERROR_CODE_MNF_3 = new ErrorMessageCode("MNF3");
43 private static final ErrorMessageCode ERROR_CODE_MNF_4 = new ErrorMessageCode("MNF4");
44 private static final ErrorMessageCode ERROR_CODE_MNF_5 = new ErrorMessageCode("MNF5");
45 private static final ErrorMessageCode ERROR_CODE_MNF_6 = new ErrorMessageCode("MNF6");
46 private static final ErrorMessageCode ERROR_CODE_MNF_7 = new ErrorMessageCode("MNF7");
47 private static final ErrorMessageCode ERROR_CODE_MNF_8 = new ErrorMessageCode("MNF8");
50 public void validate(GlobalValidationContext globalContext) {
51 Optional<InputStream> content = globalContext.getFileContent(SdcCommon.MANIFEST_NAME);
52 ManifestContent manifestContent;
55 if (content.isPresent()) {
56 manifestContent = JsonUtil.json2Object(content.get(), ManifestContent.class);
58 throw new Exception("The manifest file '" + SdcCommon.MANIFEST_NAME + "' has no content");
60 } catch (Exception re) {
62 globalContext.addMessage(SdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
63 ErrorMessagesFormatBuilder
64 .getErrorWithParameters(ERROR_CODE_MNF_6,
65 Messages.INVALID_MANIFEST_FILE.getErrorMessage()));
69 List<String> manifestFiles = getManifestFileList(manifestContent, globalContext);
70 manifestFiles.stream().filter(name ->
71 !globalContext.getFileContextMap().containsKey(name)
72 ).forEach(name -> globalContext
73 .addMessage(name, ErrorLevel.ERROR,ErrorMessagesFormatBuilder
74 .getErrorWithParameters(ERROR_CODE_MNF_4,
75 Messages.MISSING_FILE_IN_ZIP.getErrorMessage())));
77 globalContext.getFileContextMap().keySet().stream().filter(name ->
78 isNotManifestFiles(manifestFiles, name) && isNotManifestName(name)
80 globalContext.addMessage(name, ErrorLevel.WARNING,
81 ErrorMessagesFormatBuilder
82 .getErrorWithParameters(ERROR_CODE_MNF_5,
83 Messages.MISSING_FILE_IN_MANIFEST.getErrorMessage())));
86 private boolean isNotManifestFiles(List<String> manifestFiles, String name) {
87 return !manifestFiles.contains(name);
90 private boolean isNotManifestName(String name) {
91 return !SdcCommon.MANIFEST_NAME.equals(name);
95 private List<String> getManifestFileList(ManifestContent manifestContent,
96 GlobalValidationContext context) {
97 ManifestScanner manifestScanner = new ManifestScanner();
98 manifestScanner.scan(null, manifestContent.getData(), context);
99 return manifestScanner.getFileList();
103 private class ManifestScanner {
104 private final List<String> fileList = new ArrayList<>();
106 public void scan(FileData fileData, List<FileData> data,
107 GlobalValidationContext globalContext) {
108 if (fileData == null) {
109 for (FileData childFileData : data) {
110 validateIfEnvIsAssociatedToHeat(globalContext, childFileData);
113 if (fileData != null) {
114 fileList.add(fileData.getFile());
115 validateFileTypeVsFileName(globalContext,fileData);
120 data.forEach(chileFileData -> scan(chileFileData, chileFileData.getData(), globalContext));
123 public List<String> getFileList() {
124 return this.fileList;
127 private void validateFileTypeVsFileName(GlobalValidationContext globalValidationContext,
129 String fileName = fileData.getFile();
130 validateIfFileExists(globalValidationContext,fileName);
131 FileData.Type type = fileData.getType();
133 globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
134 ErrorMessagesFormatBuilder.getErrorWithParameters(ERROR_CODE_MNF_8,
135 Messages.INVALID_FILE_TYPE.getErrorMessage()));
136 } else if (type.equals(FileData.Type.HEAT_NET) || type.equals(FileData.Type.HEAT_VOL)
137 || type.equals(FileData.Type.HEAT)) {
138 validateIfFileHasYamlExtenstion(globalValidationContext,fileName);
139 } else if (type.equals(FileData.Type.HEAT_ENV)) {
140 validateIfFileHasEnvExtension(globalValidationContext,fileName);
144 private void validateIfEnvIsAssociatedToHeat(GlobalValidationContext globalContext,
145 FileData childFileData) {
146 if (childFileData.getType() != null
147 && childFileData.getType().equals(FileData.Type.HEAT_ENV)) {
148 globalContext.addMessage(childFileData.getFile(), ErrorLevel.ERROR,
149 ErrorMessagesFormatBuilder
150 .getErrorWithParameters(ERROR_CODE_MNF_1,
151 Messages.ENV_NOT_ASSOCIATED_TO_HEAT.getErrorMessage()));
155 private void validateIfFileHasEnvExtension(GlobalValidationContext globalValidationContext,
157 if (fileName != null && !fileName.endsWith(".env")) {
158 globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
159 ErrorMessagesFormatBuilder
160 .getErrorWithParameters(ERROR_CODE_MNF_3,
161 Messages.WRONG_ENV_FILE_EXTENSION.getErrorMessage(),
166 private void validateIfFileHasYamlExtenstion(GlobalValidationContext globalValidationContext,
168 if (fileName != null && !fileName.endsWith(".yml") && !fileName.endsWith(".yaml")) {
169 globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
170 ErrorMessagesFormatBuilder
171 .getErrorWithParameters(ERROR_CODE_MNF_2,
172 Messages.WRONG_HEAT_FILE_EXTENSION.getErrorMessage(),
177 private void validateIfFileExists(GlobalValidationContext globalValidationContext,
179 if (fileName == null) {
180 globalValidationContext.addMessage(SdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
181 ErrorMessagesFormatBuilder
182 .getErrorWithParameters(ERROR_CODE_MNF_7,
183 Messages.MISSING_FILE_NAME_IN_MANIFEST.getErrorMessage()));