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