aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/HeatTreeManager.java
blob: 02278acf3f5498d4c23b344b6c9db872ff4addae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.heat.services.tree;

import org.openecomp.core.utilities.file.FileContentHandler;
import org.openecomp.core.utilities.file.FileUtils;
import org.openecomp.core.utilities.json.JsonUtil;
import org.openecomp.core.utilities.yaml.YamlUtil;
import org.openecomp.core.validation.types.GlobalValidationContext;
import org.openecomp.sdc.common.utils.SdcCommon;
import org.openecomp.sdc.datatypes.error.ErrorMessage;
import org.openecomp.sdc.heat.datatypes.manifest.FileData;
import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
import org.openecomp.sdc.heat.datatypes.structure.Artifact;
import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;

import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;


public class HeatTreeManager {

  private static Logger logger = (Logger) LoggerFactory.getLogger(HeatTreeManager.class);


  private FileContentHandler heatContentMap = new FileContentHandler();
  private byte[] manifest;
  private HeatStructureTree tree = new HeatStructureTree();
  private Map<String, HeatStructureTree> fileTreeRef = new HashMap<>();
  private Map<String, Artifact> artifactRef = new HashMap<>();
  private Map<String, Artifact> candidateOrphanArtifacts = new HashMap<>();
  private Map<String, HeatStructureTree> nestedFiles = new HashMap<>();
  private Map<HeatStructureTree, HeatStructureTree> volumeFileToParent = new HashMap<>();
  private Map<HeatStructureTree, HeatStructureTree> networkFileToParent = new HashMap<>();
  private Set<String> manifestFiles = new HashSet<>();

  /**
   * Add file.
   *
   * @param fileName the file name
   * @param content  the content
   */
  public void addFile(String fileName, InputStream content) {
    if (fileName.equals(SdcCommon.MANIFEST_NAME)) {
      manifest = FileUtils.toByteArray(content);

    } else {
      heatContentMap.addFile(fileName, content);
    }
  }

  /**
   * Create tree.
   */
  public void createTree() {
    if (manifest == null) {
      logger.error("Missing manifest file in the zip.");
      return;
    }
    ManifestContent manifestData =
        JsonUtil.json2Object(new String(manifest), ManifestContent.class);
    scanTree(null, manifestData.getData());
    addNonNestedVolumeNetworkToTree(volumeFileToParent, nestedFiles.keySet(), true);
    addNonNestedVolumeNetworkToTree(networkFileToParent, nestedFiles.keySet(), false);
    handleOrphans();

    tree = fileTreeRef.get(SdcCommon.PARENT);
  }

  private void handleOrphans() {
    tree = fileTreeRef.get(SdcCommon.PARENT);
    candidateOrphanArtifacts.entrySet().stream()
        .forEach(entry -> tree.addArtifactToArtifactList(entry.getValue()));
    nestedFiles
        .values().stream().filter(heatStructureTree -> tree.getHeat().contains(heatStructureTree))
        .forEach(heatStructureTree -> tree.getHeat().remove(heatStructureTree));

    heatContentMap.getFileList().stream().filter(fileName -> !manifestFiles.contains(fileName))
        .forEach(fileName -> addTreeOther(fileName));
  }

  private void addTreeOther(String fileName) {
    if (tree.getOther() == null) {
      tree.setOther(new HashSet<>());
    }
    HeatStructureTree other = new HeatStructureTree(fileName, false);
    fileTreeRef.put(fileName, other);
    tree.getOther().add(other);
  }


  private void handleHeatContentReference(String filename, HeatStructureTree fileHeatStructureTree,
                                          GlobalValidationContext globalContext) {

    String fileName = fileHeatStructureTree.getFileName();
    InputStream fileContent = this.heatContentMap.getFileContent(fileName);
    if (fileContent == null) {
      return; // file exist in manifest but does not exist in zip
    }
    try {
      HeatOrchestrationTemplate hot =
          new YamlUtil().yamlToObject(fileContent, HeatOrchestrationTemplate.class);

      Set<String> nestedSet = HeatTreeManagerUtil.getNestedFiles(filename, hot, globalContext);
      addHeatNestedFiles(fileHeatStructureTree, nestedSet);

      Set<String> artifactSet = HeatTreeManagerUtil.getArtifactFiles(filename, hot, globalContext);
      addHeatArtifactFiles(fileHeatStructureTree, artifactSet);
    } catch (Exception ignore) { /* invalid yaml no need to process reference */ }
  }


  private void addHeatArtifactFiles(HeatStructureTree fileHeatStructureTree,
                                    Set<String> artifactSet) {
    Artifact artifact;
    for (String artifactName : artifactSet) {
      FileData.Type type =
          candidateOrphanArtifacts.get(artifactName) != null ? candidateOrphanArtifacts
              .get(artifactName).getType() : null;
      artifact = new Artifact(artifactName, type);
      artifactRef.put(artifactName, artifact);
      candidateOrphanArtifacts.remove(artifactName);
      fileHeatStructureTree.addArtifactToArtifactList(artifact);
    }
  }


  private void addHeatNestedFiles(HeatStructureTree fileHeatStructureTree, Set<String> nestedSet) {
    HeatStructureTree childHeatStructureTree;
    for (String nestedName : nestedSet) {
      childHeatStructureTree = fileTreeRef.get(nestedName);
      if (childHeatStructureTree == null) {
        childHeatStructureTree = new HeatStructureTree();
        childHeatStructureTree.setFileName(nestedName);
        fileTreeRef.put(nestedName, childHeatStructureTree);
      }
      fileHeatStructureTree.addHeatStructureTreeToNestedHeatList(childHeatStructureTree);
      nestedFiles.put(childHeatStructureTree.getFileName(), childHeatStructureTree);
    }
  }


  /**
   * Add errors.
   *
   * @param validationErrors the validation errors
   */
  public void addErrors(Map<String, List<ErrorMessage>> validationErrors) {

    validationErrors.entrySet().stream().filter(entry -> {
      return fileTreeRef.get(entry.getKey()) != null;
    }).forEach(entry -> entry.getValue().stream().forEach(error ->
        fileTreeRef.get(entry.getKey()).addErrorToErrorsList(error)));

    validationErrors.entrySet().stream().filter(entry -> {
      return artifactRef.get(entry.getKey()) != null;
    }).forEach(entry -> artifactRef.get(entry.getKey()).setErrors(entry.getValue()));

  }

  /**
   * Scan tree.
   *
   * @param parent the parent
   * @param data   the data
   */
  public void scanTree(String parent, List<FileData> data) {
    String fileName;
    FileData.Type type;
    HeatStructureTree parentHeatStructureTree;
    HeatStructureTree fileHeatStructureTree;
    HeatStructureTree childHeatStructureTree;
    Artifact artifact;
    if (parent == null) {
      parentHeatStructureTree = new HeatStructureTree();
      fileTreeRef.put(SdcCommon.PARENT, parentHeatStructureTree);
    } else {
      parentHeatStructureTree = fileTreeRef.get(parent);
    }

    for (FileData fileData : data) {
      fileName = fileData.getFile();
      manifestFiles.add(fileName);
      type = fileData.getType();

      if (Objects.nonNull(type) && FileData.Type.HEAT.equals(type)) {
        fileHeatStructureTree = fileTreeRef.get(fileName);
        if (fileHeatStructureTree == null) {
          fileHeatStructureTree = new HeatStructureTree();
          fileTreeRef.put(fileName, fileHeatStructureTree);
        }
        fileHeatStructureTree.setFileName(fileName);
        fileHeatStructureTree.setBase(fileData.getBase());
        fileHeatStructureTree.setType(type);
        handleHeatContentReference(null, fileHeatStructureTree, null);
        parentHeatStructureTree.addHeatToHeatList(fileHeatStructureTree);
        if (fileData.getData() != null) {
          scanTree(fileName, fileData.getData());
        }
      } else {
        childHeatStructureTree = new HeatStructureTree();
        childHeatStructureTree.setFileName(fileName);
        childHeatStructureTree.setBase(fileData.getBase());
        childHeatStructureTree.setType(type);
        fileTreeRef.put(childHeatStructureTree.getFileName(), childHeatStructureTree);

        if (type == null) {
          parentHeatStructureTree.addOtherToOtherList(childHeatStructureTree);
        } else if (FileData.Type.HEAT_NET.equals(type)) {
          //parentHeatStructureTree.addNetworkToNetworkList(childHeatStructureTree);
          networkFileToParent.put(childHeatStructureTree, parentHeatStructureTree);
          if (fileData.getData() != null) {
            scanTree(fileName, fileData.getData());
          }

        } else if (FileData.Type.HEAT_VOL.equals(type)) {
          //parentHeatStructureTree.addVolumeFileToVolumeList(childHeatStructureTree);
          volumeFileToParent.put(childHeatStructureTree, parentHeatStructureTree);
          if (fileData.getData() != null) {
            scanTree(fileName, fileData.getData());
          }
        } else if (FileData.Type.HEAT_ENV.equals(type)) {
          if (parentHeatStructureTree != null && parentHeatStructureTree.getFileName() != null) {
            parentHeatStructureTree.setEnv(childHeatStructureTree);
          } else {
            if (parentHeatStructureTree.getOther() == null) {
              parentHeatStructureTree.setOther(new HashSet<>());
            }
            parentHeatStructureTree.getOther().add(childHeatStructureTree);
          }
        } else {
          artifact = new Artifact(fileName, type);
          if (!artifactRef.keySet().contains(fileName)) {
            artifactRef.put(fileName, artifact);
            candidateOrphanArtifacts.put(fileName, artifact);
          }
        }
      }
    }
  }


  private void addNonNestedVolumeNetworkToTree(
      Map<HeatStructureTree, HeatStructureTree> netVolToParent, Set<String> nestedFileNames,
      boolean isVolume) {
    for (Map.Entry<HeatStructureTree, HeatStructureTree> entry : netVolToParent.entrySet()) {
      HeatStructureTree netOrVolNode = entry.getKey();
      HeatStructureTree parent = entry.getValue();
      if (!nestedFileNames.contains(netOrVolNode.getFileName())) {
        if (isVolume) {
          parent.addVolumeFileToVolumeList(netOrVolNode);
        } else {
          parent.addNetworkToNetworkList(netOrVolNode);
        }
      }
    }
  }


  public HeatStructureTree getTree() {
    return tree;
  }
}