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