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