3345c959467b805a5a23f5e5c18e06d6c86e0700
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / CapabilityDataDefinition.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.be.datatypes.elements;
22
23 import com.google.common.collect.Lists;
24 import lombok.Getter;
25 import lombok.Setter;
26 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
27 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 /**
33  * Represents the capability of the component or component instance
34  */
35 public class CapabilityDataDefinition extends ToscaDataDefinition {
36
37     public static final String MIN_OCCURRENCES = "0";
38     public static final String MAX_OCCURRENCES = "UNBOUNDED";
39
40     @Getter
41     @Setter
42     private boolean external = false;
43
44     /**
45      * The default constructor initializing limits of the occurrences
46      */
47     public CapabilityDataDefinition() {
48         this.setMinOccurrences(MIN_OCCURRENCES);
49         this.setMaxOccurrences(MAX_OCCURRENCES);
50         this.setLeftOccurrences(MAX_OCCURRENCES);
51     }
52
53     /**
54      * Deep copy constructor.
55      *
56      * @param other the capability data definition to copy
57      */
58     public CapabilityDataDefinition(final CapabilityDataDefinition other) {
59         this.setUniqueId(other.getUniqueId());
60         this.setType(other.getType());
61         this.setDescription(other.getDescription());
62         this.setName(other.getName());
63         this.setParentName(other.getParentName());
64         this.setPreviousName(other.getPreviousName());
65
66         if (other.getValidSourceTypes() == null) {
67             this.setValidSourceTypes(Lists.newArrayList());
68         } else {
69             this.setValidSourceTypes(Lists.newArrayList(other.getValidSourceTypes()));
70         }
71
72         if (other.getCapabilitySources() == null) {
73             this.setCapabilitySources(Lists.newArrayList());
74         } else {
75             this.setCapabilitySources(Lists.newArrayList(other.getCapabilitySources()));
76         }
77
78         this.setOwnerId(other.getOwnerId());
79         this.setOwnerName(other.getOwnerName());
80         this.setOwnerType(other.getOwnerType());
81         this.setMinOccurrences(other.getMinOccurrences());
82         this.setMaxOccurrences(other.getMaxOccurrences());
83         this.setLeftOccurrences(other.getLeftOccurrences());
84
85         if (other.getPath() == null) {
86             this.setPath(Lists.newArrayList());
87         } else {
88             this.setPath(Lists.newArrayList(other.getPath()));
89         }
90
91         this.setSource(other.getSource());
92         this.setOwnerType(other.getOwnerType());
93         this.setExternal(other.isExternal());
94     }
95
96     public CapabilityDataDefinition(CapabilityTypeDataDefinition other) {
97         super();
98         this.setUniqueId(other.getUniqueId());
99         this.setType(other.getType());
100         this.setDescription(other.getDescription());
101         this.setOwnerId(other.getOwnerId());
102         if (other.getValidSourceTypes() == null) {
103             this.setValidSourceTypes(Lists.newArrayList());
104         } else {
105             this.setValidSourceTypes(Lists.newArrayList(other.getValidSourceTypes()));
106         }
107         this.setOwnerId(other.getOwnerId());
108     }
109
110     @Override
111     public String getOwnerId() {
112         return (String) getToscaPresentationValue(JsonPresentationFields.OWNER_ID);
113     }
114
115     @Override
116     public void setOwnerId(String ownerId) {
117         setToscaPresentationValue(JsonPresentationFields.OWNER_ID, ownerId);
118     }
119
120     public String getOwnerName() {
121         return (String) getToscaPresentationValue(JsonPresentationFields.OWNER_NAME);
122     }
123
124     public void setOwnerName(String ownerName) {
125         setToscaPresentationValue(JsonPresentationFields.OWNER_NAME, ownerName);
126     }
127
128     public OwnerType getOwnerType() {
129         return OwnerType.getByValue((String) getToscaPresentationValue(JsonPresentationFields.OWNER_TYPE));
130     }
131
132     public void setOwnerType(OwnerType ownerType) {
133         if (ownerType != null) {
134             setToscaPresentationValue(JsonPresentationFields.OWNER_TYPE, ownerType.getValue());
135         }
136     }
137
138     public String getMinOccurrences() {
139         return (String) getToscaPresentationValue(JsonPresentationFields.MIN_OCCURRENCES);
140     }
141
142     public void setMinOccurrences(String minOccurrences) {
143         setToscaPresentationValue(JsonPresentationFields.MIN_OCCURRENCES, minOccurrences);
144     }
145
146     public String getMaxOccurrences() {
147         return (String) getToscaPresentationValue(JsonPresentationFields.MAX_OCCURRENCES);
148     }
149
150     public void setMaxOccurrences(String maxOccurrences) {
151         setToscaPresentationValue(JsonPresentationFields.MAX_OCCURRENCES, maxOccurrences);
152     }
153
154     public String getLeftOccurrences() {
155         return (String) getToscaPresentationValue(JsonPresentationFields.LEFT_OCCURRENCES);
156     }
157
158     public void setLeftOccurrences(String leftOccurrences) {
159         setToscaPresentationValue(JsonPresentationFields.LEFT_OCCURRENCES, leftOccurrences);
160     }
161
162     public String getUniqueId() {
163         return (String) getToscaPresentationValue(JsonPresentationFields.UNIQUE_ID);
164     }
165
166     public void setUniqueId(String uniqueId) {
167         setToscaPresentationValue(JsonPresentationFields.UNIQUE_ID, uniqueId);
168     }
169
170     public String getDescription() {
171         return (String) getToscaPresentationValue(JsonPresentationFields.DESCRIPTION);
172     }
173
174     public void setDescription(String description) {
175         setToscaPresentationValue(JsonPresentationFields.DESCRIPTION, description);
176     }
177
178     public String getName() {
179         return (String) getToscaPresentationValue(JsonPresentationFields.NAME);
180     }
181
182     public void setName(String name) {
183         setToscaPresentationValue(JsonPresentationFields.NAME, name);
184     }
185
186     public String getParentName() {
187         return (String) getToscaPresentationValue(JsonPresentationFields.PARENT_NAME);
188     }
189
190     public void setParentName(String parentName) {
191         setToscaPresentationValue(JsonPresentationFields.PARENT_NAME, parentName);
192     }
193
194     public String getPreviousName() {
195         return (String) getToscaPresentationValue(JsonPresentationFields.PREVIOUS_NAME);
196     }
197
198     public void setPreviousName(String previousName) {
199         setToscaPresentationValue(JsonPresentationFields.PREVIOUS_NAME, previousName);
200     }
201
202     public String getType() {
203         return (String) getToscaPresentationValue(JsonPresentationFields.TYPE);
204     }
205
206     public void setType(String type) {
207         setToscaPresentationValue(JsonPresentationFields.TYPE, type);
208     }
209
210     @SuppressWarnings("unchecked")
211     public List<String> getValidSourceTypes() {
212         return (List<String>) getToscaPresentationValue(JsonPresentationFields.VALID_SOURCE_TYPE);
213     }
214
215     public void setValidSourceTypes(List<String> validSourceTypes) {
216         setToscaPresentationValue(JsonPresentationFields.VALID_SOURCE_TYPE, validSourceTypes);
217     }
218
219     @SuppressWarnings("unchecked")
220     public List<String> getCapabilitySources() {
221         return (List<String>) getToscaPresentationValue(JsonPresentationFields.CAPABILITY_SOURCES);
222     }
223
224     public void setCapabilitySources(List<String> capabilitySources) {
225         setToscaPresentationValue(JsonPresentationFields.CAPABILITY_SOURCES, capabilitySources);
226     }
227
228     public void setPath(List<String> path) {
229         setToscaPresentationValue(JsonPresentationFields.PATH, path);
230     }
231
232     @SuppressWarnings("unchecked")
233     public List<String> getPath() {
234         return (List<String>) getToscaPresentationValue(JsonPresentationFields.PATH);
235     }
236
237     public void setSource(String source) {
238         setToscaPresentationValue(JsonPresentationFields.SOURCE, source);
239     }
240
241     public String getSource() {
242         return (String) getToscaPresentationValue(JsonPresentationFields.SOURCE);
243     }
244
245     /**
246      * Adds the element to the path avoiding duplication
247      *
248      * @param elementInPath
249      */
250     public void addToPath(String elementInPath) {
251         List<String> path = getPath();
252         if (path == null) {
253             path = new ArrayList<>();
254         }
255         if (!path.contains(elementInPath)) {
256             path.add(elementInPath);
257         }
258         setPath(path);
259     }
260
261
262     @Override
263     public int hashCode() {
264         final int prime = 31;
265         int result = 1;
266
267         String uniqueId = this.getUniqueId();
268         String description = this.getDescription();
269         String name = this.getName();
270         String type = this.getType();
271         List<String> validSourceTypes = this.getValidSourceTypes();
272         List<String> capabilitySources = this.getCapabilitySources();
273         List<String> path = this.getPath();
274
275         String ownerId = this.getOwnerId();
276         String ownerName = this.getOwnerName();
277         String minOccurrences = this.getMinOccurrences();
278         String maxOccurrences = this.getMaxOccurrences();
279         String leftOccurrences = getLeftOccurrences();
280         String source = getSource();
281
282         result = prime * result + ((capabilitySources == null) ? 0 : capabilitySources.hashCode());
283         result = prime * result + ((description == null) ? 0 : description.hashCode());
284         result = prime * result + ((maxOccurrences == null) ? 0 : maxOccurrences.hashCode());
285         result = prime * result + ((minOccurrences == null) ? 0 : minOccurrences.hashCode());
286         result = prime * result + ((name == null) ? 0 : name.hashCode());
287         result = prime * result + ((ownerId == null) ? 0 : ownerId.hashCode());
288         result = prime * result + ((ownerName == null) ? 0 : ownerName.hashCode());
289
290         result = prime * result + ((type == null) ? 0 : type.hashCode());
291         result = prime * result + ((uniqueId == null) ? 0 : uniqueId.hashCode());
292         result = prime * result + ((validSourceTypes == null) ? 0 : validSourceTypes.hashCode());
293         result = prime * result + ((leftOccurrences == null) ? 0 : leftOccurrences.hashCode());
294         result = prime * result + ((path == null) ? 0 : path.hashCode());
295         result = prime * result + ((source == null) ? 0 : source.hashCode());
296         return result;
297     }
298
299     @Override
300     public boolean equals(Object obj) {
301
302         String uniqueId = this.getUniqueId();
303         String description = this.getDescription();
304         String name = this.getName();
305         String type = this.getType();
306         List<String> validSourceTypes = this.getValidSourceTypes();
307         List<String> capabilitySources = this.getCapabilitySources();
308         String ownerId = this.getOwnerId();
309         String ownerName = this.getOwnerName();
310         String minOccurrences = this.getMinOccurrences();
311         String maxOccurrences = this.getMaxOccurrences();
312         String leftOccurrences = getLeftOccurrences();
313         List<String> path = this.getPath();
314         String source = getSource();
315
316         if (this == obj) {
317             return true;
318         }
319         if (obj == null) {
320             return false;
321         }
322         if (getClass() != obj.getClass()) {
323             return false;
324         }
325         CapabilityDataDefinition other = (CapabilityDataDefinition) obj;
326         if (capabilitySources == null) {
327             if (other.getCapabilitySources() != null) {
328                 return false;
329             }
330         } else if (!capabilitySources.equals(other.getCapabilitySources())) {
331             return false;
332         }
333         if (description == null) {
334             if (other.getDescription() != null) {
335                 return false;
336             }
337         } else if (!description.equals(other.getDescription())) {
338             return false;
339         }
340         if (maxOccurrences == null) {
341             if (other.getMaxOccurrences() != null) {
342                 return false;
343             }
344         } else if (!maxOccurrences.equals(other.getMaxOccurrences())) {
345             return false;
346         }
347         if (minOccurrences == null) {
348             if (other.getMinOccurrences() != null) {
349                 return false;
350             }
351         } else if (!minOccurrences.equals(other.getMinOccurrences())) {
352             return false;
353         }
354         if (name == null) {
355             if (other.getName() != null) {
356                 return false;
357             }
358         } else if (!name.equals(other.getName())) {
359             return false;
360         }
361         if (ownerId == null) {
362             if (other.getOwnerId() != null) {
363                 return false;
364             }
365         } else if (!ownerId.equals(other.getOwnerId())) {
366             return false;
367         }
368         if (ownerName == null) {
369             if (other.getOwnerName() != null) {
370                 return false;
371             }
372         } else if (!ownerName.equals(other.getOwnerName())) {
373             return false;
374         }
375         if (type == null) {
376             if (other.getType() != null) {
377                 return false;
378             }
379         } else if (!type.equals(other.getType())) {
380             return false;
381         }
382         if (uniqueId == null) {
383             if (other.getUniqueId() != null) {
384                 return false;
385             }
386         } else if (!uniqueId.equals(other.getUniqueId())) {
387             return false;
388         }
389         if (validSourceTypes == null) {
390             if (other.getValidSourceTypes() != null) {
391                 return false;
392             }
393         } else if (!validSourceTypes.equals(other.getValidSourceTypes())) {
394             return false;
395         }
396         if (leftOccurrences == null) {
397             if (other.getLeftOccurrences() != null) {
398                 return false;
399             }
400         } else if (!leftOccurrences.equals(other.getLeftOccurrences())) {
401             return false;
402         }
403         if (path == null) {
404             if (other.getPath() != null) {
405                 return false;
406             }
407         } else if (!path.equals(other.getPath())) {
408             return false;
409         }
410         if (source == null) {
411             return other.getSource() == null;
412         } else {
413             return source.equals(other.getSource());
414         }
415     }
416
417     @Override
418     public String toString() {
419         String uniqueId = this.getUniqueId();
420         String description = this.getDescription();
421         String name = this.getName();
422         String type = this.getType();
423         List<String> validSourceTypes = this.getValidSourceTypes();
424         List<String> capabilitySources = this.getCapabilitySources();
425         List<String> path = this.getPath();
426         String ownerId = this.getOwnerId();
427         String ownerName = this.getOwnerName();
428         String minOccurrences = this.getMinOccurrences();
429         String maxOccurrences = this.getMaxOccurrences();
430         String source = this.getSource();
431
432         return "CapabilityDefinition [uniqueId=" + uniqueId + ", description=" + description + ", name=" + name
433             + ", type=" + type + ", validSourceTypes=" + validSourceTypes + ", capabilitySources="
434             + capabilitySources + ", ownerId=" + ownerId + ", ownerName=" + ownerName
435             + ", minOccurrences=" + minOccurrences + ", maxOccurrences=" + maxOccurrences + ", path=" + path + ", source=" + source
436             + ", external=" + external + "]";
437     }
438
439     public enum OwnerType {
440         GROUP("group"),
441         COMPONENT_INSTANCE("component instance"),
442         RESOURCE("resource");
443
444         private String value;
445
446         OwnerType(String value) {
447             this.value = value;
448         }
449
450         public String getValue() {
451             return value;
452         }
453
454         public static OwnerType getByValue(String value) {
455             for (OwnerType type : values()) {
456                 if (type.getValue().equals(value)) {
457                     return type;
458                 }
459             }
460             return null;
461         }
462
463     }
464
465 }