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