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.core.validation.errors.ErrorMessagesFormatBuilder;
25 import org.openecomp.core.validation.errors.Messages;
26 import org.openecomp.core.validation.interfaces.Validator;
27 import org.openecomp.core.validation.types.GlobalValidationContext;
28 import org.openecomp.sdc.common.utils.AsdcCommon;
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.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.io.InputStream;
36 import java.util.ArrayList;
37 import java.util.List;
39 public class ManifestValidator implements Validator {
41 private static Logger logger = LoggerFactory.getLogger(YamlValidator.class);
45 public void validate(GlobalValidationContext globalContext) {
48 InputStream content = globalContext.getFileContent(AsdcCommon.MANIFEST_NAME);
49 ManifestContent manifestContent;
52 manifestContent = JsonUtil.json2Object(content, ManifestContent.class);
53 } catch (RuntimeException re) {
54 globalContext.addMessage(AsdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
55 Messages.INVALID_MANIFEST_FILE.getErrorMessage());
59 List<String> manifestFiles = getManifestFileList(manifestContent, globalContext);
60 manifestFiles.stream().filter(name ->
61 !globalContext.getFileContextMap().containsKey(name)
62 ).forEach(name -> globalContext
63 .addMessage(name, ErrorLevel.ERROR, Messages.MISSING_FILE_IN_ZIP.getErrorMessage()));
65 globalContext.getFileContextMap().keySet().stream().filter(name ->
66 !manifestFiles.contains(name) && !AsdcCommon.MANIFEST_NAME.equals(name)
68 globalContext.addMessage(name, ErrorLevel.WARNING,
69 Messages.MISSING_FILE_IN_MANIFEST.getErrorMessage())
74 private List<String> getManifestFileList(ManifestContent manifestContent,
75 GlobalValidationContext context) {
76 ManifestScanner manifestScanner = new ManifestScanner();
77 manifestScanner.init(context);
78 manifestScanner.scan(null, manifestContent.getData(), context);
79 return manifestScanner.getFileList();
83 private class ManifestScanner {
84 private GlobalValidationContext globalValidationContext;
85 private List<String> fileList;
87 public void init(GlobalValidationContext globalValidationContext) {
88 this.globalValidationContext = globalValidationContext;
89 this.fileList = new ArrayList<>();
93 public void scan(FileData fileData, List<FileData> data,
94 GlobalValidationContext globalContext) {
95 if (fileData == null) {
96 for (FileData childFileData : data) {
97 if (childFileData.getType() != null
98 && childFileData.getType().equals(FileData.Type.HEAT_ENV)) {
99 globalContext.addMessage(childFileData.getFile(), ErrorLevel.ERROR,
100 ErrorMessagesFormatBuilder
101 .getErrorWithParameters(Messages.ENV_NOT_ASSOCIATED_TO_HEAT.getErrorMessage()));
105 if (fileData != null) {
106 fileList.add(fileData.getFile());
107 validateFileTypeVsFileName(fileData);
112 data.stream().forEach(chileFileData -> {
113 scan(chileFileData, chileFileData.getData(), globalContext);
118 public List<String> getFileList() {
119 return this.fileList;
122 private void validateFileTypeVsFileName(FileData fileData) {
123 String fileName = fileData.getFile();
124 if (fileName == null) {
125 this.globalValidationContext.addMessage(AsdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
126 Messages.MISSING_FILE_NAME_IN_MANIFEST.getErrorMessage());
129 FileData.Type type = fileData.getType();
131 this.globalValidationContext
132 .addMessage(fileName, ErrorLevel.ERROR, Messages.INVALID_FILE_TYPE.getErrorMessage());
133 } else if (type.equals(FileData.Type.HEAT_NET) || type.equals(FileData.Type.HEAT_VOL)
134 || type.equals(FileData.Type.HEAT)) {
135 if (fileName != null && !fileName.endsWith(".yml") && !fileName.endsWith(".yaml")) {
136 this.globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
137 ErrorMessagesFormatBuilder
138 .getErrorWithParameters(Messages.WRONG_HEAT_FILE_EXTENSION.getErrorMessage(),
141 } else if (type.equals(FileData.Type.HEAT_ENV)) {
142 if (fileName != null && !fileName.endsWith(".env")) {
143 this.globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
144 ErrorMessagesFormatBuilder
145 .getErrorWithParameters(Messages.WRONG_ENV_FILE_EXTENSION.getErrorMessage(),