Support of get_property in property assignment
[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     protected boolean definition = false;
44     protected 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
84     public PropertyDataDefinition() {
85         super();
86     }
87
88     public PropertyDataDefinition(Map<String, Object> pr) {
89         super(pr);
90     }
91
92     public PropertyDataDefinition(final PropertyDataDefinition propertyDataDefinition) {
93         super();
94         this.setUniqueId(propertyDataDefinition.getUniqueId());
95         this.setRequired(propertyDataDefinition.isRequired());
96         this.setDefaultValue(propertyDataDefinition.getDefaultValue());
97         this.setDefinition(propertyDataDefinition.getDefinition());
98         this.setDescription(propertyDataDefinition.getDescription());
99         if (propertyDataDefinition.getSchema() != null) {
100             this.setSchema(new SchemaDefinition(propertyDataDefinition.getSchema()));
101         }
102         this.setPassword(propertyDataDefinition.isPassword());
103         this.setType(propertyDataDefinition.getType());
104         this.setName(propertyDataDefinition.getName());
105         this.setValue(propertyDataDefinition.getValue());
106         this.setRequired(propertyDataDefinition.isRequired());
107         this.setHidden(propertyDataDefinition.isHidden());
108         this.setLabel(propertyDataDefinition.getLabel());
109         this.setImmutable(propertyDataDefinition.isImmutable());
110         this.setMappedToComponentProperty(propertyDataDefinition.isMappedToComponentProperty());
111         this.setParentUniqueId(propertyDataDefinition.getParentUniqueId());
112         this.setOwnerId(propertyDataDefinition.getOwnerId());
113         this.setGetInputValues(propertyDataDefinition.getGetInputValues());
114         this.setGetPolicyValues(propertyDataDefinition.getGetPolicyValues());
115         this.setInputPath(propertyDataDefinition.getInputPath());
116         this.setStatus(propertyDataDefinition.getStatus());
117         this.setInputId(propertyDataDefinition.getInputId());
118         this.setInstanceUniqueId(propertyDataDefinition.getInstanceUniqueId());
119         this.setModel(propertyDataDefinition.getModel());
120         this.setPropertyId(propertyDataDefinition.getPropertyId());
121         this.setToscaGetFunctionType(propertyDataDefinition.getToscaGetFunctionType());
122         this.setToscaGetFunction(propertyDataDefinition.getToscaGetFunction());
123         this.parentPropertyType = propertyDataDefinition.getParentPropertyType();
124         this.subPropertyInputPath = propertyDataDefinition.getSubPropertyInputPath();
125         if (isNotEmpty(propertyDataDefinition.annotations)) {
126             this.setAnnotations(propertyDataDefinition.annotations);
127         }
128         if (MapUtils.isNotEmpty(propertyDataDefinition.getMetadata())) {
129             setMetadata(new HashMap<>(propertyDataDefinition.getMetadata()));
130         }
131         if (isNotEmpty(propertyDataDefinition.getPropertyConstraints())) {
132             setPropertyConstraints(new ArrayList<>(propertyDataDefinition.getPropertyConstraints()));
133         }
134         this.setIsDeclaredListInput(propertyDataDefinition.getIsDeclaredListInput());
135     }
136
137     // @Override
138     public boolean isDefinition() {
139         return true;
140     }
141
142     public boolean getDefinition() {
143         return definition;
144     }
145
146     public Boolean isRequired() {
147         return required;
148     }
149
150     public void setSchemaType(String schemaType) {
151         if (schema != null && schema.getProperty() != null) {
152             schema.getProperty().setType(schemaType);
153         }
154     }
155
156     public String getSchemaType() {
157         if (schema != null && schema.getProperty() != null) {
158             return schema.getProperty().getType();
159         }
160         return null;
161     }
162
163     public PropertyDataDefinition getSchemaProperty() {
164         if (schema != null) {
165             return schema.getProperty();
166         }
167         return null;
168     }
169
170     public ToscaGetFunctionType getToscaGetFunctionType() {
171         if (toscaGetFunction != null) {
172             return toscaGetFunction.getFunctionType();
173         }
174         return toscaGetFunctionType;
175     }
176
177     public Boolean isHidden() {
178         return hidden;
179     }
180
181     public Boolean isImmutable() {
182         return immutable;
183     }
184
185     public Boolean isMappedToComponentProperty() {
186         return mappedToComponentProperty;
187     }
188
189     public String getParentUniqueId() {
190         return getOwnerId();
191     }
192
193     public void setParentUniqueId(String parentUniqueId) {
194         setOwnerId(parentUniqueId);
195     }
196
197     public List<GetPolicyValueDataDefinition> safeGetGetPolicyValues() {
198         return CollectionUtils.isEmpty(getPolicyValues) ? new ArrayList<>() : getPolicyValues;
199     }
200
201     public boolean typeEquals(Object obj) {
202         if (this == obj) {
203             return true;
204         }
205         if (obj == null) {
206             return false;
207         }
208         if (getClass() != obj.getClass()) {
209             return false;
210         }
211         PropertyDataDefinition other = (PropertyDataDefinition) obj;
212         if (this.getType() == null) {
213             return other.getType() == null;
214         }
215         if (!this.type.equals(other.type)) {
216             return false;
217         }
218         String thisSchemaType = this.getSchemaType();
219         String otherSchemaType = other.getSchemaType();
220         if (thisSchemaType == null) {
221             return otherSchemaType == null;
222         }
223         return thisSchemaType.equals(otherSchemaType);
224     }
225
226     @Override
227     public Object getToscaPresentationValue(JsonPresentationFields field) {
228         switch (field) {
229             case NAME:
230                 return name;
231             case UNIQUE_ID:
232                 return uniqueId;
233             case PASSWORD:
234                 return password;
235             case TYPE:
236                 return type;
237             case DEFINITION:
238                 return definition;
239             case VALUE:
240                 return value;
241             case DEFAULT_VALUE:
242                 return defaultValue;
243             default:
244                 return super.getToscaPresentationValue(field);
245         }
246     }
247
248     @Override
249     public void setToscaPresentationValue(JsonPresentationFields name, Object value) {
250         switch (name) {
251             case NAME:
252                 setName((String) value);
253                 break;
254             case UNIQUE_ID:
255                 setUniqueId((String) value);
256                 break;
257             case PASSWORD:
258                 setPassword((Boolean) value);
259                 break;
260             case TYPE:
261                 setType((String) value);
262                 break;
263             case DEFINITION:
264                 setDefinition((Boolean) value);
265                 break;
266             case VALUE:
267                 setValue((String) value);
268                 break;
269             case DEFAULT_VALUE:
270                 setDefaultValue((String) value);
271                 break;
272             default:
273                 super.setToscaPresentationValue(name, value);
274                 break;
275         }
276     }
277
278     private <T extends ToscaDataDefinition> boolean compareSchemaType(T other) {
279         return !"list".equals(type) && !"map".equals(type) || this.getSchema().getProperty().getType()
280             .equals(((PropertyDataDefinition) other).getSchema().getProperty().getType());
281     }
282
283     @Override
284     public <T extends ToscaDataDefinition> T mergeFunction(T other, boolean allowDefaultValueOverride) {
285         if (this.getType() != null && this.getType().equals(other.getToscaPresentationValue(JsonPresentationFields.TYPE)) && compareSchemaType(
286             other)) {
287             other.setOwnerId(getOwnerId());
288             if (allowDefaultValueOverride && getDefaultValue() != null && !getDefaultValue().isEmpty()) {
289                 other.setToscaPresentationValue(JsonPresentationFields.DEFAULT_VALUE, getDefaultValue());
290             }
291             return other;
292         }
293         return null;
294     }
295
296     public void convertPropertyDataToInstancePropertyData() {
297         if (null != value) {
298             defaultValue = value;
299         }
300     }
301
302     public boolean isGetInputProperty() {
303         return this.getGetInputValues() != null && !this.getGetInputValues().isEmpty();
304     }
305
306     public void setAnnotations(List<Annotation> newAnnotations) {
307         Set<Annotation> annotationSet = isNotEmpty(newAnnotations) ? new HashSet<>(newAnnotations) : new HashSet<>();
308         //We would to prioritize the new valid annotations over the old ones if the same one existed.
309         if (this.annotations != null) {
310             annotationSet.addAll(this.annotations);
311         }
312         this.annotations = new ArrayList<>(annotationSet);
313         setToscaPresentationValue(JsonPresentationFields.ANNOTATIONS, this.annotations);
314     }
315
316     public List<Annotation> getAnnotations() {
317         return (List<Annotation>) getToscaPresentationValue(JsonPresentationFields.ANNOTATIONS);
318     }
319
320     public boolean isGetFunction() {
321         return this.toscaGetFunctionType != null || this.toscaGetFunction != null;
322     }
323
324     public boolean hasGetFunction() {
325         return this.toscaGetFunction != null;
326     }
327
328 }