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