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