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