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.
17 package org.openecomp.sdc.heat.services.tree;
19 import java.io.InputStream;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
24 import java.util.Objects;
27 import org.onap.sdc.tosca.services.YamlUtil;
28 import org.openecomp.core.utilities.file.FileContentHandler;
29 import org.openecomp.core.utilities.file.FileUtils;
30 import org.openecomp.core.utilities.json.JsonUtil;
31 import org.openecomp.core.validation.types.GlobalValidationContext;
32 import org.openecomp.sdc.common.utils.SdcCommon;
33 import org.openecomp.sdc.datatypes.error.ErrorMessage;
34 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
35 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
36 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
37 import org.openecomp.sdc.heat.datatypes.structure.Artifact;
38 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
39 import org.openecomp.sdc.logging.api.Logger;
40 import org.openecomp.sdc.logging.api.LoggerFactory;
43 public class HeatTreeManager {
45 private static final Logger LOGGER = LoggerFactory.getLogger(HeatTreeManager.class);
48 private FileContentHandler heatContentMap = new FileContentHandler();
49 private byte[] manifest;
50 private HeatStructureTree tree = new HeatStructureTree();
51 private Map<String, HeatStructureTree> fileTreeRef = new HashMap<>();
52 private Map<String, Artifact> artifactRef = new HashMap<>();
53 private Map<String, Artifact> candidateOrphanArtifacts = new HashMap<>();
54 private Map<String, HeatStructureTree> nestedFiles = new HashMap<>();
55 private Map<HeatStructureTree, HeatStructureTree> volumeFileToParent = new HashMap<>();
56 private Map<HeatStructureTree, HeatStructureTree> networkFileToParent = new HashMap<>();
57 private Set<String> manifestFiles = new HashSet<>();
62 * @param fileName the file name
63 * @param content the content
65 public void addFile(String fileName, InputStream content) {
66 if (fileName.equals(SdcCommon.MANIFEST_NAME)) {
67 manifest = FileUtils.toByteArray(content);
70 heatContentMap.addFile(fileName, content);
77 public void createTree() {
78 if (manifest == null) {
79 LOGGER.error("Missing manifest file in the zip.");
82 ManifestContent manifestData =
83 JsonUtil.json2Object(new String(manifest), ManifestContent.class);
84 scanTree(null, manifestData.getData());
85 addNonNestedVolumeNetworkToTree(volumeFileToParent, nestedFiles.keySet(), true);
86 addNonNestedVolumeNetworkToTree(networkFileToParent, nestedFiles.keySet(), false);
89 tree = fileTreeRef.get(SdcCommon.PARENT);
92 private void handleOrphans() {
93 tree = fileTreeRef.get(SdcCommon.PARENT);
94 candidateOrphanArtifacts.forEach((key, value) -> tree.addArtifactToArtifactList(value));
96 .values().stream().filter(tree.getHeat()::contains)
97 .forEach(tree.getHeat()::remove);
99 heatContentMap.getFileList().stream().filter(this::isNotInManifestFiles)
100 .forEach(this::addTreeOther);
103 private boolean isNotInManifestFiles(String fileName) {
104 return !manifestFiles.contains(fileName);
107 private void addTreeOther(String fileName) {
108 if (tree.getOther() == null) {
109 tree.setOther(new HashSet<>());
111 HeatStructureTree other = new HeatStructureTree(fileName, false);
112 fileTreeRef.put(fileName, other);
113 tree.getOther().add(other);
117 private void handleHeatContentReference(HeatStructureTree fileHeatStructureTree,
118 GlobalValidationContext globalContext) {
120 String fileName = fileHeatStructureTree.getFileName();
121 try (InputStream fileContent = this.heatContentMap.getFileContentAsStream(fileName)) {
122 HeatOrchestrationTemplate hot =
123 new YamlUtil().yamlToObject(fileContent, HeatOrchestrationTemplate.class);
125 Set<String> nestedSet = HeatTreeManagerUtil.getNestedFiles(hot);
126 addHeatNestedFiles(fileHeatStructureTree, nestedSet);
128 Set<String> artifactSet = HeatTreeManagerUtil.getArtifactFiles(fileName, hot, globalContext);
129 addHeatArtifactFiles(fileHeatStructureTree, artifactSet);
130 } catch (Exception exp) {
131 LOGGER.debug("Invalid YAML received. No need to process content reference - ignoring", exp);
136 private void addHeatArtifactFiles(HeatStructureTree fileHeatStructureTree,
137 Set<String> artifactSet) {
139 for (String artifactName : artifactSet) {
141 candidateOrphanArtifacts.get(artifactName) != null ? candidateOrphanArtifacts
142 .get(artifactName).getType() : null;
143 artifact = new Artifact(artifactName, type);
144 artifactRef.put(artifactName, artifact);
145 candidateOrphanArtifacts.remove(artifactName);
146 fileHeatStructureTree.addArtifactToArtifactList(artifact);
151 private void addHeatNestedFiles(HeatStructureTree fileHeatStructureTree, Set<String> nestedSet) {
152 HeatStructureTree childHeatStructureTree;
153 for (String nestedName : nestedSet) {
154 childHeatStructureTree = fileTreeRef.get(nestedName);
155 if (childHeatStructureTree == null) {
156 childHeatStructureTree = new HeatStructureTree();
157 childHeatStructureTree.setFileName(nestedName);
158 fileTreeRef.put(nestedName, childHeatStructureTree);
160 fileHeatStructureTree.addHeatStructureTreeToNestedHeatList(childHeatStructureTree);
161 nestedFiles.put(childHeatStructureTree.getFileName(), childHeatStructureTree);
169 * @param validationErrors the validation errors
171 public void addErrors(Map<String, List<ErrorMessage>> validationErrors) {
173 validationErrors.entrySet().stream()
174 .filter(entry -> fileTreeRef.get(entry.getKey()) != null)
175 .forEach(entry -> entry.getValue().forEach(fileTreeRef.get(entry.getKey())::addErrorToErrorsList));
177 validationErrors.entrySet().stream()
178 .filter(entry -> artifactRef.get(entry.getKey()) != null)
179 .forEach(entry -> artifactRef.get(entry.getKey()).setErrors(entry.getValue()));
186 * @param parent the parent
187 * @param data the data
189 public void scanTree(String parent, List<FileData> data) {
192 HeatStructureTree parentHeatStructureTree;
193 HeatStructureTree fileHeatStructureTree;
194 HeatStructureTree childHeatStructureTree;
196 if (parent == null) {
197 parentHeatStructureTree = new HeatStructureTree();
198 fileTreeRef.put(SdcCommon.PARENT, parentHeatStructureTree);
199 fileTreeRef.put(SdcCommon.MANIFEST_NAME, parentHeatStructureTree);
201 parentHeatStructureTree = fileTreeRef.get(parent);
204 for (FileData fileData : data) {
205 fileName = fileData.getFile();
206 manifestFiles.add(fileName);
207 type = fileData.getType();
209 if (Objects.nonNull(type) && FileData.Type.HEAT.equals(type)) {
210 fileHeatStructureTree = fileTreeRef.get(fileName);
211 if (fileHeatStructureTree == null) {
212 fileHeatStructureTree = new HeatStructureTree();
213 fileTreeRef.put(fileName, fileHeatStructureTree);
215 fileHeatStructureTree.setFileName(fileName);
216 fileHeatStructureTree.setBase(fileData.getBase());
217 fileHeatStructureTree.setType(type);
218 handleHeatContentReference(fileHeatStructureTree, null);
219 parentHeatStructureTree.addHeatToHeatList(fileHeatStructureTree);
220 if (fileData.getData() != null) {
221 scanTree(fileName, fileData.getData());
224 childHeatStructureTree = new HeatStructureTree();
225 childHeatStructureTree.setFileName(fileName);
226 childHeatStructureTree.setBase(fileData.getBase());
227 childHeatStructureTree.setType(type);
228 fileTreeRef.put(childHeatStructureTree.getFileName(), childHeatStructureTree);
231 parentHeatStructureTree.addOtherToOtherList(childHeatStructureTree);
232 } else if (FileData.Type.HEAT_NET.equals(type)) {
233 networkFileToParent.put(childHeatStructureTree, parentHeatStructureTree);
234 if (fileData.getData() != null) {
235 scanTree(fileName, fileData.getData());
237 handleHeatContentReference(childHeatStructureTree, null);
239 } else if (FileData.Type.HEAT_VOL.equals(type)) {
240 volumeFileToParent.put(childHeatStructureTree, parentHeatStructureTree);
241 if (fileData.getData() != null) {
242 scanTree(fileName, fileData.getData());
244 handleHeatContentReference(childHeatStructureTree, null);
245 } else if (FileData.Type.HEAT_ENV.equals(type)) {
246 if (parentHeatStructureTree != null && parentHeatStructureTree.getFileName() != null) {
247 parentHeatStructureTree.setEnv(childHeatStructureTree);
249 if (parentHeatStructureTree.getOther() == null) {
250 parentHeatStructureTree.setOther(new HashSet<>());
252 parentHeatStructureTree.getOther().add(childHeatStructureTree);
254 } else if (FileData.Type.HELM.equals(type)) {
255 parentHeatStructureTree.addToHelmList(childHeatStructureTree);
258 artifact = new Artifact(fileName, type);
259 if (!artifactRef.keySet().contains(fileName)) {
260 artifactRef.put(fileName, artifact);
261 candidateOrphanArtifacts.put(fileName, artifact);
269 private void addNonNestedVolumeNetworkToTree(
270 Map<HeatStructureTree, HeatStructureTree> netVolToParent, Set<String> nestedFileNames,
272 for (Map.Entry<HeatStructureTree, HeatStructureTree> entry : netVolToParent.entrySet()) {
273 HeatStructureTree netOrVolNode = entry.getKey();
274 HeatStructureTree parent = entry.getValue();
275 if (!nestedFileNames.contains(netOrVolNode.getFileName())) {
277 parent.addVolumeFileToVolumeList(netOrVolNode);
279 parent.addNetworkToNetworkList(netOrVolNode);
286 public HeatStructureTree getTree() {