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