[SDC-29] Amdocs OnBoard 1707 initial commit.
[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 public class HeatStructureTree implements Comparable<HeatStructureTree> {
34
35   private String fileName;
36   private FileData.Type type;
37   private Boolean isBase;
38   private HeatStructureTree env;
39   private List<ErrorMessage> errors;
40   private Set<HeatStructureTree> heat;
41   private Set<HeatStructureTree> volume;
42   private Set<HeatStructureTree> network;
43   private Set<HeatStructureTree> nested;
44   private Set<HeatStructureTree> other;
45   private Set<Artifact> artifacts;
46
47   public HeatStructureTree() {
48   }
49
50   ;
51
52   public HeatStructureTree(String fileName, boolean isBase) {
53     setBase(isBase);
54     setFileName(fileName);
55   }
56
57   /**
58    * Gets heat structure tree by name.
59    *
60    * @param filesSet the files set
61    * @param filename the filename
62    * @return the heat structure tree by name
63    */
64   public static HeatStructureTree getHeatStructureTreeByName(Set<HeatStructureTree> filesSet,
65                                                              String filename) {
66     for (HeatStructureTree heatStructureTree : filesSet) {
67       if (heatStructureTree.getFileName().equals(filename)) {
68         return heatStructureTree;
69       }
70     }
71
72     return null;
73   }
74
75   public void setType(FileData.Type type) {
76     this.type = type;
77   }
78
79   public Boolean getBase() {
80     return isBase;
81   }
82
83   public void setBase(Boolean base) {
84     isBase = base;
85   }
86
87   public String getFileName() {
88     return fileName;
89   }
90
91   public void setFileName(String file) {
92     this.fileName = file;
93   }
94
95   @JsonProperty(value = "heat")
96   public Set<HeatStructureTree> getHeat() {
97     return heat;
98   }
99
100   public void setHeat(Set<HeatStructureTree> heat) {
101     this.heat = heat;
102   }
103
104   public Set<HeatStructureTree> getNested() {
105     return nested;
106   }
107
108   public void setNested(Set<HeatStructureTree> nested) {
109     this.nested = nested;
110   }
111
112   public Set<Artifact> getArtifacts() {
113     return artifacts;
114   }
115
116   public void setArtifacts(Set<Artifact> artifacts) {
117     this.artifacts = artifacts;
118   }
119
120   /**
121    * Add heat structure tree to nested heat list.
122    *
123    * @param heatStructureTree the heat structure tree
124    */
125   public void addHeatStructureTreeToNestedHeatList(HeatStructureTree heatStructureTree) {
126     if (this.nested == null) {
127       this.nested = new TreeSet<>();
128     }
129     if (!findItemInSetByName(this.nested, heatStructureTree)) {
130       this.nested.add(heatStructureTree);
131     }
132   }
133
134   /**
135    * Add artifact to artifact list.
136    *
137    * @param artifact the artifact
138    */
139   public void addArtifactToArtifactList(Artifact artifact) {
140     if (this.artifacts == null || this.artifacts.isEmpty()) {
141       this.artifacts = new TreeSet<>();
142     }
143     this.artifacts.add(artifact);
144   }
145
146   public HeatStructureTree getEnv() {
147     return env;
148   }
149
150   public void setEnv(HeatStructureTree env) {
151     this.env = env;
152   }
153
154   public Set<HeatStructureTree> getVolume() {
155     return volume;
156   }
157
158   public void setVolume(Set<HeatStructureTree> volume) {
159     this.volume = volume;
160   }
161
162   public Set<HeatStructureTree> getNetwork() {
163     return network;
164   }
165
166   public void setNetwork(Set<HeatStructureTree> network) {
167     this.network = network;
168   }
169
170   /**
171    * Add network to network list.
172    *
173    * @param heatStructureTree the heat structure tree
174    */
175   public void addNetworkToNetworkList(HeatStructureTree heatStructureTree) {
176     if (this.network == null) {
177       this.network = new TreeSet<>();
178     }
179     if (!findItemInSetByName(this.network, heatStructureTree)) {
180       this.network.add(heatStructureTree);
181     }
182   }
183
184   /**
185    * Add volume file to volume list.
186    *
187    * @param heatStructureTree the heat structure tree
188    */
189   public void addVolumeFileToVolumeList(HeatStructureTree heatStructureTree) {
190     if (this.volume == null) {
191       this.volume = new TreeSet<>();
192     }
193     if (!findItemInSetByName(this.volume, heatStructureTree)) {
194       this.volume.add(heatStructureTree);
195     }
196   }
197
198   /**
199    * Add heat to heat list.
200    *
201    * @param heat the heat
202    */
203   public void addHeatToHeatList(HeatStructureTree heat) {
204     if (this.heat == null) {
205       this.heat = new TreeSet<>();
206     }
207
208     this.heat.add(heat);
209   }
210
211   /**
212    * Add other to other list.
213    *
214    * @param other the other
215    */
216   public void addOtherToOtherList(HeatStructureTree other) {
217     if (this.other == null) {
218       this.other = new TreeSet<>();
219     }
220
221     this.other.add(other);
222   }
223
224   /**
225    * Find item in set by name boolean.
226    *
227    * @param searchSet the search set
228    * @param toFind    the to find
229    * @return the boolean
230    */
231   public boolean findItemInSetByName(Set<HeatStructureTree> searchSet, HeatStructureTree toFind) {
232     for (HeatStructureTree heatStructureTree : searchSet) {
233       if (heatStructureTree.getFileName().equals(toFind.getFileName())) {
234         return true;
235       }
236
237     }
238
239     return false;
240   }
241
242   /**
243    * Remove from volume or network.
244    *
245    * @param fileNameToRemove the file name to remove
246    * @param type             the type
247    */
248   public void removeFromVolumeOrNetwork(String fileNameToRemove, FileData.Type type) {
249     Set<HeatStructureTree> volumeOrNetworkSet =
250         type.equals(FileData.Type.HEAT_VOL) ? this.volume : this.network;
251     HeatStructureTree toRemove = getHeatStructureTreeByName(volumeOrNetworkSet, fileNameToRemove);
252
253     volumeOrNetworkSet.remove(toRemove);
254   }
255
256   @Override
257   public int hashCode() {
258     int result1 = fileName != null ? fileName.hashCode() : 0;
259     result1 = 31 * result1 + (env != null ? env.hashCode() : 0);
260     result1 = 31 * result1 + (heat != null ? heat.hashCode() : 0);
261     result1 = 31 * result1 + (volume != null ? volume.hashCode() : 0);
262     result1 = 31 * result1 + (network != null ? network.hashCode() : 0);
263     result1 = 31 * result1 + (artifacts != null ? artifacts.hashCode() : 0);
264     result1 = 31 * result1 + (nested != null ? nested.hashCode() : 0);
265     result1 = 31 * result1 + (errors != null ? errors.hashCode() : 0);
266
267
268     return result1;
269   }
270
271   @Override
272   public boolean equals(Object other) {
273     if (this == other) {
274       return true;
275     }
276     if (other == null || getClass() != other.getClass()) {
277       return false;
278     }
279
280     HeatStructureTree heatStructureTree = (HeatStructureTree) other;
281
282     if (fileName != null ? !fileName.equals(heatStructureTree.fileName)
283         : heatStructureTree.fileName != null) {
284       return false;
285     }
286     if (env != null ? !env.equals(heatStructureTree.env) : heatStructureTree.env != null) {
287       return false;
288     }
289     if (heat != null ? !heat.equals(heatStructureTree.heat) : heatStructureTree.heat != null) {
290       return false;
291     }
292     if (volume != null ? !volume.equals(heatStructureTree.volume)
293         : heatStructureTree.volume != null) {
294       return false;
295     }
296     if (network != null ? !network.equals(heatStructureTree.network)
297         : heatStructureTree.network != null) {
298       return false;
299     }
300     if (artifacts != null ? !artifacts.equals(heatStructureTree.artifacts)
301         : heatStructureTree.artifacts != null) {
302       return false;
303     }
304     if (nested != null ? !nested.equals(heatStructureTree.nested)
305         : heatStructureTree.nested != null) {
306       return false;
307     }
308     if (errors != null ? !errors.equals(heatStructureTree.errors)
309         : heatStructureTree.errors != null) {
310       return false;
311     }
312
313     return true;
314   }
315
316   public List<ErrorMessage> getErrors() {
317     return errors;
318   }
319
320   public void setErrors(List<ErrorMessage> errors) {
321     this.errors = errors;
322   }
323
324   /**
325    * Add error to errors list.
326    *
327    * @param error the error
328    */
329   public void addErrorToErrorsList(ErrorMessage error) {
330     if (this.errors == null || this.errors.isEmpty()) {
331       this.errors = new ArrayList<>();
332     }
333     if (!this.errors.contains(error)) {
334       this.errors.add(error);
335     }
336   }
337
338   public Set<HeatStructureTree> getOther() {
339     return other;
340   }
341
342   public void setOther(Set<HeatStructureTree> other) {
343     this.other = other;
344   }
345
346   @Override
347   public int compareTo(HeatStructureTree obj) {
348     return obj.getFileName().compareTo(this.getFileName());
349   }
350 }