Change to enable SDC list type input
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / PropertyDataDefinition.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 static org.apache.commons.collections.CollectionUtils.isNotEmpty;
24
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Objects;
30 import java.util.Set;
31 import org.apache.commons.collections.CollectionUtils;
32 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
33 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
34
35 public class PropertyDataDefinition extends ToscaDataDefinition {
36
37         private String uniqueId;
38
39         // "boolean", "string", "float", "integer", "version" })
40         private String type;
41
42         private Boolean required = Boolean.FALSE;
43
44         protected boolean definition = false;
45
46         private String defaultValue;
47
48         private String description;
49
50         private SchemaDefinition schema;
51
52         private boolean password;
53
54         private String name;
55
56         private String value;
57
58         private String label;
59         protected Boolean hidden = Boolean.FALSE;
60         private Boolean immutable = Boolean.FALSE;
61
62         private String inputPath;
63         private String status;
64         private String inputId;
65         private String instanceUniqueId;
66         private String propertyId;
67     private String parentPropertyType;
68     private String subPropertyInputPath;
69
70         private List<Annotation> annotations;
71         /**
72          * The resource id which this property belongs to
73          */
74         private String parentUniqueId;
75
76         private List<GetInputValueDataDefinition> getInputValues;
77
78         private Boolean isDeclaredListInput = Boolean.FALSE;
79
80         private List<GetPolicyValueDataDefinition> getPolicyValues;
81
82         public PropertyDataDefinition() {
83                 super();
84         }
85
86         public PropertyDataDefinition(Map<String, Object> pr) {
87                 super(pr);
88
89         }
90
91         public PropertyDataDefinition(PropertyDataDefinition propertyDataDefinition) {
92                 super();
93                 this.setUniqueId(propertyDataDefinition.getUniqueId());
94                 this.setRequired(propertyDataDefinition.isRequired());
95                 this.setDefaultValue(propertyDataDefinition.getDefaultValue());
96                 this.setDescription(propertyDataDefinition.getDescription());
97                 this.setSchema(propertyDataDefinition.getSchema());
98                 this.setPassword(propertyDataDefinition.isPassword());
99                 this.setType(propertyDataDefinition.getType());
100                 this.setName(propertyDataDefinition.getName());
101                 this.setValue(propertyDataDefinition.getValue());
102                 this.setRequired(propertyDataDefinition.isRequired());
103                 this.setHidden(propertyDataDefinition.isHidden());
104                 this.setLabel(propertyDataDefinition.getLabel());
105                 this.setImmutable(propertyDataDefinition.isImmutable());
106                 this.setParentUniqueId(propertyDataDefinition.getParentUniqueId());
107                 this.setOwnerId(propertyDataDefinition.getOwnerId());
108                 this.setGetInputValues(propertyDataDefinition.getGetInputValues());
109                 this.setGetPolicyValues(propertyDataDefinition.getGetPolicyValues());
110                 this.setInputPath(propertyDataDefinition.getInputPath());
111                 this.setStatus(propertyDataDefinition.getStatus());
112                 this.setInputId(propertyDataDefinition.getInputId());
113                 this.setInstanceUniqueId(propertyDataDefinition.getInstanceUniqueId());
114                 this.setPropertyId(propertyDataDefinition.getPropertyId());
115                 this.parentPropertyType = propertyDataDefinition.getParentPropertyType();
116                 this.subPropertyInputPath = propertyDataDefinition.getSubPropertyInputPath();
117                 if(isNotEmpty(propertyDataDefinition.annotations)){
118                     this.setAnnotations(propertyDataDefinition.annotations);
119         }
120                 this.setIsDeclaredListInput(propertyDataDefinition.getIsDeclaredListInput());
121         }
122
123         public String getParentPropertyType() {
124             return parentPropertyType;
125         }
126
127         public void setParentPropertyType(String parentPropertyType) {
128             this.parentPropertyType = parentPropertyType;
129         }
130
131         public String getSubPropertyInputPath() {
132             return subPropertyInputPath;
133         }
134
135         public void setSubPropertyInputPath(String subPropertyInputPath) {
136             this.subPropertyInputPath = subPropertyInputPath;
137         }
138
139         public String getInputPath() {
140                 return inputPath;
141         }
142
143         public void setInputPath(String inputPath) {
144                 this.inputPath = inputPath;
145         }
146
147         public String getName() {
148                 return name;
149         }
150
151         public void setName(String name) {
152                 this.name = name;
153         }
154
155         public String getValue() {
156                 return value;
157         }
158
159         public void setValue(String value) {
160                 this.value = value;
161         }
162
163         // @Override
164         public boolean isDefinition() {
165                 return true;
166         }
167
168         public void setDefinition(boolean definition) {
169                 this.definition = definition;
170         }
171
172         public String getType() {
173                 return type;
174         }
175
176         public String getDefaultValue() {
177                 return defaultValue;
178         }
179
180         public void setDefaultValue(String defaultValue) {
181                 this.defaultValue = defaultValue;
182         }
183
184         public void setType(String type) {
185                 this.type = type;
186         }
187
188         public Boolean isRequired() {
189                 return required;
190         }
191
192         public void setRequired(Boolean required) {
193                 this.required = required;
194         }
195
196         public String getDescription() {
197                 return description;
198         }
199
200         public void setDescription(String description) {
201                 this.description = description;
202         }
203
204         public boolean isPassword() {
205                 return password;
206         }
207
208         public void setPassword(boolean password) {
209                 this.password = password;
210         }
211
212         public String getUniqueId() {
213                 return uniqueId;
214         }
215
216         public void setUniqueId(String uniqueId) {
217                 this.uniqueId = uniqueId;
218         }
219
220         public SchemaDefinition getSchema() {
221                 return schema;
222         }
223
224         public void setSchema(SchemaDefinition entrySchema) {
225                 this.schema = entrySchema;
226         }
227
228         public String getSchemaType() {
229                 if (schema != null && schema.getProperty() != null) {
230                         return schema.getProperty().getType();
231                 }
232                 return null;
233         }
234         
235         public PropertyDataDefinition getSchemaProperty() {
236             if (schema != null) {
237                 return schema.getProperty();
238             }
239             
240             return null;
241         }
242
243         public String getLabel() {
244                 return label;
245         }
246
247         public void setLabel(String label) {
248                 this.label = label;
249         }
250
251         public Boolean isHidden() {
252                 return hidden;
253         }
254
255         public void setHidden(Boolean hidden) {
256                 this.hidden = hidden;
257         }
258
259         public Boolean isImmutable() {
260                 return immutable;
261         }
262
263         public void setImmutable(Boolean immutable) {
264                 this.immutable = immutable;
265         }
266
267         public String getParentUniqueId() {
268                 return getOwnerId();
269         }
270
271         public void setParentUniqueId(String parentUniqueId) {
272                 setOwnerId(parentUniqueId);
273         }
274
275         public List<GetInputValueDataDefinition> getGetInputValues() {
276                 return getInputValues;
277         }
278
279         public void setGetInputValues(List<GetInputValueDataDefinition> getInputValues) {
280                 this.getInputValues = getInputValues;
281         }
282
283         public List<GetPolicyValueDataDefinition> safeGetGetPolicyValues() {
284                 return CollectionUtils.isEmpty(getPolicyValues) ? new ArrayList<>() : getPolicyValues;
285         }
286
287         public List<GetPolicyValueDataDefinition> getGetPolicyValues() {
288                 return getPolicyValues;
289         }
290
291         public void setGetPolicyValues(List<GetPolicyValueDataDefinition> getPolicyValues) {
292                 this.getPolicyValues = getPolicyValues;
293         }
294
295         public String getStatus() {
296                 return status;
297         }
298
299         public void setStatus(String status) {
300                 this.status = status;
301         }
302
303         public String getInputId() {
304                 return inputId;
305         }
306
307         public void setInputId(String inputId) {
308                 this.inputId = inputId;
309         }
310
311         public String getInstanceUniqueId() {
312                 return instanceUniqueId;
313         }
314
315         public void setInstanceUniqueId(String instanceUniqueId) {
316                 this.instanceUniqueId = instanceUniqueId;
317         }
318
319         public String getPropertyId() {
320                 return propertyId;
321         }
322
323         public void setPropertyId(String propertyId) {
324                 this.propertyId = propertyId;
325         }
326
327         public Boolean getIsDeclaredListInput() {
328                 return isDeclaredListInput;
329         }
330
331         public void setIsDeclaredListInput(Boolean isDeclaredListInput) {
332                 this.isDeclaredListInput = isDeclaredListInput;
333         }
334
335         @Override
336         public String toString() {
337                 return "PropertyDataDefinition [uniqueId=" + uniqueId + ", type=" + type + ", required=" + required + ", definition=" + definition + ", defaultValue=" + defaultValue + ", description=" + description + ", schema=" + schema + ", password="
338                                 + password + ", name=" + name + ", value=" + value + ", label=" + label + ", hidden=" + hidden + ", immutable=" + immutable + ", inputPath=" + inputPath + ", status=" + status + ", inputId=" + inputId + ", instanceUniqueId="
339                                 + instanceUniqueId + ", propertyId=" + propertyId + ", parentUniqueId=" + parentUniqueId + ", getInputValues=" + getInputValues + ", isDeclaredListInput=" + isDeclaredListInput
340                                 +  ", parentPropertyType=" + parentPropertyType
341                                 +  ", subPropertyInputPath=" + subPropertyInputPath +"]";
342         }
343
344         @Override
345         public int hashCode() {
346                 final int prime = 31;
347                 int result = 1;
348                 result = prime * result + ((defaultValue == null) ? 0 : defaultValue.hashCode());
349                 result = prime * result + (definition ? 1231 : 1237);
350                 result = prime * result + ((description == null) ? 0 : description.hashCode());
351                 result = prime * result + (password ? 1231 : 1237);
352                 result = prime * result + ((required == null) ? 0 : required.hashCode());
353                 result = prime * result + ((schema == null) ? 0 : schema.hashCode());
354                 result = prime * result + ((type == null) ? 0 : type.hashCode());
355                 result = prime * result + ((uniqueId == null) ? 0 : uniqueId.hashCode());
356                 result = prime * result + ((parentUniqueId == null) ? 0 : parentUniqueId.hashCode());
357                 result = prime * result + ((status == null) ? 0 : status.hashCode());
358                 result = prime * result + ((isDeclaredListInput == null) ? 0 : isDeclaredListInput.hashCode());
359                 return result;
360         }
361
362         public boolean typeEquals(Object obj) {
363                 if (this == obj) {
364             return true;
365         }
366                 if (obj == null) {
367             return false;
368         }
369                 if (getClass() != obj.getClass()) {
370             return false;
371         }
372                 PropertyDataDefinition other = (PropertyDataDefinition) obj;
373                 if (this.getType() == null) {
374             return other.getType() == null;
375         }
376                 if (!this.type.equals(other.type)) {
377                         return false;
378                 }
379                 String thisSchemaType = this.getSchemaType();
380                 String otherSchemaType = other.getSchemaType();
381                 if (thisSchemaType == null) {
382                         return otherSchemaType == null;
383                 }
384                 return thisSchemaType.equals(otherSchemaType);
385         }
386
387         @Override
388         public boolean equals(Object obj) {
389                 if (this == obj) {
390             return true;
391         }
392                 if (obj == null) {
393             return false;
394         }
395                 if (getClass() != obj.getClass()) {
396             return false;
397         }
398                 PropertyDataDefinition other = (PropertyDataDefinition) obj;
399                 if (defaultValue == null) {
400                         if (other.defaultValue != null) {
401                 return false;
402             }
403                 } else if (!defaultValue.equals(other.defaultValue)) {
404             return false;
405         }
406                 if (definition != other.definition) {
407             return false;
408         }
409                 if (description == null) {
410                         if (other.description != null) {
411                 return false;
412             }
413                 } else if (!description.equals(other.description)) {
414             return false;
415         }
416                 if (password != other.password) {
417             return false;
418         }
419                 if (required == null) {
420                         if (other.required != null) {
421                 return false;
422             }
423                 } else if (!required.equals(other.required)) {
424             return false;
425         }
426                 if (schema == null) {
427                         if (other.schema != null) {
428                 return false;
429             }
430                 } else if (!schema.equals(other.schema)) {
431             return false;
432         }
433                 if (type == null) {
434                         if (other.type != null) {
435                 return false;
436             }
437                 } else if (!type.equals(other.type)) {
438             return false;
439         }
440                 if (parentUniqueId == null) {
441                         if (other.parentUniqueId != null) {
442                 return false;
443             }
444                 } else if (!parentUniqueId.equals(other.parentUniqueId)) {
445             return false;
446         }
447                 if (uniqueId == null) {
448                         if (other.uniqueId != null) {
449                 return false;
450             }
451                 } else if (!uniqueId.equals(other.uniqueId)) {
452             return false;
453         }
454                 if (status == null) {
455                         if (other.status != null) {
456                 return false;
457             }
458                 } else if (!status.equals(other.status)) {
459             return false;
460         }
461                 if (isDeclaredListInput == null) {
462                         if (other.isDeclaredListInput != null) {
463                                 return false;
464                         }
465                 } else if (!isDeclaredListInput.equals(other.isDeclaredListInput)) {
466                         return false;
467                 }
468                 return true;
469         }
470
471         @Override
472         public Object getToscaPresentationValue(JsonPresentationFields field) {
473                 switch (field) {
474                 case NAME:
475                         return name;
476                 case UNIQUE_ID:
477                         return uniqueId;
478                 case PASSWORD:
479                         return password;
480                 case TYPE:
481                         return type;
482                 case DEFINITION:
483                         return definition;
484                 case VALUE:
485                         return value;
486                 case DEFAULT_VALUE:
487                         return defaultValue;
488                 default:
489                         return super.getToscaPresentationValue(field);
490                 }
491         }
492
493         @Override
494         public void setToscaPresentationValue(JsonPresentationFields name, Object value) {
495         switch (name) {
496             case NAME:
497                 setName((String)value);
498                 break;
499             case UNIQUE_ID:
500                 setUniqueId((String)value);
501                 break;
502             case PASSWORD:
503                 setPassword((Boolean)value);
504                 break;
505             case TYPE:
506                 setType((String)value);
507                 break;
508             case DEFINITION:
509                 setDefinition((Boolean)value);
510                 break;
511             case VALUE:
512                 setValue((String)value);
513                 break;
514             case DEFAULT_VALUE:
515                 setDefaultValue((String)value);
516                 break;
517             default:
518                 super.setToscaPresentationValue(name, value);
519                 break;
520         }
521     }
522
523         private <T extends ToscaDataDefinition> boolean compareSchemaType(T other) {
524                 return !"list".equals(type) && !"map".equals(type) || this.getSchema().getProperty().getType().equals(((PropertyDataDefinition) other).getSchema().getProperty().getType());
525         }
526
527         @Override
528         public <T extends ToscaDataDefinition> T mergeFunction(T other, boolean allowDefaultValueOverride) {
529                 if (this.getType() != null && 
530                                 this.getType().equals(other.getToscaPresentationValue(JsonPresentationFields.TYPE)) && 
531                                 compareSchemaType(other)) {
532                         other.setOwnerId(getOwnerId());
533                         if (allowDefaultValueOverride && getDefaultValue() != null && !getDefaultValue().isEmpty()) {
534                 other.setToscaPresentationValue(JsonPresentationFields.DEFAULT_VALUE, getDefaultValue());
535                         }
536                         return other;
537                 }
538                 return null;
539         }
540
541         public void convertPropertyDataToInstancePropertyData() {
542                 if (null != value) {
543             defaultValue = value;
544         }
545         }
546
547         public boolean isGetInputProperty() {
548                 return this.getGetInputValues() != null && !this.getGetInputValues().isEmpty();
549         }
550
551         public void setAnnotations(List<Annotation> newAnnotations) {
552                 Set<Annotation> annotationSet = isNotEmpty(newAnnotations) ? new HashSet<>(newAnnotations) : new HashSet<>();
553                 //We would to prioritize the new valid annotations over the old ones if the same one existed.
554                 if(this.annotations != null){
555                         for(Annotation oldInputAnnotation : this.annotations)
556                                 annotationSet.add(oldInputAnnotation);
557                 }
558
559                 this.annotations = new ArrayList<>(annotationSet);
560                 setToscaPresentationValue(JsonPresentationFields.ANNOTATIONS, this.annotations);
561         }
562         
563         public List<Annotation> getAnnotations() {
564                 return (List<Annotation>) getToscaPresentationValue(JsonPresentationFields.ANNOTATIONS);
565         }
566
567 }