944ab62f9f578956f9d83ae292ff6c3c98dc06a6
[sdc.git] /
1 /*
2  * Copyright © 2017-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.heat.services.tree;
18
19 import java.io.File;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Objects;
24 import java.util.regex.Pattern;
25
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;
30
31 /**
32  * The type Tosca tree manager.
33  */
34 public class ToscaTreeManager {
35
36     private FileContentHandler csarContentMap = new FileContentHandler();
37     private HeatStructureTree tree = new HeatStructureTree();
38     private Map<String, HeatStructureTree> fileTreeRef = new HashMap<>();
39
40
41     /**
42      * Add file.
43      *
44      * @param fileName the file name
45      * @param content  the content
46      */
47     public void addFile(String fileName, byte[] content) {
48         if (!fileName.equals(SdcCommon.CSAR_MANIFEST_NAME)) {
49             csarContentMap.addFile(fileName, content);
50         }
51     }
52
53     /**
54      * Create tree.
55      */
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);
60         }
61     }
62
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);
71         }
72         parent.addHeatStructureTreeToNestedHeatList(heatStructureTree);
73     }
74
75     /**
76      * Add errors.
77      *
78      * @param validationErrors the validation errors
79      */
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)));
84     }
85
86     private String getFullFileName(String parentFullName, String fileName) {
87         return parentFullName + File.separator + fileName;
88     }
89
90     private String[] getFullFileNameAsArray(String filename) {
91         if (filename.contains("/")) {
92             return filename.split("/");
93         }
94
95         return filename.split(Pattern.quote(File.separator));
96     }
97
98     /**
99      * Gets tree.
100      *
101      * @return the tree
102      */
103     public HeatStructureTree getTree() {
104         return tree;
105     }
106 }