a65db6f2ea8b6de9c12165496e7e09db53f657b6
[sdc.git] /
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 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.TreeSet;
27
28 import lombok.AccessLevel;
29 import lombok.Data;
30 import lombok.Getter;
31 import lombok.Setter;
32 import org.openecomp.sdc.datatypes.error.ErrorMessage;
33 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
34
35 @Data
36 public class HeatStructureTree implements Comparable<HeatStructureTree> {
37
38     private String fileName;
39     private FileData.Type type;
40
41     @Getter(AccessLevel.NONE)
42     @Setter(AccessLevel.NONE)
43     private Boolean isBase;
44
45     private HeatStructureTree env;
46     private List<ErrorMessage> errors;
47     private Set<HeatStructureTree> heat;
48     private Set<HeatStructureTree> volume;
49     private Set<HeatStructureTree> network;
50     private Set<HeatStructureTree> nested;
51     private Set<HeatStructureTree> other;
52     private Set<Artifact> artifacts;
53     private Set<HeatStructureTree> helm;
54
55     public HeatStructureTree() {
56     }
57
58     public HeatStructureTree(String fileName, boolean isBase) {
59         this.isBase = isBase;
60         this.fileName = fileName;
61     }
62
63     public Boolean getBase() {
64         return isBase;
65     }
66
67     public void setBase(Boolean isBase) {
68         this.isBase = isBase;
69     }
70
71     /**
72      * Gets heat structure tree by name.
73      *
74      * @param filesSet the files set
75      * @param filename the filename
76      * @return the heat structure tree by name
77      */
78     public static HeatStructureTree getHeatStructureTreeByName(Set<HeatStructureTree> filesSet,
79                                                                String filename) {
80         for (HeatStructureTree heatStructureTree : filesSet) {
81             if (heatStructureTree.getFileName().equals(filename)) {
82                 return heatStructureTree;
83             }
84         }
85
86         return null;
87     }
88
89     /**
90      * Add heat structure tree to nested heat list.
91      *
92      * @param heatStructureTree the heat structure tree
93      */
94     public void addHeatStructureTreeToNestedHeatList(HeatStructureTree heatStructureTree) {
95         if (this.nested == null) {
96             this.nested = new TreeSet<>();
97         }
98         if (!findItemInSetByName(this.nested, heatStructureTree)) {
99             this.nested.add(heatStructureTree);
100         }
101     }
102
103     /**
104      * Add artifact to artifact list.
105      *
106      * @param artifact the artifact
107      */
108     public void addArtifactToArtifactList(Artifact artifact) {
109         if (this.artifacts == null || this.artifacts.isEmpty()) {
110             this.artifacts = new TreeSet<>();
111         }
112         this.artifacts.add(artifact);
113     }
114
115     /**
116      * Add network to network list.
117      *
118      * @param heatStructureTree the heat structure tree
119      */
120     public void addNetworkToNetworkList(HeatStructureTree heatStructureTree) {
121         if (this.network == null) {
122             this.network = new TreeSet<>();
123         }
124         if (!findItemInSetByName(this.network, heatStructureTree)) {
125             this.network.add(heatStructureTree);
126         }
127     }
128
129     /**
130      * Add volume file to volume list.
131      *
132      * @param heatStructureTree the heat structure tree
133      */
134     public void addVolumeFileToVolumeList(HeatStructureTree heatStructureTree) {
135         if (this.volume == null) {
136             this.volume = new TreeSet<>();
137         }
138         if (!findItemInSetByName(this.volume, heatStructureTree)) {
139             this.volume.add(heatStructureTree);
140         }
141     }
142
143     /**
144      * Add heat to heat list.
145      *
146      * @param heat the heat
147      */
148     public void addHeatToHeatList(HeatStructureTree heat) {
149         if (this.heat == null) {
150             this.heat = new TreeSet<>();
151         }
152
153         this.heat.add(heat);
154     }
155
156     /**
157      * Add other to other list.
158      *
159      * @param other the other
160      */
161     public void addOtherToOtherList(HeatStructureTree other) {
162         if (this.other == null) {
163             this.other = new TreeSet<>();
164         }
165
166         this.other.add(other);
167     }
168
169     public void addToHelmList(HeatStructureTree helm){
170         if (this.helm == null) {
171             this.helm = new TreeSet<>();
172         }
173
174         this.helm.add(helm);
175     }
176
177     /**
178      * Find item in set by name boolean.
179      *
180      * @param searchSet the search set
181      * @param toFind    the to find
182      * @return the boolean
183      */
184     public boolean findItemInSetByName(Set<HeatStructureTree> searchSet, HeatStructureTree toFind) {
185         for (HeatStructureTree heatStructureTree : searchSet) {
186             if (heatStructureTree.getFileName().equals(toFind.getFileName())) {
187                 return true;
188             }
189         }
190         return false;
191     }
192
193     /**
194      * Remove from volume or network.
195      *
196      * @param fileNameToRemove the file name to remove
197      * @param type             the type
198      */
199     public void removeFromVolumeOrNetwork(String fileNameToRemove, FileData.Type type) {
200         Set<HeatStructureTree> volumeOrNetworkSet =
201                 type.equals(FileData.Type.HEAT_VOL) ? this.volume : this.network;
202         HeatStructureTree toRemove = getHeatStructureTreeByName(volumeOrNetworkSet, fileNameToRemove);
203
204         volumeOrNetworkSet.remove(toRemove);
205     }
206
207     @Override
208     public int hashCode() {
209         int result1 = fileName != null ? fileName.hashCode() : 0;
210         result1 = 31 * result1 + (env != null ? env.hashCode() : 0);
211         result1 = 31 * result1 + (heat != null ? heat.hashCode() : 0);
212         result1 = 31 * result1 + (volume != null ? volume.hashCode() : 0);
213         result1 = 31 * result1 + (network != null ? network.hashCode() : 0);
214         result1 = 31 * result1 + (artifacts != null ? artifacts.hashCode() : 0);
215         result1 = 31 * result1 + (nested != null ? nested.hashCode() : 0);
216         result1 = 31 * result1 + (errors != null ? errors.hashCode() : 0);
217
218
219         return result1;
220     }
221
222     @Override
223     public boolean equals(Object other) {
224         if (this == other) {
225             return true;
226         }
227         if (other == null || getClass() != other.getClass()) {
228             return false;
229         }
230
231         HeatStructureTree heatStructureTree = (HeatStructureTree) other;
232
233         if (fileName != null ? !fileName.equals(heatStructureTree.fileName)
234                 : heatStructureTree.fileName != null) {
235             return false;
236         }
237         if (env != null ? !env.equals(heatStructureTree.env) : heatStructureTree.env != null) {
238             return false;
239         }
240         if (heat != null ? !heat.equals(heatStructureTree.heat) : heatStructureTree.heat != null) {
241             return false;
242         }
243         if (volume != null ? !volume.equals(heatStructureTree.volume)
244                 : heatStructureTree.volume != null) {
245             return false;
246         }
247         if (network != null ? !network.equals(heatStructureTree.network)
248                 : heatStructureTree.network != null) {
249             return false;
250         }
251         if (artifacts != null ? !artifacts.equals(heatStructureTree.artifacts)
252                 : heatStructureTree.artifacts != null) {
253             return false;
254         }
255         if (nested != null ? !nested.equals(heatStructureTree.nested)
256                 : heatStructureTree.nested != null) {
257             return false;
258         }
259
260         return errors != null ? errors.equals(heatStructureTree.errors) : heatStructureTree.errors == null;
261     }
262
263     /**
264      * Add error to errors list.
265      *
266      * @param error the error
267      */
268     public void addErrorToErrorsList(ErrorMessage error) {
269         if (this.errors == null || this.errors.isEmpty()) {
270             this.errors = new ArrayList<>();
271         }
272         if (!this.errors.contains(error)) {
273             this.errors.add(error);
274         }
275     }
276
277     @Override
278     public int compareTo(HeatStructureTree obj) {
279         return obj.getFileName().compareTo(this.getFileName());
280     }
281 }