push addional code
[sdc.git] / openecomp-be / lib / openecomp-heat-lib / src / main / java / org / openecomp / sdc / heat / datatypes / structure / HeatStructureTree.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.heat.datatypes.structure;
22
23
24 import org.codehaus.jackson.annotate.JsonProperty;
25 import org.openecomp.sdc.datatypes.error.ErrorMessage;
26 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Set;
31 import java.util.TreeSet;
32
33 /**
34  * The type Heat structure tree.
35  */
36 public class HeatStructureTree implements Comparable<HeatStructureTree> {
37
38   private String fileName;
39   private FileData.Type type;
40   private Boolean isBase;
41   private HeatStructureTree env;
42   private List<ErrorMessage> errors;
43   private Set<HeatStructureTree> HEAT;
44   private Set<HeatStructureTree> volume;
45   private Set<HeatStructureTree> network;
46   private Set<HeatStructureTree> nested;
47   private Set<HeatStructureTree> other;
48   private Set<Artifact> artifacts;
49
50   /**
51    * Instantiates a new Heat structure tree.
52    */
53   public HeatStructureTree() {
54   }
55
56   ;
57
58   /**
59    * Instantiates a new Heat structure tree.
60    *
61    * @param fileName the file name
62    * @param isBase   the is base
63    */
64   public HeatStructureTree(String fileName, boolean isBase) {
65     setBase(isBase);
66     setFileName(fileName);
67   }
68
69   /**
70    * Gets heat structure tree by name.
71    *
72    * @param filesSet the files set
73    * @param filename the filename
74    * @return the heat structure tree by name
75    */
76   public static HeatStructureTree getHeatStructureTreeByName(Set<HeatStructureTree> filesSet,
77                                                              String filename) {
78     for (HeatStructureTree heatStructureTree : filesSet) {
79       if (heatStructureTree.getFileName().equals(filename)) {
80         return heatStructureTree;
81       }
82     }
83
84     return null;
85   }
86
87   /**
88    * Sets type.
89    *
90    * @param type the type
91    */
92   public void setType(FileData.Type type) {
93     this.type = type;
94   }
95
96   /**
97    * Gets base.
98    *
99    * @return the base
100    */
101   public Boolean getBase() {
102     return isBase;
103   }
104
105   /**
106    * Sets base.
107    *
108    * @param base the base
109    */
110   public void setBase(Boolean base) {
111     isBase = base;
112   }
113
114   /**
115    * Gets file name.
116    *
117    * @return the file name
118    */
119   public String getFileName() {
120     return fileName;
121   }
122
123   /**
124    * Sets file name.
125    *
126    * @param file the file
127    */
128   public void setFileName(String file) {
129     this.fileName = file;
130   }
131
132   /**
133    * Gets heat.
134    *
135    * @return the heat
136    */
137   @JsonProperty(value = "HEAT")
138   public Set<HeatStructureTree> getHEAT() {
139     return HEAT;
140   }
141
142   /**
143    * Sets heat.
144    *
145    * @param heat the heat
146    */
147   public void setHEAT(Set<HeatStructureTree> heat) {
148     this.HEAT = heat;
149   }
150
151   /**
152    * Gets nested.
153    *
154    * @return the nested
155    */
156   public Set<HeatStructureTree> getNested() {
157     return nested;
158   }
159
160   /**
161    * Sets nested.
162    *
163    * @param nested the nested
164    */
165   public void setNested(Set<HeatStructureTree> nested) {
166     this.nested = nested;
167   }
168
169   /**
170    * Gets artifacts.
171    *
172    * @return the artifacts
173    */
174   public Set<Artifact> getArtifacts() {
175     return artifacts;
176   }
177
178   /**
179    * Sets artifacts.
180    *
181    * @param artifacts the artifacts
182    */
183   public void setArtifacts(Set<Artifact> artifacts) {
184     this.artifacts = artifacts;
185   }
186
187   /**
188    * Add heat structure tree to nested heat list.
189    *
190    * @param heatStructureTree the heat structure tree
191    */
192   public void addHeatStructureTreeToNestedHeatList(HeatStructureTree heatStructureTree) {
193     if (this.nested == null) {
194       this.nested = new TreeSet<>();
195     }
196     if (!findItemInSetByName(this.nested, heatStructureTree)) {
197       this.nested.add(heatStructureTree);
198     }
199   }
200
201   /**
202    * Add artifact to artifact list.
203    *
204    * @param artifact the artifact
205    */
206   public void addArtifactToArtifactList(Artifact artifact) {
207     if (this.artifacts == null || this.artifacts.isEmpty()) {
208       this.artifacts = new TreeSet<>();
209     }
210     this.artifacts.add(artifact);
211   }
212
213   /**
214    * Gets env.
215    *
216    * @return the env
217    */
218   public HeatStructureTree getEnv() {
219     return env;
220   }
221
222   /**
223    * Sets env.
224    *
225    * @param env the env
226    */
227   public void setEnv(HeatStructureTree env) {
228     this.env = env;
229   }
230
231   /**
232    * Gets volume.
233    *
234    * @return the volume
235    */
236   public Set<HeatStructureTree> getVolume() {
237     return volume;
238   }
239
240   /**
241    * Sets volume.
242    *
243    * @param volume the volume
244    */
245   public void setVolume(Set<HeatStructureTree> volume) {
246     this.volume = volume;
247   }
248
249   /**
250    * Gets network.
251    *
252    * @return the network
253    */
254   public Set<HeatStructureTree> getNetwork() {
255     return network;
256   }
257
258   /**
259    * Sets network.
260    *
261    * @param network the network
262    */
263   public void setNetwork(Set<HeatStructureTree> network) {
264     this.network = network;
265   }
266
267   /**
268    * Add network to network list.
269    *
270    * @param heatStructureTree the heat structure tree
271    */
272   public void addNetworkToNetworkList(HeatStructureTree heatStructureTree) {
273     if (this.network == null) {
274       this.network = new TreeSet<>();
275     }
276     if (!findItemInSetByName(this.network, heatStructureTree)) {
277       this.network.add(heatStructureTree);
278     }
279   }
280
281   /**
282    * Add volume file to volume list.
283    *
284    * @param heatStructureTree the heat structure tree
285    */
286   public void addVolumeFileToVolumeList(HeatStructureTree heatStructureTree) {
287     if (this.volume == null) {
288       this.volume = new TreeSet<>();
289     }
290     if (!findItemInSetByName(this.volume, heatStructureTree)) {
291       this.volume.add(heatStructureTree);
292     }
293   }
294
295   /**
296    * Add heat to heat list.
297    *
298    * @param heat the heat
299    */
300   public void addHeatToHEATList(HeatStructureTree heat) {
301     if (this.HEAT == null) {
302       this.HEAT = new TreeSet<>();
303     }
304
305     this.HEAT.add(heat);
306   }
307
308   /**
309    * Add other to other list.
310    *
311    * @param other the other
312    */
313   public void addOtherToOtherList(HeatStructureTree other) {
314     if (this.other == null) {
315       this.other = new TreeSet<>();
316     }
317
318     this.other.add(other);
319   }
320
321   /**
322    * Find item in set by name boolean.
323    *
324    * @param searchSet the search set
325    * @param toFind    the to find
326    * @return the boolean
327    */
328   public boolean findItemInSetByName(Set<HeatStructureTree> searchSet, HeatStructureTree toFind) {
329     for (HeatStructureTree heatStructureTree : searchSet) {
330       if (heatStructureTree.getFileName().equals(toFind.getFileName())) {
331         return true;
332       }
333
334     }
335
336     return false;
337   }
338
339   /**
340    * Remove from volume or network.
341    *
342    * @param fileNameToRemove the file name to remove
343    * @param type             the type
344    */
345   public void removeFromVolumeOrNetwork(String fileNameToRemove, FileData.Type type) {
346     Set<HeatStructureTree> volumeOrNetworkSet =
347         type.equals(FileData.Type.HEAT_VOL) ? this.volume : this.network;
348     HeatStructureTree toRemove = getHeatStructureTreeByName(volumeOrNetworkSet, fileNameToRemove);
349
350     volumeOrNetworkSet.remove(toRemove);
351   }
352
353   @Override
354   public boolean equals(Object other) {
355     if (this == other) {
356       return true;
357     }
358     if (other == null || getClass() != other.getClass()) {
359       return false;
360     }
361
362     HeatStructureTree heatStructureTree = (HeatStructureTree) other;
363
364     if (fileName != null ? !fileName.equals(heatStructureTree.fileName)
365         : heatStructureTree.fileName != null) {
366       return false;
367     }
368     if (env != null ? !env.equals(heatStructureTree.env) : heatStructureTree.env != null) {
369       return false;
370     }
371     if (HEAT != null ? !HEAT.equals(heatStructureTree.HEAT) : heatStructureTree.HEAT != null) {
372       return false;
373     }
374     if (volume != null ? !volume.equals(heatStructureTree.volume)
375         : heatStructureTree.volume != null) {
376       return false;
377     }
378     if (network != null ? !network.equals(heatStructureTree.network)
379         : heatStructureTree.network != null) {
380       return false;
381     }
382     if (artifacts != null ? !artifacts.equals(heatStructureTree.artifacts)
383         : heatStructureTree.artifacts != null) {
384       return false;
385     }
386     if (nested != null ? !nested.equals(heatStructureTree.nested)
387         : heatStructureTree.nested != null) {
388       return false;
389     }
390     if (errors != null ? !errors.equals(heatStructureTree.errors)
391         : heatStructureTree.errors != null) {
392       return false;
393     }
394
395     return true;
396   }
397
398   @Override
399   public int hashCode() {
400     int result1 = fileName != null ? fileName.hashCode() : 0;
401     result1 = 31 * result1 + (env != null ? env.hashCode() : 0);
402     result1 = 31 * result1 + (HEAT != null ? HEAT.hashCode() : 0);
403     result1 = 31 * result1 + (volume != null ? volume.hashCode() : 0);
404     result1 = 31 * result1 + (network != null ? network.hashCode() : 0);
405     result1 = 31 * result1 + (artifacts != null ? artifacts.hashCode() : 0);
406     result1 = 31 * result1 + (nested != null ? nested.hashCode() : 0);
407     result1 = 31 * result1 + (errors != null ? errors.hashCode() : 0);
408
409
410     return result1;
411   }
412
413   /**
414    * Gets errors.
415    *
416    * @return the errors
417    */
418   public List<ErrorMessage> getErrors() {
419     return errors;
420   }
421
422   /**
423    * Sets errors.
424    *
425    * @param errors the errors
426    */
427   public void setErrors(List<ErrorMessage> errors) {
428     this.errors = errors;
429   }
430
431   /**
432    * Add error to errors list.
433    *
434    * @param error the error
435    */
436   public void addErrorToErrorsList(ErrorMessage error) {
437     if (this.errors == null || this.errors.isEmpty()) {
438       this.errors = new ArrayList<>();
439     }
440     if (!this.errors.contains(error)) {
441       this.errors.add(error);
442     }
443   }
444
445   /**
446    * Gets other.
447    *
448    * @return the other
449    */
450   public Set<HeatStructureTree> getOther() {
451     return other;
452   }
453
454   /**
455    * Sets other.
456    *
457    * @param other the other
458    */
459   public void setOther(Set<HeatStructureTree> other) {
460     this.other = other;
461   }
462
463   @Override
464   public int compareTo(HeatStructureTree heatStructureTree) {
465     return heatStructureTree.getFileName().compareTo(this.getFileName());
466   }
467 }