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