2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.validation.impl.validators;
23 import org.openecomp.core.utilities.json.JsonUtil;
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.common.utils.SdcCommon;
29 import org.openecomp.sdc.datatypes.error.ErrorLevel;
30 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
31 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
35 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
36 import org.openecomp.sdc.logging.types.LoggerConstants;
37 import org.openecomp.sdc.logging.types.LoggerErrorCode;
38 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
39 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
41 import java.io.InputStream;
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.Optional;
47 public class ManifestValidator implements Validator {
48 public static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
49 private static Logger logger = (Logger) LoggerFactory.getLogger(YamlValidator.class);
52 public void validate(GlobalValidationContext globalContext) {
53 mdcDataDebugMessage.debugEntryMessage(null, null);
55 Optional<InputStream> content = globalContext.getFileContent(SdcCommon.MANIFEST_NAME);
56 ManifestContent manifestContent;
59 if (content.isPresent()) {
60 manifestContent = JsonUtil.json2Object(content.get(), ManifestContent.class);
62 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
63 LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT, ErrorLevel.ERROR.name(),
64 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.EMPTY_FILE);
65 throw new Exception("The manifest file '" + SdcCommon.MANIFEST_NAME + "' has no content");
67 } catch (Exception re) {
68 globalContext.addMessage(SdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
69 Messages.INVALID_MANIFEST_FILE.getErrorMessage(),
70 LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT,
71 LoggerErrorDescription.INVALID_MANIFEST);
75 List<String> manifestFiles = getManifestFileList(manifestContent, globalContext);
76 manifestFiles.stream().filter(name ->
77 !globalContext.getFileContextMap().containsKey(name)
78 ).forEach(name -> globalContext
79 .addMessage(name, ErrorLevel.ERROR, Messages.MISSING_FILE_IN_ZIP.getErrorMessage(),
80 LoggerTragetServiceName.VALIDATE_FILE_IN_ZIP, LoggerErrorDescription.MISSING_FILE));
82 globalContext.getFileContextMap().keySet().stream().filter(name ->
83 !manifestFiles.contains(name) && !SdcCommon.MANIFEST_NAME.equals(name)
85 globalContext.addMessage(name, ErrorLevel.WARNING,
86 Messages.MISSING_FILE_IN_MANIFEST.getErrorMessage(),
87 LoggerTragetServiceName.VALIDATE_FILE_IN_MANIFEST, LoggerErrorDescription.MISSING_FILE)
90 mdcDataDebugMessage.debugExitMessage(null, null);
94 private List<String> getManifestFileList(ManifestContent manifestContent,
95 GlobalValidationContext context) {
98 mdcDataDebugMessage.debugEntryMessage(null, null);
100 ManifestScanner manifestScanner = new ManifestScanner();
101 manifestScanner.init(context);
102 manifestScanner.scan(null, manifestContent.getData(), context);
104 mdcDataDebugMessage.debugExitMessage(null, null);
105 return manifestScanner.getFileList();
109 private class ManifestScanner {
110 private GlobalValidationContext globalValidationContext;
111 private List<String> fileList;
113 public void init(GlobalValidationContext globalValidationContext) {
114 this.globalValidationContext = globalValidationContext;
115 this.fileList = new ArrayList<>();
119 public void scan(FileData fileData, List<FileData> data,
120 GlobalValidationContext globalContext) {
121 if (fileData == null) {
122 for (FileData childFileData : data) {
123 if (childFileData.getType() != null
124 && childFileData.getType().equals(FileData.Type.HEAT_ENV)) {
125 globalContext.addMessage(childFileData.getFile(), ErrorLevel.ERROR,
126 ErrorMessagesFormatBuilder
127 .getErrorWithParameters(Messages.ENV_NOT_ASSOCIATED_TO_HEAT.getErrorMessage()),
128 LoggerTragetServiceName.SCAN_MANIFEST_STRUCTURE,
129 "env file is not associated to HEAT file");
133 if (fileData != null) {
134 fileList.add(fileData.getFile());
135 validateFileTypeVsFileName(fileData);
140 data.forEach(chileFileData -> scan(chileFileData, chileFileData.getData(), globalContext));
144 public List<String> getFileList() {
145 return this.fileList;
148 private void validateFileTypeVsFileName(FileData fileData) {
149 String fileName = fileData.getFile();
150 if (fileName == null) {
151 this.globalValidationContext.addMessage(SdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
152 Messages.MISSING_FILE_NAME_IN_MANIFEST.getErrorMessage(),
153 LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME, "Missing file name in manifest");
156 FileData.Type type = fileData.getType();
158 this.globalValidationContext
159 .addMessage(fileName, ErrorLevel.ERROR, Messages.INVALID_FILE_TYPE.getErrorMessage(),
160 LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME, "Invalid file type");
161 } else if (type.equals(FileData.Type.HEAT_NET) || type.equals(FileData.Type.HEAT_VOL)
162 || type.equals(FileData.Type.HEAT)) {
163 if (fileName != null && !fileName.endsWith(".yml") && !fileName.endsWith(".yaml")) {
164 this.globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
165 ErrorMessagesFormatBuilder
166 .getErrorWithParameters(Messages.WRONG_HEAT_FILE_EXTENSION.getErrorMessage(),
167 fileName), LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME,
168 "Wrong HEAT file extention");
170 } else if (type.equals(FileData.Type.HEAT_ENV)) {
171 if (fileName != null && !fileName.endsWith(".env")) {
172 this.globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
173 ErrorMessagesFormatBuilder
174 .getErrorWithParameters(Messages.WRONG_ENV_FILE_EXTENSION.getErrorMessage(),
175 fileName), LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME,
176 "Wrong env file extention");