d9e053f4540587f987c04a025424a653ae50545e
[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.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import lombok.Data;
34 import lombok.EqualsAndHashCode;
35
36 import org.apache.commons.collections.MapUtils;
37 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
38
39 @EqualsAndHashCode(callSuper = false)
40 @Data
41 public class PropertyDataDefinition extends ToscaDataDefinition {
42
43     private String uniqueId;
44
45     // "boolean", "string", "float", "integer", "version" })
46     private String type;
47
48     private Boolean required = Boolean.FALSE;
49
50     protected boolean definition = false;
51
52     private String defaultValue;
53
54     private String description;
55
56     private SchemaDefinition schema;
57
58     private boolean password;
59
60     private String name;
61
62     private String value;
63
64     private String label;
65     protected Boolean hidden = Boolean.FALSE;
66     private Boolean immutable = Boolean.FALSE;
67
68     private String inputPath;
69     private String status;
70     private String inputId;
71     private String instanceUniqueId;
72     private String propertyId;
73     private String parentPropertyType;
74     private String subPropertyInputPath;
75
76     private List<Annotation> annotations;
77     /**
78      * The resource id which this property belongs to
79      */
80     private String parentUniqueId;
81
82     private List<GetInputValueDataDefinition> getInputValues;
83
84     private Boolean isDeclaredListInput = Boolean.FALSE;
85
86     private List<GetPolicyValueDataDefinition> getPolicyValues;
87
88     private List<String> propertyConstraints;
89     
90     private Map<String, String> metadata;
91
92     public PropertyDataDefinition() {
93         super();
94     }
95
96     public PropertyDataDefinition(Map<String, Object> pr) {
97         super(pr);
98     }
99
100     public PropertyDataDefinition(PropertyDataDefinition propertyDataDefinition) {
101         super();
102         this.setUniqueId(propertyDataDefinition.getUniqueId());
103         this.setRequired(propertyDataDefinition.isRequired());
104         this.setDefaultValue(propertyDataDefinition.getDefaultValue());
105         this.setDefinition(propertyDataDefinition.getDefinition());
106         this.setDescription(propertyDataDefinition.getDescription());
107         this.setSchema(propertyDataDefinition.getSchema());
108         this.setPassword(propertyDataDefinition.isPassword());
109         this.setType(propertyDataDefinition.getType());
110         this.setName(propertyDataDefinition.getName());
111         this.setValue(propertyDataDefinition.getValue());
112         this.setRequired(propertyDataDefinition.isRequired());
113         this.setHidden(propertyDataDefinition.isHidden());
114         this.setLabel(propertyDataDefinition.getLabel());
115         this.setImmutable(propertyDataDefinition.isImmutable());
116         this.setParentUniqueId(propertyDataDefinition.getParentUniqueId());
117         this.setOwnerId(propertyDataDefinition.getOwnerId());
118         this.setGetInputValues(propertyDataDefinition.getGetInputValues());
119         this.setGetPolicyValues(propertyDataDefinition.getGetPolicyValues());
120         this.setInputPath(propertyDataDefinition.getInputPath());
121         this.setStatus(propertyDataDefinition.getStatus());
122         this.setInputId(propertyDataDefinition.getInputId());
123         this.setInstanceUniqueId(propertyDataDefinition.getInstanceUniqueId());
124         this.setPropertyId(propertyDataDefinition.getPropertyId());
125         this.parentPropertyType = propertyDataDefinition.getParentPropertyType();
126         this.subPropertyInputPath = propertyDataDefinition.getSubPropertyInputPath();
127         if (isNotEmpty(propertyDataDefinition.annotations)) {
128             this.setAnnotations(propertyDataDefinition.annotations);
129         }
130         if (MapUtils.isNotEmpty(propertyDataDefinition.getMetadata())) {
131             setMetadata(new HashMap<>(propertyDataDefinition.getMetadata()));
132         }
133         if(isNotEmpty(propertyDataDefinition.getPropertyConstraints())){
134             setPropertyConstraints(new ArrayList<>(propertyDataDefinition.getPropertyConstraints()));
135         }
136         this.setIsDeclaredListInput(propertyDataDefinition.getIsDeclaredListInput());
137     }
138
139     // @Override
140     public boolean isDefinition() {
141         return true;
142     }
143
144     public boolean getDefinition() {
145         return definition;
146     }
147
148     public Boolean isRequired() {
149         return required;
150     }
151
152     public void setSchemaType(String schemaType) {
153         if (schema != null && schema.getProperty() != null) {
154             schema.getProperty().setType(schemaType);
155         }
156     }
157
158     public String getSchemaType() {
159         if (schema != null && schema.getProperty() != null) {
160             return schema.getProperty().getType();
161         }
162         return null;
163     }
164
165     public PropertyDataDefinition getSchemaProperty() {
166         if (schema != null) {
167             return schema.getProperty();
168         }
169
170         return null;
171     }
172
173     public Boolean isHidden() {
174         return hidden;
175     }
176
177     public Boolean isImmutable() {
178         return immutable;
179     }
180
181     public String getParentUniqueId() {
182         return getOwnerId();
183     }
184
185     public void setParentUniqueId(String parentUniqueId) {
186         setOwnerId(parentUniqueId);
187     }
188
189     public List<GetPolicyValueDataDefinition> safeGetGetPolicyValues() {
190         return CollectionUtils.isEmpty(getPolicyValues) ? new ArrayList<>() : getPolicyValues;
191     }
192
193     public boolean typeEquals(Object obj) {
194         if (this == obj) {
195             return true;
196         }
197         if (obj == null) {
198             return false;
199         }
200         if (getClass() != obj.getClass()) {
201             return false;
202         }
203         PropertyDataDefinition other = (PropertyDataDefinition) obj;
204         if (this.getType() == null) {
205             return other.getType() == null;
206         }
207         if (!this.type.equals(other.type)) {
208             return false;
209         }
210         String thisSchemaType = this.getSchemaType();
211         String otherSchemaType = other.getSchemaType();
212         if (thisSchemaType == null) {
213             return otherSchemaType == null;
214         }
215         return thisSchemaType.equals(otherSchemaType);
216     }
217
218     @Override
219     public Object getToscaPresentationValue(JsonPresentationFields field) {
220         switch (field) {
221             case NAME:
222                 return name;
223             case UNIQUE_ID:
224                 return uniqueId;
225             case PASSWORD:
226                 return password;
227             case TYPE:
228                 return type;
229             case DEFINITION:
230                 return definition;
231             case VALUE:
232                 return value;
233             case DEFAULT_VALUE:
234                 return defaultValue;
235             default:
236                 return super.getToscaPresentationValue(field);
237         }
238     }
239
240     @Override
241     public void setToscaPresentationValue(JsonPresentationFields name, Object value) {
242         switch (name) {
243             case NAME:
244                 setName((String) value);
245                 break;
246             case UNIQUE_ID:
247                 setUniqueId((String) value);
248                 break;
249             case PASSWORD:
250                 setPassword((Boolean) value);
251                 break;
252             case TYPE:
253                 setType((String) value);
254                 break;
255             case DEFINITION:
256                 setDefinition((Boolean) value);
257                 break;
258             case VALUE:
259                 setValue((String) value);
260                 break;
261             case DEFAULT_VALUE:
262                 setDefaultValue((String) value);
263                 break;
264             default:
265                 super.setToscaPresentationValue(name, value);
266                 break;
267         }
268     }
269
270     private <T extends ToscaDataDefinition> boolean compareSchemaType(T other) {
271         return !"list".equals(type) && !"map".equals(type) || this.getSchema().getProperty().getType().equals(((PropertyDataDefinition) other).getSchema().getProperty().getType());
272     }
273
274     @Override
275     public <T extends ToscaDataDefinition> T mergeFunction(T other, boolean allowDefaultValueOverride) {
276         if (this.getType() != null
277                 && this.getType().equals(other.getToscaPresentationValue(JsonPresentationFields.TYPE))
278                 && compareSchemaType(other)) {
279             other.setOwnerId(getOwnerId());
280             if (allowDefaultValueOverride
281                     && getDefaultValue() != null
282                     && !getDefaultValue().isEmpty()) {
283                 other.setToscaPresentationValue(JsonPresentationFields.DEFAULT_VALUE, getDefaultValue());
284             }
285             return other;
286         }
287         return null;
288     }
289
290     public void convertPropertyDataToInstancePropertyData() {
291         if (null != value) {
292             defaultValue = value;
293         }
294     }
295
296     public boolean isGetInputProperty() {
297         return this.getGetInputValues() != null && !this.getGetInputValues().isEmpty();
298     }
299
300     public void setAnnotations(List<Annotation> newAnnotations) {
301         Set<Annotation> annotationSet = isNotEmpty(newAnnotations) ? new HashSet<>(newAnnotations) : new HashSet<>();
302         //We would to prioritize the new valid annotations over the old ones if the same one existed.
303         if (this.annotations != null) {
304             annotationSet.addAll(this.annotations);
305         }
306
307         this.annotations = new ArrayList<>(annotationSet);
308         setToscaPresentationValue(JsonPresentationFields.ANNOTATIONS, this.annotations);
309     }
310
311     public List<Annotation> getAnnotations() {
312         return (List<Annotation>) getToscaPresentationValue(JsonPresentationFields.ANNOTATIONS);
313     }
314 }