2 * Copyright © 2017-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.heat.services.tree;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.Objects;
24 import java.util.regex.Pattern;
26 import org.openecomp.core.utilities.file.FileContentHandler;
27 import org.openecomp.sdc.common.utils.SdcCommon;
28 import org.openecomp.sdc.datatypes.error.ErrorMessage;
29 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
32 * The type Tosca tree manager.
34 public class ToscaTreeManager {
36 private FileContentHandler csarContentMap = new FileContentHandler();
37 private HeatStructureTree tree = new HeatStructureTree();
38 private Map<String, HeatStructureTree> fileTreeRef = new HashMap<>();
44 * @param fileName the file name
45 * @param content the content
47 public void addFile(String fileName, byte[] content) {
48 if (!fileName.equals(SdcCommon.CSAR_MANIFEST_NAME)) {
49 csarContentMap.addFile(fileName, content);
56 public void createTree() {
57 for (Map.Entry<String, byte[]> fileEntry : csarContentMap.getFiles().entrySet()) {
58 String[] splitFilename = getFullFileNameAsArray(fileEntry.getKey());
59 addFileToTree(splitFilename, 0, splitFilename[0], tree);
63 private void addFileToTree(String[] splitFilename, int startIndex, String fullFileName,
64 HeatStructureTree parent) {
65 fileTreeRef.putIfAbsent(fullFileName, new HeatStructureTree());
66 HeatStructureTree heatStructureTree = fileTreeRef.get(fullFileName);
67 heatStructureTree.setFileName(splitFilename[startIndex]);
68 if (startIndex < splitFilename.length - 1) {
69 addFileToTree(splitFilename, startIndex + 1,
70 getFullFileName(fullFileName, splitFilename[startIndex + 1]), heatStructureTree);
72 parent.addHeatStructureTreeToNestedHeatList(heatStructureTree);
78 * @param validationErrors the validation errors
80 public void addErrors(Map<String, List<ErrorMessage>> validationErrors) {
81 validationErrors.entrySet().stream().filter(entry ->
82 Objects.nonNull(fileTreeRef.get(entry.getKey()))).forEach(entry -> entry.getValue()
83 .forEach(error -> fileTreeRef.get(entry.getKey()).addErrorToErrorsList(error)));
86 private String getFullFileName(String parentFullName, String fileName) {
87 return parentFullName + File.separator + fileName;
90 private String[] getFullFileNameAsArray(String filename) {
91 if (filename.contains("/")) {
92 return filename.split("/");
95 return filename.split(Pattern.quote(File.separator));
103 public HeatStructureTree getTree() {