08cd2450f273a7008b22554775bb7c11f453b648
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.validation.impl.validators;
18
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.logging.context.impl.MdcDataDebugMessage;
31 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
32 import org.openecomp.sdc.logging.types.LoggerConstants;
33 import org.openecomp.sdc.logging.types.LoggerErrorCode;
34 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
35 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
36 import org.openecomp.sdc.validation.Validator;
37
38 import java.io.InputStream;
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.Optional;
42
43
44 public class ManifestValidator implements Validator {
45   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
46   private static final Logger LOGGER = LoggerFactory.getLogger(YamlValidator.class);
47   private static final ErrorMessageCode ERROR_CODE_MNF_1 = new ErrorMessageCode("MNF1");
48   private static final ErrorMessageCode ERROR_CODE_MNF_2 = new ErrorMessageCode("MNF2");
49   private static final ErrorMessageCode ERROR_CODE_MNF_3 = new ErrorMessageCode("MNF3");
50   private static final ErrorMessageCode ERROR_CODE_MNF_4 = new ErrorMessageCode("MNF4");
51   private static final ErrorMessageCode ERROR_CODE_MNF_5 = new ErrorMessageCode("MNF5");
52   private static final ErrorMessageCode ERROR_CODE_MNF_6 = new ErrorMessageCode("MNF6");
53   private static final ErrorMessageCode ERROR_CODE_MNF_7 = new ErrorMessageCode("MNF7");
54   private static final ErrorMessageCode ERROR_CODE_MNF_8 = new ErrorMessageCode("MNF8");
55
56   @Override
57   public void validate(GlobalValidationContext globalContext) {
58     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(null, null);
59
60     Optional<InputStream> content = globalContext.getFileContent(SdcCommon.MANIFEST_NAME);
61     ManifestContent manifestContent;
62
63     try {
64       if (content.isPresent()) {
65         manifestContent = JsonUtil.json2Object(content.get(), ManifestContent.class);
66       } else {
67         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
68             LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT, ErrorLevel.ERROR.name(),
69             LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.EMPTY_FILE);
70         throw new Exception("The manifest file '" + SdcCommon.MANIFEST_NAME + "' has no content");
71       }
72     } catch (Exception re) {
73       LOGGER.debug("",re);
74       globalContext.addMessage(SdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
75               ErrorMessagesFormatBuilder
76                       .getErrorWithParameters(ERROR_CODE_MNF_6,
77                               Messages.INVALID_MANIFEST_FILE.getErrorMessage()),
78           LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT,
79           LoggerErrorDescription.INVALID_MANIFEST);
80       return;
81     }
82
83     List<String> manifestFiles = getManifestFileList(manifestContent, globalContext);
84     manifestFiles.stream().filter(name ->
85         !globalContext.getFileContextMap().containsKey(name)
86     ).forEach(name -> globalContext
87         .addMessage(name, ErrorLevel.ERROR,ErrorMessagesFormatBuilder
88                 .getErrorWithParameters(ERROR_CODE_MNF_4,
89                         Messages.MISSING_FILE_IN_ZIP.getErrorMessage()),
90             LoggerTragetServiceName.VALIDATE_FILE_IN_ZIP, LoggerErrorDescription.MISSING_FILE));
91
92     globalContext.getFileContextMap().keySet().stream().filter(name ->
93         isNotManifestFiles(manifestFiles, name) && isNotManifestName(name)
94     ).forEach(name ->
95         globalContext.addMessage(name, ErrorLevel.WARNING,
96                 ErrorMessagesFormatBuilder
97                         .getErrorWithParameters(ERROR_CODE_MNF_5,
98                                 Messages.MISSING_FILE_IN_MANIFEST.getErrorMessage()),
99             LoggerTragetServiceName.VALIDATE_FILE_IN_MANIFEST, LoggerErrorDescription.MISSING_FILE)
100     );
101
102     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(null, null);
103   }
104
105   private boolean isNotManifestFiles(List<String> manifestFiles, String name) {
106     return !manifestFiles.contains(name);
107   }
108
109   private boolean isNotManifestName(String name) {
110     return !SdcCommon.MANIFEST_NAME.equals(name);
111   }
112
113
114   private List<String> getManifestFileList(ManifestContent manifestContent,
115                                            GlobalValidationContext context) {
116
117
118     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(null, null);
119
120     ManifestScanner manifestScanner = new ManifestScanner();
121     manifestScanner.scan(null, manifestContent.getData(), context);
122
123     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(null, null);
124     return manifestScanner.getFileList();
125   }
126
127
128   private class ManifestScanner {
129     private List<String> fileList = new ArrayList<>();
130
131     public void scan(FileData fileData, List<FileData> data,
132                      GlobalValidationContext globalContext) {
133       if (fileData == null) {
134         for (FileData childFileData : data) {
135           validateIfEnvIsAssociatedToHeat(globalContext, childFileData);
136         }
137       }
138       if (fileData != null) {
139         fileList.add(fileData.getFile());
140         validateFileTypeVsFileName(globalContext,fileData);
141       }
142       if (data == null) {
143         return;
144       }
145       data.forEach(chileFileData -> scan(chileFileData, chileFileData.getData(), globalContext));
146     }
147
148     public List<String> getFileList() {
149       return this.fileList;
150     }
151
152     private void validateFileTypeVsFileName(GlobalValidationContext globalValidationContext,
153                                             FileData fileData) {
154       String fileName = fileData.getFile();
155       validateIfFileExists(globalValidationContext,fileName);
156       FileData.Type type = fileData.getType();
157       if (type == null) {
158         globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
159                 ErrorMessagesFormatBuilder.getErrorWithParameters(ERROR_CODE_MNF_8,
160                         Messages.INVALID_FILE_TYPE.getErrorMessage()),
161                 LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME, "Invalid file type");
162       } else if (type.equals(FileData.Type.HEAT_NET) || type.equals(FileData.Type.HEAT_VOL)
163               || type.equals(FileData.Type.HEAT)) {
164         validateIfFileHasYamlExtenstion(globalValidationContext,fileName);
165       } else if (type.equals(FileData.Type.HEAT_ENV)) {
166         validateIfFileHasEnvExtension(globalValidationContext,fileName);
167       }
168     }
169
170     private void validateIfEnvIsAssociatedToHeat(GlobalValidationContext globalContext,
171                                                  FileData childFileData) {
172       if (childFileData.getType() != null
173               && childFileData.getType().equals(FileData.Type.HEAT_ENV)) {
174         globalContext.addMessage(childFileData.getFile(), ErrorLevel.ERROR,
175                 ErrorMessagesFormatBuilder
176                         .getErrorWithParameters(ERROR_CODE_MNF_1,
177                                 Messages.ENV_NOT_ASSOCIATED_TO_HEAT.getErrorMessage()),
178                 LoggerTragetServiceName.SCAN_MANIFEST_STRUCTURE,
179                 "env file is not associated to HEAT file");
180       }
181     }
182
183     private void validateIfFileHasEnvExtension(GlobalValidationContext globalValidationContext,
184                                                String fileName) {
185       if (fileName != null && !fileName.endsWith(".env")) {
186         globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
187                 ErrorMessagesFormatBuilder
188                         .getErrorWithParameters(ERROR_CODE_MNF_3,
189                                 Messages.WRONG_ENV_FILE_EXTENSION.getErrorMessage(),
190                                 fileName), LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME,
191                 "Wrong env file extention");
192       }
193     }
194
195     private void validateIfFileHasYamlExtenstion(GlobalValidationContext globalValidationContext,
196                                                  String fileName) {
197       if (fileName != null && !fileName.endsWith(".yml") && !fileName.endsWith(".yaml")) {
198         globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
199                 ErrorMessagesFormatBuilder
200                         .getErrorWithParameters(ERROR_CODE_MNF_2,
201                                 Messages.WRONG_HEAT_FILE_EXTENSION.getErrorMessage(),
202                                 fileName), LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME,
203                 "Wrong HEAT file extention");
204       }
205     }
206
207     private void validateIfFileExists(GlobalValidationContext globalValidationContext,
208                                       String fileName) {
209       if (fileName == null) {
210         globalValidationContext.addMessage(SdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
211                 ErrorMessagesFormatBuilder
212                         .getErrorWithParameters(ERROR_CODE_MNF_7,
213                                 Messages.MISSING_FILE_NAME_IN_MANIFEST.getErrorMessage()),
214                 LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME,
215                 "Missing file name in manifest");
216
217       }
218     }
219
220   }
221   
222 }