f88514ac49d9856e95303dbb53288309e38578ee
[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()) {
177             if (toscaFunction != null) {
178                 return ((ToscaGetFunctionDataDefinition) toscaFunction).getFunctionType();
179             }
180             return toscaGetFunctionType;
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.toscaGetFunctionType != null || (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 }