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