Centralize onboarding package validation
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-core / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / services / impl / filedatastructuremodule / CandidateServiceImpl.java
1 /*
2  * Copyright © 2016-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.vendorsoftwareproduct.services.impl.filedatastructuremodule;
18
19 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.nio.ByteBuffer;
26 import java.nio.charset.StandardCharsets;
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Objects;
32 import java.util.Optional;
33 import java.util.Set;
34 import java.util.stream.Collectors;
35 import java.util.zip.ZipEntry;
36 import java.util.zip.ZipInputStream;
37 import java.util.zip.ZipOutputStream;
38 import org.apache.commons.collections4.CollectionUtils;
39 import org.openecomp.core.utilities.file.FileContentHandler;
40 import org.openecomp.core.utilities.json.JsonUtil;
41 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
42 import org.openecomp.sdc.common.errors.CoreException;
43 import org.openecomp.sdc.common.errors.ErrorCategory;
44 import org.openecomp.sdc.common.errors.ErrorCode;
45 import org.openecomp.sdc.common.errors.Messages;
46 import org.openecomp.sdc.common.utils.SdcCommon;
47 import org.openecomp.sdc.common.zip.ZipUtils;
48 import org.openecomp.sdc.common.zip.exception.ZipSlipException;
49 import org.openecomp.sdc.datatypes.error.ErrorLevel;
50 import org.openecomp.sdc.datatypes.error.ErrorMessage;
51 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
52 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
53 import org.openecomp.sdc.heat.datatypes.structure.Artifact;
54 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
55 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
56 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
57 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDaoFactory;
58 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
59 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
60 import org.openecomp.sdc.vendorsoftwareproduct.errors.utils.ErrorsUtil;
61 import org.openecomp.sdc.vendorsoftwareproduct.services.HeatFileAnalyzer;
62 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
63 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.ManifestCreator;
64 import org.openecomp.sdc.vendorsoftwareproduct.services.utils.CandidateServiceValidator;
65 import org.openecomp.sdc.vendorsoftwareproduct.types.CandidateDataEntityTo;
66 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.AnalyzedZipHeatFiles;
67 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
68 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
69 import org.openecomp.sdc.versioning.dao.types.Version;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
72
73 public class CandidateServiceImpl implements CandidateService {
74   private static final Logger logger = LoggerFactory.getLogger(CandidateServiceImpl.class);
75   private CandidateServiceValidator candidateServiceValidator = new CandidateServiceValidator();
76   private ManifestCreator manifestCreator;
77   private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao;
78
79   public CandidateServiceImpl(ManifestCreator manifestCreator,
80                               OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao) {
81     this.manifestCreator = manifestCreator;
82     this.orchestrationTemplateCandidateDao = orchestrationTemplateCandidateDao;
83   }
84
85   public CandidateServiceImpl() {
86   }
87
88   @Override
89   public Optional<ErrorMessage> validateNonEmptyFileToUpload(InputStream fileToUpload,
90                                                              String fileSuffix) {
91     String errorMessage =
92         getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(),
93             fileSuffix);
94
95     if (Objects.isNull(fileToUpload)) {
96       return Optional.of(new ErrorMessage(ErrorLevel.ERROR,
97           errorMessage));
98     } else {
99       try {
100         int available = fileToUpload.available();
101         if (available == 0) {
102           return Optional.of(new ErrorMessage(ErrorLevel.ERROR,
103               errorMessage));
104         }
105       } catch (IOException e) {
106         logger.debug(e.getMessage(), e);
107         return Optional.of(new ErrorMessage(ErrorLevel.ERROR,
108             errorMessage));
109       }
110     }
111     return Optional.empty();
112   }
113
114   @Override
115   public Optional<ErrorMessage> validateRawZipData(String fileSuffix,
116                                                    byte[] uploadedFileData) {
117     if (Objects.isNull(uploadedFileData)) {
118       return Optional.of(new ErrorMessage(ErrorLevel.ERROR,
119           getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(),
120               fileSuffix)));
121     }
122     return Optional.empty();
123   }
124
125   private String heatStructureTreeToFileDataStructure(HeatStructureTree tree,
126                                                       FileContentHandler zipContentMap,
127                                                       Map<String, List<ErrorMessage>> uploadErrors,
128                                                       AnalyzedZipHeatFiles analyzedZipHeatFiles) {
129     FilesDataStructure structure = new FilesDataStructure();
130     Set<String> usedEnvFiles = new HashSet<>();
131     addHeatsToFileDataStructure(tree, usedEnvFiles, structure, uploadErrors,
132         analyzedZipHeatFiles);
133     handleOtherResources(tree, usedEnvFiles, structure);
134     FilesDataStructure fileDataStructureFromManifest =
135         createFileDataStructureFromManifest(zipContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME));
136     List<String> structureArtifacts = structure.getArtifacts();
137     structureArtifacts.addAll(fileDataStructureFromManifest.getArtifacts().stream().filter
138         (artifact -> isNotStrctureArtifact(structureArtifacts, artifact))
139         .collect(Collectors.toList()));
140     handleArtifactsFromTree(tree, structure);
141
142     return JsonUtil.object2Json(structure);
143   }
144
145   private boolean isNotStrctureArtifact(List<String> structureArtifacts, String artifact) {
146     return !structureArtifacts.contains(artifact);
147   }
148
149   @Override
150   public OrchestrationTemplateCandidateData createCandidateDataEntity(
151       CandidateDataEntityTo candidateDataEntityTo, InputStream zipFileManifest,
152       AnalyzedZipHeatFiles analyzedZipHeatFiles) {
153     FileContentHandler zipContentMap = candidateDataEntityTo.getContentMap();
154     FilesDataStructure filesDataStructure;
155     String dataStructureJson;
156
157     if (zipFileManifest != null) {
158       // create data structure from manifest
159       filesDataStructure = createFileDataStructureFromManifest(zipFileManifest);
160       Set<String> zipFileList = zipContentMap.getFileList();
161       balanceManifestFilesWithZipFiles(filesDataStructure,
162           zipContentMap, analyzedZipHeatFiles);
163       Set<String> filesDataStructureFiles = getFlatFileNames(filesDataStructure);
164       filesDataStructure.getUnassigned().addAll(zipFileList.stream()
165           .filter(fileName -> (!filesDataStructureFiles.contains(fileName)
166               && !filesDataStructure.getNested().contains(fileName)
167               && !fileName.equals(SdcCommon.MANIFEST_NAME)))
168           .collect(Collectors.toList()));
169       dataStructureJson = JsonUtil.object2Json(filesDataStructure);
170     } else {
171       // create data structure from based on naming convention
172       dataStructureJson =
173           heatStructureTreeToFileDataStructure(candidateDataEntityTo.getTree(), zipContentMap,
174               candidateDataEntityTo.getErrors(), analyzedZipHeatFiles);
175     }
176
177     OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData();
178     candidateData.setContentData(ByteBuffer.wrap(candidateDataEntityTo.getUploadedFileData()));
179     candidateData.setFilesDataStructure(dataStructureJson);
180     return candidateData;
181   }
182
183   private void balanceManifestFilesWithZipFiles(
184       FilesDataStructure filesDataStructure,
185       FileContentHandler fileContentHandler, AnalyzedZipHeatFiles analyzedZipHeatFiles) {
186     Set<String> zipFileList = fileContentHandler.getFileList();
187     filesDataStructure.getNested().addAll(analyzedZipHeatFiles.getNestedFiles());
188     List<Module> modules = filesDataStructure.getModules();
189     if (CollectionUtils.isEmpty(modules)) {
190       return;
191     }
192
193     for (int i = 0; i < modules.size(); i++) {
194       Module module = modules.get(i);
195       if (!isFileExistInZipContains(zipFileList, module.getYaml())) {
196         addFileToUnassigned(filesDataStructure, zipFileList, module.getEnv());
197         addFileToUnassigned(filesDataStructure, zipFileList, module.getVol());
198         addFileToUnassigned(filesDataStructure, zipFileList, module.getVolEnv());
199         modules.remove(i--);
200       } else if (Objects.nonNull(module.getVol()) && !zipFileList.contains(module.getVol())) {
201         module.setVol(null);
202         CollectionUtils
203             .addIgnoreNull(filesDataStructure.getUnassigned(), module.getVolEnv());
204       } else {
205         if (filesDataStructure.getNested().contains(module.getYaml())) {
206           moveModuleFileToNested(filesDataStructure, i--, module);
207         }
208       }
209     }
210   }
211
212   private void addFileToUnassigned(FilesDataStructure filesDataStructure, Set<String> zipFileList,
213                                    String fileName) {
214     if (isFileExistInZipContains(zipFileList, fileName)) {
215       filesDataStructure.getUnassigned().add(fileName);
216     }
217   }
218
219   private boolean isFileExistInZipContains(Set<String> zipFileList, String fileName) {
220     return Objects.nonNull(fileName) && zipFileList.contains(fileName);
221   }
222
223   private void moveModuleFileToNested(FilesDataStructure filesDataStructure, int i,
224                                       Module module) {
225     if (!filesDataStructure.getNested().contains(module.getYaml())) {
226       filesDataStructure.getNested().add(module.getYaml());
227     }
228     if (Objects.nonNull(module.getEnv())) {
229       filesDataStructure.getNested().add(module.getEnv());
230     }
231     if (Objects.nonNull(module.getVol())) {
232       filesDataStructure.getNested().add(module.getVol());
233     }
234     if (Objects.nonNull(module.getVolEnv())) {
235       filesDataStructure.getNested().add(module.getVolEnv());
236     }
237     filesDataStructure.getModules().remove(i);
238   }
239
240   private Set<String> getFlatFileNames(FilesDataStructure filesDataStructure) {
241     Set<String> fileNames = new HashSet<>();
242     if (!CollectionUtils.isEmpty(filesDataStructure.getModules())) {
243       for (Module module : filesDataStructure.getModules()) {
244         CollectionUtils.addIgnoreNull(fileNames, module.getEnv());
245         CollectionUtils.addIgnoreNull(fileNames, module.getVol());
246         CollectionUtils.addIgnoreNull(fileNames, module.getVolEnv());
247         CollectionUtils.addIgnoreNull(fileNames, module.getYaml());
248       }
249     }
250     fileNames.addAll(filesDataStructure.getArtifacts());
251     fileNames.addAll(filesDataStructure.getNested());
252     fileNames.addAll(filesDataStructure.getUnassigned());
253
254     return fileNames;
255   }
256
257   private FilesDataStructure createFileDataStructureFromManifest(InputStream isManifestContent) {
258     ManifestContent manifestContent =
259         JsonUtil.json2Object(isManifestContent, ManifestContent.class);
260     FilesDataStructure structure = new FilesDataStructure();
261     for (FileData fileData : manifestContent.getData()) {
262       if (Objects.nonNull(fileData.getType()) &&
263           fileData.getType().equals(FileData.Type.HEAT)) {
264         Module module = new Module();
265         module.setYaml(fileData.getFile());
266         module.setIsBase(fileData.getBase());
267         addHeatDependenciesToModule(module, fileData.getData());
268         structure.getModules().add(module);
269       } else if (HeatFileAnalyzer.isYamlOrEnvFile(fileData.getFile()) &&
270           !FileData.Type.isArtifact(fileData.getType())) {
271         structure.getUnassigned().add(fileData.getFile());
272       } else {
273         structure.getArtifacts().add(fileData.getFile());
274       }
275     }
276     return structure;
277   }
278
279   private void addHeatDependenciesToModule(Module module, List<FileData> data) {
280     if (CollectionUtils.isEmpty(data)) {
281       return;
282     }
283
284     for (FileData fileData : data) {
285       if (fileData.getType().equals(FileData.Type.HEAT_ENV)) {
286         module.setEnv(fileData.getFile());
287       } else if (fileData.getType().equals(FileData.Type.HEAT_VOL)) { // must be volume
288         module.setVol(fileData.getFile());
289         if (!CollectionUtils.isEmpty(fileData.getData())) {
290           FileData volEnv = fileData.getData().get(0);
291           if (volEnv.getType().equals(FileData.Type.HEAT_ENV)) {
292             module.setVolEnv(volEnv.getFile());
293           } else {
294             throw new CoreException((new ErrorCode.ErrorCodeBuilder())
295                 .withMessage(Messages.ILLEGAL_MANIFEST.getErrorMessage())
296                 .withId(Messages.ILLEGAL_MANIFEST.getErrorMessage())
297                 .withCategory(ErrorCategory.APPLICATION).build());
298           }
299         }
300       } else {
301         throw new CoreException((new ErrorCode.ErrorCodeBuilder())
302             .withMessage(Messages.FILE_TYPE_NOT_LEGAL.getErrorMessage())
303             .withId(Messages.FILE_TYPE_NOT_LEGAL.getErrorMessage())
304             .withCategory(ErrorCategory.APPLICATION).build());
305       }
306     }
307   }
308
309   @Override
310   public void updateCandidateUploadData(final String vspId, final Version version,
311                                         final OrchestrationTemplateCandidateData uploadData) {
312     orchestrationTemplateCandidateDao.update(vspId, version, uploadData);
313   }
314
315   @Override
316   public Optional<FilesDataStructure> getOrchestrationTemplateCandidateFileDataStructure(
317       String vspId, Version version) {
318     Optional<String> jsonFileDataStructure =
319         orchestrationTemplateCandidateDao.getStructure(vspId, version);
320
321     if (jsonFileDataStructure.isPresent() && JsonUtil.isValidJson(jsonFileDataStructure.get())) {
322       return Optional
323           .of(JsonUtil.json2Object(jsonFileDataStructure.get(), FilesDataStructure.class));
324     } else {
325       return Optional.empty();
326     }
327   }
328
329   @Override
330   public void updateOrchestrationTemplateCandidateFileDataStructure(String vspId, Version version,
331                                                                     FilesDataStructure fileDataStructure) {
332     OrchestrationTemplateCandidateDaoFactory.getInstance().createInterface()
333         .updateStructure(vspId, version, fileDataStructure);
334   }
335
336   @Override
337   public Optional<OrchestrationTemplateCandidateData> getOrchestrationTemplateCandidate(String vspId,
338                                                                                         Version version) {
339     return orchestrationTemplateCandidateDao.get(vspId, version);
340   }
341
342   @Override
343   public Optional<OrchestrationTemplateCandidateData> getOrchestrationTemplateCandidateInfo(
344       String vspId,
345       Version version) {
346     return orchestrationTemplateCandidateDao.getInfo(vspId, version);
347   }
348
349   @Override
350   public String createManifest(VspDetails vspDetails, FilesDataStructure structure) {
351     return JsonUtil.object2Json(manifestCreator.createManifest(vspDetails, structure)
352         .orElseThrow(() -> new CoreException(new ErrorCode.ErrorCodeBuilder()
353             .withMessage(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage()).build())));
354   }
355
356   @Override
357   public Optional<ManifestContent> createManifest(VspDetails vspDetails,
358                                                   FileContentHandler fileContentHandler,
359                                                   AnalyzedZipHeatFiles analyzedZipHeatFiles) {
360     return manifestCreator.createManifest(vspDetails, fileContentHandler, analyzedZipHeatFiles);
361   }
362
363   @Override
364   public Optional<ByteArrayInputStream> fetchZipFileByteArrayInputStream(String vspId,
365                                                                          OrchestrationTemplateCandidateData candidateDataEntity,
366                                                                          String manifest,
367                                                                          OnboardingTypesEnum type,
368                                                                          Map<String, List<ErrorMessage>> uploadErrors) {
369     byte[] file;
370     ByteArrayInputStream byteArrayInputStream = null;
371     try {
372       file = replaceManifestInZip(candidateDataEntity.getContentData(), manifest, type);
373       byteArrayInputStream = new ByteArrayInputStream(
374           Objects.isNull(file) ? candidateDataEntity.getContentData().array()
375               : file);
376     } catch (IOException e) {
377       ErrorMessage errorMessage =
378           new ErrorMessage(ErrorLevel.ERROR,
379               Messages.CANDIDATE_PROCESS_FAILED.getErrorMessage());
380       logger.error(errorMessage.getMessage(), e);
381       ErrorsUtil
382           .addStructureErrorToErrorMap(SdcCommon.UPLOAD_FILE, errorMessage, uploadErrors);
383     }
384     return Optional.ofNullable(byteArrayInputStream);
385   }
386
387   @Override
388   public byte[] replaceManifestInZip(ByteBuffer contentData, String manifest,
389                                      OnboardingTypesEnum type)
390       throws IOException {
391     ByteArrayOutputStream baos = new ByteArrayOutputStream();
392
393     try (final ZipOutputStream zos = new ZipOutputStream(baos);
394          ZipInputStream zipStream = new ZipInputStream(
395              new ByteArrayInputStream(contentData.array()))) {
396       ZipEntry zipEntry;
397       boolean manifestWritten = false;
398       while ((zipEntry = zipStream.getNextEntry()) != null) {
399         if (!zipEntry.getName().equalsIgnoreCase(SdcCommon.MANIFEST_NAME)) {
400           ZipEntry loc_ze = new ZipEntry(zipEntry.getName());
401           zos.putNextEntry(loc_ze);
402           byte[] buf = new byte[1024];
403           int len;
404           while ((len = zipStream.read(buf)) > 0) {
405             zos.write(buf, 0, (len < buf.length) ? len : buf.length);
406           }
407         } else {
408           manifestWritten = true;
409           writeManifest(manifest, type, zos);
410         }
411         zos.closeEntry();
412       }
413       if (!manifestWritten) {
414         writeManifest(manifest, type, zos);
415         zos.closeEntry();
416       }
417     }
418     return baos.toByteArray();
419   }
420
421   @Override
422   public byte[] getZipData(ByteBuffer contentData)
423       throws IOException {
424     ByteArrayOutputStream baos = new ByteArrayOutputStream();
425
426     try (final ZipOutputStream zos = new ZipOutputStream(baos);
427          ZipInputStream zipStream = new ZipInputStream(
428              new ByteArrayInputStream(contentData.array()))) {
429       ZipEntry zipEntry;
430       while ((zipEntry = zipStream.getNextEntry()) != null) {
431         try {
432           ZipUtils.checkForZipSlipInRead(zipEntry);
433         } catch (ZipSlipException e) {
434           throw new IOException(e);
435         }
436         ZipEntry locZipEntry = new ZipEntry(zipEntry.getName());
437         zos.putNextEntry(locZipEntry);
438         byte[] buf = new byte[1024];
439         int len;
440         while ((len = zipStream.read(buf)) > 0) {
441           zos.write(buf, 0, (len < buf.length) ? len : buf.length);
442         }
443         zos.closeEntry();
444       }
445     }
446     return baos.toByteArray();
447   }
448
449   @Override
450   public Optional<List<ErrorMessage>> validateFileDataStructure(
451       FilesDataStructure filesDataStructure) {
452     return candidateServiceValidator.validateFileDataStructure(filesDataStructure);
453   }
454
455   @Override
456   public void deleteOrchestrationTemplateCandidate(String vspId, Version versionId) {
457     orchestrationTemplateCandidateDao.delete(vspId, versionId);
458   }
459
460   @Override
461   public void updateValidationData(String vspId, Version version, ValidationStructureList
462       validationData) {
463     orchestrationTemplateCandidateDao.updateValidationData(vspId, version, validationData);
464   }
465
466   private void writeManifest(String manifest,
467                              OnboardingTypesEnum type,
468                              ZipOutputStream zos) throws IOException {
469
470     if (isManifestNeedsToGetWritten(type)) {
471       return;
472     }
473
474     zos.putNextEntry(new ZipEntry(SdcCommon.MANIFEST_NAME));
475     try (InputStream manifestStream = new ByteArrayInputStream(
476         manifest.getBytes(StandardCharsets.UTF_8))) {
477       byte[] buf = new byte[1024];
478       int len;
479       while ((len = (manifestStream.read(buf))) > 0) {
480         zos.write(buf, 0, (len < buf.length) ? len : buf.length);
481       }
482     }
483   }
484
485   private boolean isManifestNeedsToGetWritten(OnboardingTypesEnum type) {
486     return type.equals(OnboardingTypesEnum.CSAR);
487   }
488
489   private void handleArtifactsFromTree(HeatStructureTree tree, FilesDataStructure structure) {
490
491     if (Objects.isNull(tree) || Objects.isNull(tree.getArtifacts())) {
492       return;
493     }
494
495     if (CollectionUtils.isNotEmpty(tree.getArtifacts())) {
496       structure.getArtifacts().addAll(
497           tree.getArtifacts()
498               .stream()
499               .map(Artifact::getFileName)
500               .filter(fileName -> !structure.getArtifacts().contains(fileName))
501               .collect(Collectors.toList()));
502     }
503   }
504
505   private void handleOtherResources(HeatStructureTree tree, Set<String> usedEnvFiles,
506                                     FilesDataStructure structure) {
507     Set<HeatStructureTree> others = tree.getOther();
508     if (Objects.isNull(others)) {
509       return;
510     }
511
512     List<String> artifacts = new ArrayList<>();
513     List<String> unassigned = new ArrayList<>();
514     for (HeatStructureTree other : others) {
515       if (HeatFileAnalyzer.isYamlOrEnvFile(other.getFileName())) {
516         if (isEnvFileUsedByHeatFile(usedEnvFiles, other)) {
517           continue;
518         }
519         unassigned.add(other.getFileName());
520       } else {
521         artifacts.add(other.getFileName());
522       }
523       handleArtifactsFromTree(other, structure);
524     }
525     structure.getArtifacts().addAll(artifacts);
526     structure.getUnassigned().addAll(unassigned);
527   }
528
529   private boolean isEnvFileUsedByHeatFile(Set<String> usedEnvFiles, HeatStructureTree other) {
530     return HeatFileAnalyzer.isEnvFile(other.getFileName()) &&
531         usedEnvFiles.contains(other.getFileName());
532   }
533
534   private void addHeatsToFileDataStructure(HeatStructureTree tree, Set<String> usedEnvFiles,
535                                            FilesDataStructure structure,
536                                            Map<String, List<ErrorMessage>> uploadErrors,
537                                            AnalyzedZipHeatFiles analyzedZipHeatFiles) {
538     List<Module> modules = new ArrayList<>();
539     Set<HeatStructureTree> heatsSet = tree.getHeat();
540     if (Objects.isNull(heatsSet)) {
541       return;
542     }
543     for (HeatStructureTree heat : heatsSet) {
544       if (isFileBaseFile(heat.getFileName())) {
545         handleSingleHeat(structure, modules, heat, uploadErrors);
546       } else if (isFileModuleFile(heat.getFileName(),
547           analyzedZipHeatFiles.getModuleFiles())) {
548         handleSingleHeat(structure, modules, heat, uploadErrors);
549       } else {
550         structure.getUnassigned().add(heat.getFileName());
551         addNestedToFileDataStructure(heat, structure);
552       }
553       if (!Objects.isNull(heat.getEnv())) {
554         usedEnvFiles.add(heat.getEnv() == null ? null : heat.getEnv().getFileName());
555       }
556     }
557     structure.setModules(modules);
558
559   }
560
561   private boolean isFileModuleFile(String fileName, Set<String> modulesFileNames) {
562     return modulesFileNames.contains(fileName);
563   }
564
565   private boolean isFileBaseFile(String fileName) {
566     return manifestCreator.isFileBaseFile(fileName);
567   }
568
569   private void handleSingleHeat(FilesDataStructure structure, List<Module> modules,
570                                 HeatStructureTree heat,
571                                 Map<String, List<ErrorMessage>> uploadErrors) {
572     Module module = new Module();
573     module.setYaml(heat.getFileName());
574     module.setIsBase(heat.getBase());
575     addNestedToFileDataStructure(heat, structure);
576     Set<HeatStructureTree> volumeSet = heat.getVolume();
577     int inx = 0;
578     if (Objects.nonNull(volumeSet)) {
579       handleVolumes(module, volumeSet, structure, inx, uploadErrors);
580     }
581     handleEnv(module, heat, false, structure);
582     modules.add(module);
583   }
584
585   private void handleVolumes(Module module, Set<HeatStructureTree> volumeSet,
586                              FilesDataStructure structure, int inx,
587                              Map<String, List<ErrorMessage>> uploadErrors) {
588     for (HeatStructureTree volume : volumeSet) {
589       Objects.requireNonNull(volume, "volume cannot be null!");
590       if (inx++ > 0) {
591         ErrorsUtil.addStructureErrorToErrorMap(SdcCommon.UPLOAD_FILE,
592             new ErrorMessage(ErrorLevel.WARNING,
593                 Messages.MORE_THEN_ONE_VOL_FOR_HEAT.getErrorMessage()), uploadErrors);
594         break;
595       }
596       handleArtifactsFromTree(volume, structure);
597       module.setVol(volume.getFileName());
598       handleEnv(module, volume, true, structure);
599       addNestedToFileDataStructure(volume, structure);
600     }
601   }
602
603   private void handleEnv(Module module, HeatStructureTree tree, boolean isVolEnv,
604                          FilesDataStructure structure) {
605     if (Objects.nonNull(tree.getEnv())) {
606       if (isVolEnv) {
607         module.setVolEnv(tree.getEnv().getFileName());
608       } else {
609         module.setEnv(tree.getEnv().getFileName());
610       }
611       handleArtifactsFromTree(tree.getEnv(), structure);
612     }
613   }
614
615   private void addNestedToFileDataStructure(HeatStructureTree heat,
616                                             FilesDataStructure structure) {
617     Set<HeatStructureTree> nestedSet = heat.getNested();
618     if (Objects.isNull(nestedSet)) {
619       return;
620     }
621     for (HeatStructureTree nested : nestedSet) {
622       if (structure.getNested().contains(nested.getFileName())) {
623         continue;
624       }
625       structure.getNested().add(nested.getFileName());
626       if (CollectionUtils.isNotEmpty(nested.getArtifacts())) {
627         handleArtifactsFromTree(nested, structure);
628       }
629       addNestedToFileDataStructure(nested, structure);
630     }
631   }
632 }