Add collaboration feature
[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  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.validation.impl.validators;
22
23 import org.openecomp.core.utilities.json.JsonUtil;
24 import org.openecomp.core.validation.ErrorMessageCode;
25 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
26 import org.openecomp.core.validation.types.GlobalValidationContext;
27 import org.openecomp.sdc.common.errors.Messages;
28 import org.openecomp.sdc.common.utils.SdcCommon;
29 import org.openecomp.sdc.datatypes.error.ErrorLevel;
30 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
31 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
35 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
36 import org.openecomp.sdc.logging.types.LoggerConstants;
37 import org.openecomp.sdc.logging.types.LoggerErrorCode;
38 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
39 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
40 import org.openecomp.sdc.validation.Validator;
41
42 import java.io.InputStream;
43 import java.util.ArrayList;
44 import java.util.List;
45 import java.util.Optional;
46
47
48 public class ManifestValidator implements Validator {
49   public static final MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
50   private static Logger logger = (Logger) LoggerFactory.getLogger(YamlValidator.class);
51   private static final ErrorMessageCode ERROR_CODE_MNF_1 = new ErrorMessageCode("MNF1");
52   private static final ErrorMessageCode ERROR_CODE_MNF_2 = new ErrorMessageCode("MNF2");
53   private static final ErrorMessageCode ERROR_CODE_MNF_3 = new ErrorMessageCode("MNF3");
54   private static final ErrorMessageCode ERROR_CODE_MNF_4 = new ErrorMessageCode("MNF4");
55   private static final ErrorMessageCode ERROR_CODE_MNF_5 = new ErrorMessageCode("MNF5");
56   private static final ErrorMessageCode ERROR_CODE_MNF_6 = new ErrorMessageCode("MNF6");
57   private static final ErrorMessageCode ERROR_CODE_MNF_7 = new ErrorMessageCode("MNF7");
58   private static final ErrorMessageCode ERROR_CODE_MNF_8 = new ErrorMessageCode("MNF8");
59
60   @Override
61   public void validate(GlobalValidationContext globalContext) {
62     mdcDataDebugMessage.debugEntryMessage(null, null);
63
64     Optional<InputStream> content = globalContext.getFileContent(SdcCommon.MANIFEST_NAME);
65     ManifestContent manifestContent;
66
67     try {
68       if (content.isPresent()) {
69         manifestContent = JsonUtil.json2Object(content.get(), ManifestContent.class);
70       } else {
71         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
72             LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT, ErrorLevel.ERROR.name(),
73             LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.EMPTY_FILE);
74         throw new Exception("The manifest file '" + SdcCommon.MANIFEST_NAME + "' has no content");
75       }
76     } catch (Exception re) {
77       logger.debug("",re);
78       globalContext.addMessage(SdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
79               ErrorMessagesFormatBuilder
80                       .getErrorWithParameters(ERROR_CODE_MNF_6,
81                               Messages.INVALID_MANIFEST_FILE.getErrorMessage()),
82           LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT,
83           LoggerErrorDescription.INVALID_MANIFEST);
84       return;
85     }
86
87     List<String> manifestFiles = getManifestFileList(manifestContent, globalContext);
88     manifestFiles.stream().filter(name ->
89         !globalContext.getFileContextMap().containsKey(name)
90     ).forEach(name -> globalContext
91         .addMessage(name, ErrorLevel.ERROR,ErrorMessagesFormatBuilder
92                 .getErrorWithParameters(ERROR_CODE_MNF_4,
93                         Messages.MISSING_FILE_IN_ZIP.getErrorMessage()),
94             LoggerTragetServiceName.VALIDATE_FILE_IN_ZIP, LoggerErrorDescription.MISSING_FILE));
95
96     globalContext.getFileContextMap().keySet().stream().filter(name ->
97         isNotManifestFiles(manifestFiles, name) && isNotManifestName(name)
98     ).forEach(name ->
99         globalContext.addMessage(name, ErrorLevel.WARNING,
100                 ErrorMessagesFormatBuilder
101                         .getErrorWithParameters(ERROR_CODE_MNF_5,
102                                 Messages.MISSING_FILE_IN_MANIFEST.getErrorMessage()),
103             LoggerTragetServiceName.VALIDATE_FILE_IN_MANIFEST, LoggerErrorDescription.MISSING_FILE)
104     );
105
106     mdcDataDebugMessage.debugExitMessage(null, null);
107   }
108
109   private boolean isNotManifestFiles(List<String> manifestFiles, String name) {
110     return !manifestFiles.contains(name);
111   }
112
113   private boolean isNotManifestName(String name) {
114     return !SdcCommon.MANIFEST_NAME.equals(name);
115   }
116
117
118   private List<String> getManifestFileList(ManifestContent manifestContent,
119                                            GlobalValidationContext context) {
120
121
122     mdcDataDebugMessage.debugEntryMessage(null, null);
123
124     ManifestScanner manifestScanner = new ManifestScanner();
125     manifestScanner.scan(null, manifestContent.getData(), context);
126
127     mdcDataDebugMessage.debugExitMessage(null, null);
128     return manifestScanner.getFileList();
129   }
130
131
132   private class ManifestScanner {
133     private List<String> fileList=new ArrayList<>();
134
135     public void scan(FileData fileData, List<FileData> data,
136                      GlobalValidationContext globalContext) {
137       if (fileData == null) {
138         for (FileData childFileData : data) {
139           validateIfEnvIsAssociatedToHeat(globalContext, childFileData);
140         }
141       }
142       if (fileData != null) {
143         fileList.add(fileData.getFile());
144         validateFileTypeVsFileName(globalContext,fileData);
145       }
146       if (data == null) {
147         return;
148       }
149       data.forEach(chileFileData -> scan(chileFileData, chileFileData.getData(), globalContext));
150     }
151
152     public List<String> getFileList() {
153       return this.fileList;
154     }
155   }
156
157   private void validateFileTypeVsFileName(GlobalValidationContext globalValidationContext,
158                                           FileData fileData) {
159     String fileName = fileData.getFile();
160     validateIfFileExists(globalValidationContext,fileName);
161     FileData.Type type = fileData.getType();
162     if (type == null) {
163       globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
164           ErrorMessagesFormatBuilder.getErrorWithParameters(ERROR_CODE_MNF_8,
165               Messages.INVALID_FILE_TYPE.getErrorMessage()),
166           LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME, "Invalid file type");
167     } else if (type.equals(FileData.Type.HEAT_NET) || type.equals(FileData.Type.HEAT_VOL)
168         || type.equals(FileData.Type.HEAT)) {
169       validateIfFileHasYamlExtenstion(globalValidationContext,fileName);
170     } else if (type.equals(FileData.Type.HEAT_ENV)) {
171       validateIfFileHasEnvExtension(globalValidationContext,fileName);
172     }
173   }
174
175   private void validateIfFileHasEnvExtension(GlobalValidationContext globalValidationContext,
176                                              String fileName) {
177     if (fileName != null && !fileName.endsWith(".env")) {
178       globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
179           ErrorMessagesFormatBuilder
180               .getErrorWithParameters(ERROR_CODE_MNF_3,
181                   Messages.WRONG_ENV_FILE_EXTENSION.getErrorMessage(),
182                   fileName), LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME,
183           "Wrong env file extention");
184     }
185   }
186
187   private void validateIfFileHasYamlExtenstion(GlobalValidationContext globalValidationContext,
188                                                String fileName) {
189     if (fileName != null && !fileName.endsWith(".yml") && !fileName.endsWith(".yaml")) {
190       globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
191           ErrorMessagesFormatBuilder
192               .getErrorWithParameters(ERROR_CODE_MNF_2,
193                   Messages.WRONG_HEAT_FILE_EXTENSION.getErrorMessage(),
194                   fileName), LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME,
195           "Wrong HEAT file extention");
196     }
197   }
198
199   private void validateIfFileExists(GlobalValidationContext globalValidationContext,
200                                     String fileName) {
201     if (fileName == null) {
202       globalValidationContext.addMessage(SdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
203           ErrorMessagesFormatBuilder
204               .getErrorWithParameters(ERROR_CODE_MNF_7,
205                   Messages.MISSING_FILE_NAME_IN_MANIFEST.getErrorMessage()),
206           LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME,
207           "Missing file name in manifest");
208
209     }
210   }
211
212   private void validateIfEnvIsAssociatedToHeat(GlobalValidationContext globalContext,
213                                                FileData childFileData) {
214     if (childFileData.getType() != null
215         && childFileData.getType().equals(FileData.Type.HEAT_ENV)) {
216       globalContext.addMessage(childFileData.getFile(), ErrorLevel.ERROR,
217           ErrorMessagesFormatBuilder
218               .getErrorWithParameters(ERROR_CODE_MNF_1,
219                       Messages.ENV_NOT_ASSOCIATED_TO_HEAT.getErrorMessage()),
220           LoggerTragetServiceName.SCAN_MANIFEST_STRUCTURE,
221           "env file is not associated to HEAT file");
222     }
223   }
224
225
226 }