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