1 package org.openecomp.sdc.heat.services.tree;
3 import org.openecomp.core.utilities.file.FileContentHandler;
4 import org.openecomp.sdc.common.utils.SdcCommon;
5 import org.openecomp.sdc.datatypes.error.ErrorMessage;
6 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
9 import java.util.HashMap;
10 import java.util.List;
12 import java.util.Objects;
13 import java.util.regex.Pattern;
15 public class ToscaTreeManager {
16 private FileContentHandler csarContentMap = new FileContentHandler();
17 private HeatStructureTree tree = new HeatStructureTree();
18 private Map<String, HeatStructureTree> fileTreeRef = new HashMap<>();
21 public void addFile(String fileName, byte[] content) {
22 if (!fileName.equals(SdcCommon.CSAR_MANIFEST_NAME)) {
23 csarContentMap.addFile(fileName, content);
27 public void createTree() {
28 for (Map.Entry<String, byte[]> fileEntry : csarContentMap.getFiles().entrySet()) {
29 String[] splitFilename = getFullFileNameAsArray(fileEntry.getKey());
30 addFileToTree(splitFilename, 0, splitFilename[0], tree);
34 private void addFileToTree(String[] splitFilename, int startIndex, String fullFileName,
35 HeatStructureTree parent) {
36 fileTreeRef.putIfAbsent(fullFileName, new HeatStructureTree());
37 HeatStructureTree heatStructureTree = fileTreeRef.get(fullFileName);
38 heatStructureTree.setFileName(splitFilename[startIndex]);
39 if (startIndex < splitFilename.length - 1) {
40 addFileToTree(splitFilename, startIndex + 1,
41 getFullFileName(fullFileName, splitFilename[startIndex + 1]), heatStructureTree);
43 parent.addHeatStructureTreeToNestedHeatList(heatStructureTree);
46 public void addErrors(Map<String, List<ErrorMessage>> validationErrors) {
47 validationErrors.entrySet().stream().filter(entry ->
48 Objects.nonNull(fileTreeRef.get(entry.getKey()))).forEach(entry -> entry.getValue()
49 .forEach(error -> fileTreeRef.get(entry.getKey()).addErrorToErrorsList(error)));
52 private String getFullFileName(String parentFullName, String fileName) {
53 return parentFullName + File.separator + fileName;
56 private String[] getFullFileNameAsArray(String filename) {
57 if (filename.contains("/")) {
58 return filename.split("/");
61 return filename.split(Pattern.quote(File.separator));
64 public HeatStructureTree getTree() {