Support TOSCA functions in sub properties
[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 com.fasterxml.jackson.annotation.JsonIgnoreProperties;
26 import java.util.ArrayList;
27 import java.util.Collection;
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 import org.apache.commons.collections.CollectionUtils;
36 import org.apache.commons.collections.MapUtils;
37 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
38 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
39 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
40
41 @EqualsAndHashCode(callSuper = false)
42 @Data
43 public class PropertyDataDefinition extends ToscaDataDefinition {
44
45     private boolean definition = false;
46     private Boolean hidden = Boolean.FALSE;
47     private String uniqueId;
48     // "boolean", "string", "float", "integer", "version" })
49     private String type;
50     private Boolean required = Boolean.FALSE;
51     private String defaultValue;
52     private String description;
53     private SchemaDefinition schema;
54     private boolean password;
55     private String name;
56     private String value;
57     private String label;
58     private Boolean immutable = Boolean.FALSE;
59     private Boolean mappedToComponentProperty = Boolean.TRUE;
60     /**
61      * @deprecated use {@link #toscaFunction} instead
62      */
63     @Deprecated
64     private ToscaGetFunctionType toscaGetFunctionType;
65     /**
66      * @deprecated use {@link #toscaFunction} instead
67      */
68     @Deprecated
69     private ToscaGetFunctionDataDefinition toscaGetFunction;
70     private ToscaFunction toscaFunction;
71     private Collection<SubPropertyToscaFunction> subPropertyToscaFunctions;
72
73     private String inputPath;
74     private String status;
75     private String inputId;
76     private String instanceUniqueId;
77     private String model;
78     private String propertyId;
79     private String parentPropertyType;
80     private String subPropertyInputPath;
81     private List<Annotation> annotations;
82     private List<GetInputValueDataDefinition> getInputValues;
83     private Boolean isDeclaredListInput = Boolean.FALSE;
84     private List<GetPolicyValueDataDefinition> getPolicyValues;
85     private List<String> propertyConstraints;
86     private Map<String, String> metadata;
87     private boolean userCreated;
88
89     public PropertyDataDefinition() {
90         super();
91     }
92
93     public PropertyDataDefinition(Map<String, Object> pr) {
94         super(pr);
95     }
96
97     public PropertyDataDefinition(final PropertyDataDefinition propertyDataDefinition) {
98         super();
99         this.setUniqueId(propertyDataDefinition.getUniqueId());
100         this.setRequired(propertyDataDefinition.isRequired());
101         this.setDefaultValue(propertyDataDefinition.getDefaultValue());
102         this.setDefinition(propertyDataDefinition.getDefinition());
103         this.setDescription(propertyDataDefinition.getDescription());
104         if (propertyDataDefinition.getSchema() != null) {
105             this.setSchema(new SchemaDefinition(propertyDataDefinition.getSchema()));
106         }
107         this.setPassword(propertyDataDefinition.isPassword());
108         this.setType(propertyDataDefinition.getType());
109         this.setName(propertyDataDefinition.getName());
110         this.setValue(propertyDataDefinition.getValue());
111         this.setRequired(propertyDataDefinition.isRequired());
112         this.setHidden(propertyDataDefinition.isHidden());
113         this.setLabel(propertyDataDefinition.getLabel());
114         this.setImmutable(propertyDataDefinition.isImmutable());
115         this.setMappedToComponentProperty(propertyDataDefinition.isMappedToComponentProperty());
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.setModel(propertyDataDefinition.getModel());
125         this.setPropertyId(propertyDataDefinition.getPropertyId());
126         this.setToscaGetFunction(propertyDataDefinition.getToscaGetFunction());
127         this.setToscaGetFunctionType(propertyDataDefinition.getToscaGetFunctionType());
128         this.setToscaFunction(propertyDataDefinition.getToscaFunction());
129         this.parentPropertyType = propertyDataDefinition.getParentPropertyType();
130         this.subPropertyInputPath = propertyDataDefinition.getSubPropertyInputPath();
131         this.subPropertyToscaFunctions = propertyDataDefinition.getSubPropertyToscaFunctions();
132         if (isNotEmpty(propertyDataDefinition.annotations)) {
133             this.setAnnotations(propertyDataDefinition.annotations);
134         }
135         if (MapUtils.isNotEmpty(propertyDataDefinition.getMetadata())) {
136             setMetadata(new HashMap<>(propertyDataDefinition.getMetadata()));
137         }
138         if (isNotEmpty(propertyDataDefinition.getPropertyConstraints())) {
139             setPropertyConstraints(new ArrayList<>(propertyDataDefinition.getPropertyConstraints()));
140         }
141         this.setIsDeclaredListInput(propertyDataDefinition.getIsDeclaredListInput());
142         this.setUserCreated(propertyDataDefinition.isUserCreated());
143     }
144
145     // @Override
146     public boolean isDefinition() {
147         return true;
148     }
149
150     public boolean getDefinition() {
151         return definition;
152     }
153
154     public Boolean isRequired() {
155         return required;
156     }
157
158     public void setSchemaType(String schemaType) {
159         if (schema != null && schema.getProperty() != null) {
160             schema.getProperty().setType(schemaType);
161         }
162     }
163
164     public String getSchemaType() {
165         if (schema != null && schema.getProperty() != null) {
166             return schema.getProperty().getType();
167         }
168         return null;
169     }
170
171     public PropertyDataDefinition getSchemaProperty() {
172         if (schema != null) {
173             return schema.getProperty();
174         }
175         return null;
176     }
177
178     public ToscaGetFunctionType getToscaGetFunctionType() {
179         if (isToscaGetFunction() && toscaFunction != null) {
180             return ((ToscaGetFunctionDataDefinition) toscaFunction).getFunctionType();
181         }
182
183         return null;
184     }
185
186     public Boolean isHidden() {
187         return hidden;
188     }
189
190     public Boolean isImmutable() {
191         return immutable;
192     }
193
194     public Boolean isMappedToComponentProperty() {
195         return mappedToComponentProperty;
196     }
197
198     /**
199      * The resource id which this property belongs to
200      */
201     public String getParentUniqueId() {
202         return getOwnerId();
203     }
204
205     public void setParentUniqueId(String parentUniqueId) {
206         setOwnerId(parentUniqueId);
207     }
208
209     public List<GetPolicyValueDataDefinition> safeGetGetPolicyValues() {
210         return CollectionUtils.isEmpty(getPolicyValues) ? new ArrayList<>() : getPolicyValues;
211     }
212
213     public boolean typeEquals(Object obj) {
214         if (this == obj) {
215             return true;
216         }
217         if (obj == null) {
218             return false;
219         }
220         if (getClass() != obj.getClass()) {
221             return false;
222         }
223         PropertyDataDefinition other = (PropertyDataDefinition) obj;
224         if (this.getType() == null) {
225             return other.getType() == null;
226         }
227         if (!this.type.equals(other.type)) {
228             return false;
229         }
230         String thisSchemaType = this.getSchemaType();
231         String otherSchemaType = other.getSchemaType();
232         if (thisSchemaType == null) {
233             return otherSchemaType == null;
234         }
235         return thisSchemaType.equals(otherSchemaType);
236     }
237
238     @Override
239     public Object getToscaPresentationValue(JsonPresentationFields field) {
240         switch (field) {
241             case NAME:
242                 return name;
243             case UNIQUE_ID:
244                 return uniqueId;
245             case PASSWORD:
246                 return password;
247             case TYPE:
248                 return type;
249             case DEFINITION:
250                 return definition;
251             case VALUE:
252                 return value;
253             case DEFAULT_VALUE:
254                 return defaultValue;
255             default:
256                 return super.getToscaPresentationValue(field);
257         }
258     }
259
260     @Override
261     public void setToscaPresentationValue(JsonPresentationFields name, Object value) {
262         switch (name) {
263             case NAME:
264                 setName((String) value);
265                 break;
266             case UNIQUE_ID:
267                 setUniqueId((String) value);
268                 break;
269             case PASSWORD:
270                 setPassword((Boolean) value);
271                 break;
272             case TYPE:
273                 setType((String) value);
274                 break;
275             case DEFINITION:
276                 setDefinition((Boolean) value);
277                 break;
278             case VALUE:
279                 setValue((String) value);
280                 break;
281             case DEFAULT_VALUE:
282                 setDefaultValue((String) value);
283                 break;
284             default:
285                 super.setToscaPresentationValue(name, value);
286                 break;
287         }
288     }
289
290     private <T extends ToscaDataDefinition> boolean compareSchemaType(T other) {
291         return !"list".equals(type) && !"map".equals(type) || this.getSchema().getProperty().getType()
292             .equals(((PropertyDataDefinition) other).getSchema().getProperty().getType());
293     }
294
295     @Override
296     public <T extends ToscaDataDefinition> T mergeFunction(T other, boolean allowDefaultValueOverride) {
297         if (this.getType() != null && this.getType().equals(other.getToscaPresentationValue(JsonPresentationFields.TYPE)) && compareSchemaType(
298             other)) {
299             other.setOwnerId(getOwnerId());
300             if (allowDefaultValueOverride && getDefaultValue() != null && !getDefaultValue().isEmpty()) {
301                 other.setToscaPresentationValue(JsonPresentationFields.DEFAULT_VALUE, getDefaultValue());
302             }
303             return other;
304         }
305         return null;
306     }
307
308     public void convertPropertyDataToInstancePropertyData() {
309         if (null != value) {
310             defaultValue = value;
311         }
312     }
313
314     public boolean isGetInputProperty() {
315         return this.getGetInputValues() != null && !this.getGetInputValues().isEmpty();
316     }
317
318     public void setAnnotations(List<Annotation> newAnnotations) {
319         Set<Annotation> annotationSet = isNotEmpty(newAnnotations) ? new HashSet<>(newAnnotations) : new HashSet<>();
320         //We would to prioritize the new valid annotations over the old ones if the same one existed.
321         if (this.annotations != null) {
322             annotationSet.addAll(this.annotations);
323         }
324         this.annotations = new ArrayList<>(annotationSet);
325         setToscaPresentationValue(JsonPresentationFields.ANNOTATIONS, this.annotations);
326     }
327
328     public List<Annotation> getAnnotations() {
329         return (List<Annotation>) getToscaPresentationValue(JsonPresentationFields.ANNOTATIONS);
330     }
331
332     @JsonIgnoreProperties
333     public boolean isToscaFunction() {
334         return this.toscaGetFunctionType != null || this.toscaFunction != null;
335     }
336
337
338     @JsonIgnoreProperties
339     public boolean isToscaGetFunction() {
340         return this.toscaFunction != null
341             && (this.toscaFunction.getType() == ToscaFunctionType.GET_ATTRIBUTE
342             || this.toscaFunction.getType() == ToscaFunctionType.GET_INPUT
343             || this.toscaFunction.getType() == ToscaFunctionType.GET_PROPERTY);
344     }
345
346     @JsonIgnoreProperties
347     public boolean hasToscaFunction() {
348         return this.toscaFunction != null;
349     }
350
351 }