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