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