2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.heat.services.tree;
23 import org.openecomp.core.utilities.file.FileContentHandler;
24 import org.openecomp.core.utilities.file.FileUtils;
25 import org.openecomp.core.utilities.json.JsonUtil;
26 import org.openecomp.core.utilities.yaml.YamlUtil;
27 import org.openecomp.core.validation.types.GlobalValidationContext;
28 import org.openecomp.sdc.common.utils.AsdcCommon;
29 import org.openecomp.sdc.datatypes.error.ErrorMessage;
30 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
31 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
32 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
33 import org.openecomp.sdc.heat.datatypes.structure.Artifact;
34 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
35 import org.openecomp.sdc.heat.services.HeatStructureUtil;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import java.io.InputStream;
41 import java.util.HashMap;
42 import java.util.HashSet;
43 import java.util.List;
48 * The type Heat tree manager.
50 public class HeatTreeManager {
52 private static Logger logger = LoggerFactory.getLogger(HeatTreeManager.class);
55 private FileContentHandler heatContentMap = new FileContentHandler();
56 private byte[] manifest;
57 private HeatStructureTree tree = new HeatStructureTree();
58 private Map<String, HeatStructureTree> fileTreeRef = new HashMap<>();
59 private Map<String, Artifact> artifactRef = new HashMap<>();
60 private Map<String, Artifact> candidateOrphanArtifacts = new HashMap<>();
61 private Map<String, HeatStructureTree> nestedFiles = new HashMap<>();
62 private Map<HeatStructureTree, HeatStructureTree> volumeFileToParent = new HashMap<>();
63 private Map<HeatStructureTree, HeatStructureTree> networkFileToParent = new HashMap<>();
64 private Set<String> manifestFiles = new HashSet<>();
69 * @param fileName the file name
70 * @param content the content
72 public void addFile(String fileName, InputStream content) {
73 if (fileName.equals(AsdcCommon.MANIFEST_NAME)) {
74 manifest = FileUtils.toByteArray(content);
77 heatContentMap.addFile(fileName, content);
84 public void createTree() {
85 if (manifest == null) {
86 logger.error("Missing manifest file in the zip.");
89 ManifestContent manifestData =
90 JsonUtil.json2Object(new String(manifest), ManifestContent.class);
91 scanTree(null, manifestData.getData());
92 addNonNestedVolumeNetworkToTree(volumeFileToParent, nestedFiles.keySet(), true);
93 addNonNestedVolumeNetworkToTree(networkFileToParent, nestedFiles.keySet(), false);
96 tree = fileTreeRef.get(AsdcCommon.PARENT);
99 private void handleOrphans() {
100 tree = fileTreeRef.get(AsdcCommon.PARENT);
101 candidateOrphanArtifacts.entrySet().stream()
102 .forEach(entry -> tree.addArtifactToArtifactList(entry.getValue()));
106 .filter(heatStructureTree -> tree.getHEAT().contains(heatStructureTree))
107 .forEach(heatStructureTree -> tree.getHEAT().remove(heatStructureTree));
109 heatContentMap.getFileList().stream().filter(fileName -> !manifestFiles.contains(fileName))
110 .forEach(fileName -> addTreeOther(fileName));
113 private void addTreeOther(String fileName) {
114 if (tree.getOther() == null) {
115 tree.setOther(new HashSet<>());
117 HeatStructureTree other = new HeatStructureTree(fileName, false);
118 fileTreeRef.put(fileName, other);
119 tree.getOther().add(other);
123 private void handleHeatContentReference(String filename, HeatStructureTree fileHeatStructureTree,
124 GlobalValidationContext globalContext) {
126 String fileName = fileHeatStructureTree.getFileName();
127 InputStream fileContent = this.heatContentMap.getFileContent(fileName);
128 if (fileContent == null) {
129 return; // file exist in manifest but does not exist in zip
132 HeatOrchestrationTemplate hot =
133 new YamlUtil().yamlToObject(fileContent, HeatOrchestrationTemplate.class);
135 Set<String> nestedSet = HeatStructureUtil.getNestedFiles(filename, hot, globalContext);
136 addHeatNestedFiles(fileHeatStructureTree, nestedSet);
138 Set<String> artifactSet = HeatStructureUtil.getArtifactFiles(filename, hot, globalContext);
139 addHeatArtifactFiles(fileHeatStructureTree, artifactSet);
140 } catch (Exception ignore) { /* invalid yaml no need to process reference */ }
143 private void addHeatArtifactFiles(HeatStructureTree fileHeatStructureTree,
144 Set<String> artifactSet) {
146 for (String artifactName : artifactSet) {
148 candidateOrphanArtifacts.get(artifactName) != null ? candidateOrphanArtifacts
149 .get(artifactName).getType() : null;
150 artifact = new Artifact(artifactName, type);
151 artifactRef.put(artifactName, artifact);
152 candidateOrphanArtifacts.remove(artifactName);
153 fileHeatStructureTree.addArtifactToArtifactList(artifact);
158 private void addHeatNestedFiles(HeatStructureTree fileHeatStructureTree, Set<String> nestedSet) {
159 HeatStructureTree childHeatStructureTree;
160 for (String nestedName : nestedSet) {
161 childHeatStructureTree = fileTreeRef.get(nestedName);
162 if (childHeatStructureTree == null) {
163 childHeatStructureTree = new HeatStructureTree();
164 childHeatStructureTree.setFileName(nestedName);
165 fileTreeRef.put(nestedName, childHeatStructureTree);
167 fileHeatStructureTree.addHeatStructureTreeToNestedHeatList(childHeatStructureTree);
168 nestedFiles.put(childHeatStructureTree.getFileName(), childHeatStructureTree);
176 * @param validationErrors the validation errors
178 public void addErrors(Map<String, List<ErrorMessage>> validationErrors) {
180 validationErrors.entrySet().stream().filter(entry -> {
181 return fileTreeRef.get(entry.getKey()) != null;
182 }).forEach(entry -> entry.getValue().stream().forEach(error ->
183 fileTreeRef.get(entry.getKey()).addErrorToErrorsList(error)));
185 validationErrors.entrySet().stream().filter(entry -> {
186 return artifactRef.get(entry.getKey()) != null;
187 }).forEach(entry -> artifactRef.get(entry.getKey()).setErrors(entry.getValue()));
194 * @param parent the parent
195 * @param data the data
197 public void scanTree(String parent, List<FileData> data) {
200 HeatStructureTree parentHeatStructureTree;
201 HeatStructureTree fileHeatStructureTree;
202 HeatStructureTree childHeatStructureTree;
204 if (parent == null) {
205 parentHeatStructureTree = new HeatStructureTree();
206 fileTreeRef.put(AsdcCommon.PARENT, parentHeatStructureTree);
208 parentHeatStructureTree = fileTreeRef.get(parent);
211 for (FileData fileData : data) {
212 fileName = fileData.getFile();
213 manifestFiles.add(fileName);
214 type = fileData.getType();
216 if (FileData.Type.HEAT.equals(type)) {
217 fileHeatStructureTree = fileTreeRef.get(fileName);
218 if (fileHeatStructureTree == null) {
219 fileHeatStructureTree = new HeatStructureTree();
220 fileTreeRef.put(fileName, fileHeatStructureTree);
222 fileHeatStructureTree.setFileName(fileName);
223 fileHeatStructureTree.setBase(fileData.getBase());
224 fileHeatStructureTree.setType(type);
225 handleHeatContentReference(null, fileHeatStructureTree, null);
226 parentHeatStructureTree.addHeatToHEATList(fileHeatStructureTree);
227 if (fileData.getData() != null) {
228 scanTree(fileName, fileData.getData());
231 childHeatStructureTree = new HeatStructureTree();
232 childHeatStructureTree.setFileName(fileName);
233 childHeatStructureTree.setBase(fileData.getBase());
234 childHeatStructureTree.setType(type);
235 fileTreeRef.put(childHeatStructureTree.getFileName(), childHeatStructureTree);
238 parentHeatStructureTree.addOtherToOtherList(childHeatStructureTree);
239 } else if (FileData.Type.HEAT_NET.equals(type)) {
240 // parentHeatStructureTree.addNetworkToNetworkList(childHeatStructureTree);
241 networkFileToParent.put(childHeatStructureTree, parentHeatStructureTree);
242 if (fileData.getData() != null) {
243 scanTree(fileName, fileData.getData());
246 } else if (FileData.Type.HEAT_VOL.equals(type)) {
247 // parentHeatStructureTree.addVolumeFileToVolumeList(childHeatStructureTree);
248 volumeFileToParent.put(childHeatStructureTree, parentHeatStructureTree);
249 if (fileData.getData() != null) {
250 scanTree(fileName, fileData.getData());
252 } else if (FileData.Type.HEAT_ENV.equals(type)) {
253 if (parentHeatStructureTree != null && parentHeatStructureTree.getFileName() != null) {
254 parentHeatStructureTree.setEnv(childHeatStructureTree);
256 if (parentHeatStructureTree.getOther() == null) {
257 parentHeatStructureTree.setOther(new HashSet<>());
259 parentHeatStructureTree.getOther().add(childHeatStructureTree);
262 artifact = new Artifact(fileName, type);
263 if (!artifactRef.keySet().contains(fileName)) {
264 artifactRef.put(fileName, artifact);
265 candidateOrphanArtifacts.put(fileName, artifact);
273 private void addNonNestedVolumeNetworkToTree(
274 Map<HeatStructureTree, HeatStructureTree> netVolToParent, Set<String> nestedFileNames,
276 for (Map.Entry<HeatStructureTree, HeatStructureTree> entry : netVolToParent.entrySet()) {
277 HeatStructureTree netOrVolNode = entry.getKey();
278 HeatStructureTree parent = entry.getValue();
279 if (!nestedFileNames.contains(netOrVolNode.getFileName())) {
281 parent.addVolumeFileToVolumeList(netOrVolNode);
283 parent.addNetworkToNetworkList(netOrVolNode);
295 public HeatStructureTree getTree() {