93eb7c649d5676a9a8350f882ad31d7b92e830c2
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2021 Nokia
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.openecomp.sdc.heat.datatypes.structure;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.TreeSet;
29 import lombok.AccessLevel;
30 import lombok.Data;
31 import lombok.Getter;
32 import lombok.Setter;
33 import org.openecomp.sdc.datatypes.error.ErrorMessage;
34 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
35
36 @Data
37 public class HeatStructureTree implements Comparable<HeatStructureTree> {
38
39     private String fileName;
40     private FileData.Type type;
41
42     @Getter(AccessLevel.NONE)
43     @Setter(AccessLevel.NONE)
44     private Boolean isBase;
45
46     private HeatStructureTree env;
47     private List<ErrorMessage> errors;
48     private Set<HeatStructureTree> heat;
49     private Set<HeatStructureTree> volume;
50     private Set<HeatStructureTree> network;
51     private Set<HeatStructureTree> nested;
52     private Set<HeatStructureTree> other;
53     private Set<Artifact> artifacts;
54     private Set<HeatStructureTree> helm;
55
56     public HeatStructureTree() {
57         heat = new TreeSet<>();
58     }
59
60     public HeatStructureTree(String fileName, boolean isBase) {
61         this();
62         this.isBase = isBase;
63         this.fileName = fileName;
64     }
65
66     public Boolean getBase() {
67         return isBase;
68     }
69
70     public void setBase(Boolean isBase) {
71         this.isBase = isBase;
72     }
73
74     /**
75      * Gets heat structure tree by name.
76      *
77      * @param filesSet the files set
78      * @param filename the filename
79      * @return the heat structure tree by name
80      */
81     public static HeatStructureTree getHeatStructureTreeByName(Set<HeatStructureTree> filesSet,
82         String filename) {
83         for (HeatStructureTree heatStructureTree : filesSet) {
84             if (heatStructureTree.getFileName().equals(filename)) {
85                 return heatStructureTree;
86             }
87         }
88
89         return null;
90     }
91
92     /**
93      * Add heat structure tree to nested heat list.
94      *
95      * @param heatStructureTree the heat structure tree
96      */
97     public void addHeatStructureTreeToNestedHeatList(HeatStructureTree heatStructureTree) {
98         if (this.nested == null) {
99             this.nested = new TreeSet<>();
100         }
101         if (!findItemInSetByName(this.nested, heatStructureTree)) {
102             this.nested.add(heatStructureTree);
103         }
104     }
105
106     /**
107      * Add artifact to artifact list.
108      *
109      * @param artifact the artifact
110      */
111     public void addArtifactToArtifactList(Artifact artifact) {
112         if (this.artifacts == null || this.artifacts.isEmpty()) {
113             this.artifacts = new TreeSet<>();
114         }
115         this.artifacts.add(artifact);
116     }
117
118     /**
119      * Add network to network list.
120      *
121      * @param heatStructureTree the heat structure tree
122      */
123     public void addNetworkToNetworkList(HeatStructureTree heatStructureTree) {
124         if (this.network == null) {
125             this.network = new TreeSet<>();
126         }
127         if (!findItemInSetByName(this.network, heatStructureTree)) {
128             this.network.add(heatStructureTree);
129         }
130     }
131
132     /**
133      * Add volume file to volume list.
134      *
135      * @param heatStructureTree the heat structure tree
136      */
137     public void addVolumeFileToVolumeList(HeatStructureTree heatStructureTree) {
138         if (this.volume == null) {
139             this.volume = new TreeSet<>();
140         }
141         if (!findItemInSetByName(this.volume, heatStructureTree)) {
142             this.volume.add(heatStructureTree);
143         }
144     }
145
146     /**
147      * Add heat to heat list.
148      *
149      * @param heat the heat
150      */
151     public void addHeatToHeatList(HeatStructureTree heat) {
152         if (this.heat == null) {
153             this.heat = new TreeSet<>();
154         }
155
156         this.heat.add(heat);
157     }
158
159     /**
160      * Add other to other list.
161      *
162      * @param other the other
163      */
164     public void addOtherToOtherList(HeatStructureTree other) {
165         if (this.other == null) {
166             this.other = new TreeSet<>();
167         }
168
169         this.other.add(other);
170     }
171
172     public void addToHelmList(HeatStructureTree helm) {
173         if (this.helm == null) {
174             this.helm = new TreeSet<>();
175         }
176
177         this.helm.add(helm);
178     }
179
180     /**
181      * Find item in set by name boolean.
182      *
183      * @param searchSet the search set
184      * @param toFind    the to find
185      * @return the boolean
186      */
187     public boolean findItemInSetByName(Set<HeatStructureTree> searchSet, HeatStructureTree toFind) {
188         for (HeatStructureTree heatStructureTree : searchSet) {
189             if (heatStructureTree.getFileName().equals(toFind.getFileName())) {
190                 return true;
191             }
192         }
193         return false;
194     }
195
196     /**
197      * Remove from volume or network.
198      *
199      * @param fileNameToRemove the file name to remove
200      * @param type             the type
201      */
202     public void removeFromVolumeOrNetwork(String fileNameToRemove, FileData.Type type) {
203         Set<HeatStructureTree> volumeOrNetworkSet =
204             type.equals(FileData.Type.HEAT_VOL) ? this.volume : this.network;
205         HeatStructureTree toRemove = getHeatStructureTreeByName(volumeOrNetworkSet, fileNameToRemove);
206
207         volumeOrNetworkSet.remove(toRemove);
208     }
209
210     @Override
211     public int hashCode() {
212         int result1 = fileName != null ? fileName.hashCode() : 0;
213         result1 = 31 * result1 + (env != null ? env.hashCode() : 0);
214         result1 = 31 * result1 + (heat != null ? heat.hashCode() : 0);
215         result1 = 31 * result1 + (volume != null ? volume.hashCode() : 0);
216         result1 = 31 * result1 + (network != null ? network.hashCode() : 0);
217         result1 = 31 * result1 + (artifacts != null ? artifacts.hashCode() : 0);
218         result1 = 31 * result1 + (nested != null ? nested.hashCode() : 0);
219         result1 = 31 * result1 + (errors != null ? errors.hashCode() : 0);
220
221         return result1;
222     }
223
224     @Override
225     public boolean equals(Object other) {
226         if (this == other) {
227             return true;
228         }
229         if (other == null || getClass() != other.getClass()) {
230             return false;
231         }
232
233         HeatStructureTree heatStructureTree = (HeatStructureTree) other;
234
235         if (fileName != null ? !fileName.equals(heatStructureTree.fileName)
236             : heatStructureTree.fileName != null) {
237             return false;
238         }
239         if (env != null ? !env.equals(heatStructureTree.env) : heatStructureTree.env != null) {
240             return false;
241         }
242         if (heat != null ? !heat.equals(heatStructureTree.heat) : heatStructureTree.heat != null) {
243             return false;
244         }
245         if (volume != null ? !volume.equals(heatStructureTree.volume)
246             : heatStructureTree.volume != null) {
247             return false;
248         }
249         if (network != null ? !network.equals(heatStructureTree.network)
250             : heatStructureTree.network != null) {
251             return false;
252         }
253         if (artifacts != null ? !artifacts.equals(heatStructureTree.artifacts)
254             : heatStructureTree.artifacts != null) {
255             return false;
256         }
257         if (nested != null ? !nested.equals(heatStructureTree.nested)
258             : heatStructureTree.nested != null) {
259             return false;
260         }
261
262         return errors != null ? errors.equals(heatStructureTree.errors) : heatStructureTree.errors == null;
263     }
264
265     /**
266      * Add error to errors list.
267      *
268      * @param error the error
269      */
270     public void addErrorToErrorsList(ErrorMessage error) {
271         if (this.errors == null || this.errors.isEmpty()) {
272             this.errors = new ArrayList<>();
273         }
274         if (!this.errors.contains(error)) {
275             this.errors.add(error);
276         }
277     }
278
279     @Override
280     public int compareTo(HeatStructureTree obj) {
281         return obj.getFileName().compareTo(this.getFileName());
282     }
283 }