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