2 * Copyright © 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.
16 package org.openecomp.sdc.heat.services.tree;
18 import java.io.InputStream;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
23 import java.util.Objects;
25 import org.onap.sdc.tosca.services.YamlUtil;
26 import org.openecomp.core.utilities.file.FileContentHandler;
27 import org.openecomp.core.utilities.file.FileUtils;
28 import org.openecomp.core.utilities.json.JsonUtil;
29 import org.openecomp.core.validation.types.GlobalValidationContext;
30 import org.openecomp.sdc.common.utils.SdcCommon;
31 import org.openecomp.sdc.datatypes.error.ErrorMessage;
32 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
33 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
34 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
35 import org.openecomp.sdc.heat.datatypes.structure.Artifact;
36 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
37 import org.openecomp.sdc.logging.api.Logger;
38 import org.openecomp.sdc.logging.api.LoggerFactory;
40 public class HeatTreeManager {
42 private static final Logger LOGGER = LoggerFactory.getLogger(HeatTreeManager.class);
43 private FileContentHandler heatContentMap = new FileContentHandler();
44 private byte[] manifest;
45 private HeatStructureTree tree = new HeatStructureTree();
46 private Map<String, HeatStructureTree> fileTreeRef = new HashMap<>();
47 private Map<String, Artifact> artifactRef = new HashMap<>();
48 private Map<String, Artifact> candidateOrphanArtifacts = new HashMap<>();
49 private Map<String, HeatStructureTree> nestedFiles = new HashMap<>();
50 private Map<HeatStructureTree, HeatStructureTree> volumeFileToParent = new HashMap<>();
51 private Map<HeatStructureTree, HeatStructureTree> networkFileToParent = new HashMap<>();
52 private Set<String> manifestFiles = new HashSet<>();
57 * @param fileName the file name
58 * @param content the content
60 public void addFile(String fileName, InputStream content) {
61 if (fileName.equals(SdcCommon.MANIFEST_NAME)) {
62 manifest = FileUtils.toByteArray(content);
64 heatContentMap.addFile(fileName, content);
71 public void createTree() {
72 if (manifest == null) {
73 LOGGER.error("Missing manifest file in the zip.");
76 ManifestContent manifestData = JsonUtil.json2Object(new String(manifest), ManifestContent.class);
77 scanTree(null, manifestData.getData());
78 addNonNestedVolumeNetworkToTree(volumeFileToParent, nestedFiles.keySet(), true);
79 addNonNestedVolumeNetworkToTree(networkFileToParent, nestedFiles.keySet(), false);
81 tree = fileTreeRef.get(SdcCommon.PARENT);
84 private void handleOrphans() {
85 tree = fileTreeRef.get(SdcCommon.PARENT);
86 candidateOrphanArtifacts.forEach((key, value) -> tree.addArtifactToArtifactList(value));
87 nestedFiles.values().stream().filter(tree.getHeat()::contains).forEach(tree.getHeat()::remove);
88 heatContentMap.getFileList().stream().filter(this::isNotInManifestFiles).forEach(this::addTreeOther);
91 private boolean isNotInManifestFiles(String fileName) {
92 return !manifestFiles.contains(fileName);
95 private void addTreeOther(String fileName) {
96 if (tree.getOther() == null) {
97 tree.setOther(new HashSet<>());
99 HeatStructureTree other = new HeatStructureTree(fileName, false);
100 fileTreeRef.put(fileName, other);
101 tree.getOther().add(other);
104 private void handleHeatContentReference(HeatStructureTree fileHeatStructureTree, GlobalValidationContext globalContext) {
105 String fileName = fileHeatStructureTree.getFileName();
106 try (InputStream fileContent = this.heatContentMap.getFileContentAsStream(fileName)) {
107 HeatOrchestrationTemplate hot = new YamlUtil().yamlToObject(fileContent, HeatOrchestrationTemplate.class);
108 Set<String> nestedSet = HeatTreeManagerUtil.getNestedFiles(hot);
109 addHeatNestedFiles(fileHeatStructureTree, nestedSet);
110 Set<String> artifactSet = HeatTreeManagerUtil.getArtifactFiles(fileName, hot, globalContext);
111 addHeatArtifactFiles(fileHeatStructureTree, artifactSet);
112 } catch (Exception exp) {
113 LOGGER.debug("Invalid YAML received. No need to process content reference - ignoring", exp);
117 private void addHeatArtifactFiles(HeatStructureTree fileHeatStructureTree, Set<String> artifactSet) {
119 for (String artifactName : artifactSet) {
120 FileData.Type type = candidateOrphanArtifacts.get(artifactName) != null ? candidateOrphanArtifacts.get(artifactName).getType() : null;
121 artifact = new Artifact(artifactName, type);
122 artifactRef.put(artifactName, artifact);
123 candidateOrphanArtifacts.remove(artifactName);
124 fileHeatStructureTree.addArtifactToArtifactList(artifact);
128 private void addHeatNestedFiles(HeatStructureTree fileHeatStructureTree, Set<String> nestedSet) {
129 HeatStructureTree childHeatStructureTree;
130 for (String nestedName : nestedSet) {
131 childHeatStructureTree = fileTreeRef.get(nestedName);
132 if (childHeatStructureTree == null) {
133 childHeatStructureTree = new HeatStructureTree();
134 childHeatStructureTree.setFileName(nestedName);
135 fileTreeRef.put(nestedName, childHeatStructureTree);
137 fileHeatStructureTree.addHeatStructureTreeToNestedHeatList(childHeatStructureTree);
138 nestedFiles.put(childHeatStructureTree.getFileName(), childHeatStructureTree);
145 * @param validationErrors the validation errors
147 public void addErrors(Map<String, List<ErrorMessage>> validationErrors) {
148 validationErrors.entrySet().stream().filter(entry -> fileTreeRef.get(entry.getKey()) != null)
149 .forEach(entry -> entry.getValue().forEach(fileTreeRef.get(entry.getKey())::addErrorToErrorsList));
150 validationErrors.entrySet().stream().filter(entry -> artifactRef.get(entry.getKey()) != null)
151 .forEach(entry -> artifactRef.get(entry.getKey()).setErrors(entry.getValue()));
157 * @param parent the parent
158 * @param data the data
160 public void scanTree(String parent, List<FileData> data) {
163 HeatStructureTree parentHeatStructureTree;
164 HeatStructureTree fileHeatStructureTree;
165 HeatStructureTree childHeatStructureTree;
167 if (parent == null) {
168 parentHeatStructureTree = new HeatStructureTree();
169 fileTreeRef.put(SdcCommon.PARENT, parentHeatStructureTree);
170 fileTreeRef.put(SdcCommon.MANIFEST_NAME, parentHeatStructureTree);
172 parentHeatStructureTree = fileTreeRef.get(parent);
174 for (FileData fileData : data) {
175 fileName = fileData.getFile();
176 manifestFiles.add(fileName);
177 type = fileData.getType();
178 if (Objects.nonNull(type) && FileData.Type.HEAT.equals(type)) {
179 fileHeatStructureTree = fileTreeRef.get(fileName);
180 if (fileHeatStructureTree == null) {
181 fileHeatStructureTree = new HeatStructureTree();
182 fileTreeRef.put(fileName, fileHeatStructureTree);
184 fileHeatStructureTree.setFileName(fileName);
185 fileHeatStructureTree.setBase(fileData.getBase());
186 fileHeatStructureTree.setType(type);
187 handleHeatContentReference(fileHeatStructureTree, null);
188 parentHeatStructureTree.addHeatToHeatList(fileHeatStructureTree);
189 if (fileData.getData() != null) {
190 scanTree(fileName, fileData.getData());
193 childHeatStructureTree = new HeatStructureTree();
194 childHeatStructureTree.setFileName(fileName);
195 childHeatStructureTree.setBase(fileData.getBase());
196 childHeatStructureTree.setType(type);
197 fileTreeRef.put(childHeatStructureTree.getFileName(), childHeatStructureTree);
199 parentHeatStructureTree.addOtherToOtherList(childHeatStructureTree);
200 } else if (FileData.Type.HEAT_NET.equals(type)) {
201 networkFileToParent.put(childHeatStructureTree, parentHeatStructureTree);
202 if (fileData.getData() != null) {
203 scanTree(fileName, fileData.getData());
205 handleHeatContentReference(childHeatStructureTree, null);
206 } else if (FileData.Type.HEAT_VOL.equals(type)) {
207 volumeFileToParent.put(childHeatStructureTree, parentHeatStructureTree);
208 if (fileData.getData() != null) {
209 scanTree(fileName, fileData.getData());
211 handleHeatContentReference(childHeatStructureTree, null);
212 } else if (FileData.Type.HEAT_ENV.equals(type)) {
213 if (parentHeatStructureTree != null && parentHeatStructureTree.getFileName() != null) {
214 parentHeatStructureTree.setEnv(childHeatStructureTree);
216 if (parentHeatStructureTree.getOther() == null) {
217 parentHeatStructureTree.setOther(new HashSet<>());
219 parentHeatStructureTree.getOther().add(childHeatStructureTree);
221 } else if (FileData.Type.HELM.equals(type)) {
222 parentHeatStructureTree.addToHelmList(childHeatStructureTree);
224 artifact = new Artifact(fileName, type);
225 if (!artifactRef.keySet().contains(fileName)) {
226 artifactRef.put(fileName, artifact);
227 candidateOrphanArtifacts.put(fileName, artifact);
234 private void addNonNestedVolumeNetworkToTree(Map<HeatStructureTree, HeatStructureTree> netVolToParent, Set<String> nestedFileNames,
236 for (Map.Entry<HeatStructureTree, HeatStructureTree> entry : netVolToParent.entrySet()) {
237 HeatStructureTree netOrVolNode = entry.getKey();
238 HeatStructureTree parent = entry.getValue();
239 if (!nestedFileNames.contains(netOrVolNode.getFileName())) {
241 parent.addVolumeFileToVolumeList(netOrVolNode);
243 parent.addNetworkToNetworkList(netOrVolNode);
249 public HeatStructureTree getTree() {