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