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