[sdc] update code of sdc
[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 java.io.Serializable;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.stream.Collectors;
28
29 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
30 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
31
32 import fj.data.Either;
33
34
35 public class PropertyDataDefinition extends ToscaDataDefinition implements Serializable {
36
37         /**
38          * 
39          */
40         private static final long serialVersionUID = 5798685557528432389L;
41
42         private String uniqueId;
43
44         // "boolean", "string", "float", "integer", "version" })
45         private String type;
46
47         private Boolean required = Boolean.FALSE;
48
49         protected boolean definition = false;
50
51         private String defaultValue;
52
53         private String description;
54
55         private SchemaDefinition schema;
56
57         private boolean password;
58
59         private String name;
60
61         private String value;
62
63         private String label;
64         protected Boolean hidden = Boolean.FALSE;
65         private Boolean immutable = Boolean.FALSE;
66         
67         private String inputPath;
68     private String status;
69
70         /**
71          * The resource id which this property belongs to
72          */
73         private String parentUniqueId;
74
75
76         private List<GetInputValueDataDefinition> getInputValues;
77         
78         public PropertyDataDefinition() {
79                 super();
80         }
81
82         public PropertyDataDefinition(Map<String, Object> pr) {
83                 super(pr);
84
85         }
86
87         public PropertyDataDefinition(PropertyDataDefinition p) {
88
89                 super();
90                 this.setUniqueId(p.getUniqueId());
91                 this.setRequired(p.isRequired());
92                 this.setDefaultValue(p.getDefaultValue());
93                 this.setDescription(p.getDescription());
94                 this.setSchema(p.getSchema());
95                 this.setPassword(p.isPassword());
96                 this.setType(p.getType());
97                 this.setName(p.getName());
98                 this.setValue(p.getValue());
99                 this.setRequired(p.isRequired());
100                 this.setHidden(p.isHidden());
101                 this.setLabel(p.getLabel());
102                 this.setImmutable(p.isImmutable());
103                 this.setParentUniqueId(p.getParentUniqueId());
104                 this.setOwnerId(p.getOwnerId());
105                 this.setGetInputValues(p.getInputValues);
106                 this.setInputPath(p.getInputPath());
107                 this.setStatus(p.getStatus());
108
109         }
110
111         public String getInputPath() {
112                 return inputPath;
113         }
114
115         public void setInputPath(String inputPath) {
116                 this.inputPath = inputPath;
117         }
118
119         public String getName() {
120                 return name;
121         }
122
123         public void setName(String name) {
124                 this.name = name;
125         }
126
127         public String getValue() {
128                 return value;
129         }
130
131         public void setValue(String value) {
132                 this.value = value;
133         }
134
135         // @Override
136         public boolean isDefinition() {
137                 return true;
138         }
139
140         public void setDefinition(boolean definition) {
141                 this.definition = definition;
142         }
143
144         public String getType() {
145                 return type;
146         }
147
148         public String getDefaultValue() {
149                 return defaultValue;
150         }
151
152         public void setDefaultValue(String defaultValue) {
153                 this.defaultValue = defaultValue;
154         }
155
156         public void setType(String type) {
157                 this.type = type;
158         }
159
160         public Boolean isRequired() {
161                 return required;
162         }
163
164         public void setRequired(Boolean required) {
165                 this.required = required;
166         }
167
168         public String getDescription() {
169                 return description;
170         }
171
172         public void setDescription(String description) {
173                 this.description = description;
174         }
175
176         public boolean isPassword() {
177                 return password;
178         }
179
180         public void setPassword(boolean password) {
181                 this.password = password;
182         }
183
184         public String getUniqueId() {
185                 return uniqueId;
186         }
187
188         public void setUniqueId(String uniqueId) {
189                 this.uniqueId = uniqueId;
190         }
191
192         public SchemaDefinition getSchema() {
193                 return schema;
194         }
195
196         public void setSchema(SchemaDefinition entrySchema) {
197                 this.schema = entrySchema;
198         }
199
200         public String getLabel() {
201                 return label;
202         }
203
204         public void setLabel(String label) {
205                 this.label = label;
206         }
207
208         public Boolean isHidden() {
209                 return hidden;
210         }
211
212         public void setHidden(Boolean hidden) {
213                 this.hidden = hidden;
214         }
215
216         public Boolean isImmutable() {
217                 return immutable;
218         }
219
220         public void setImmutable(Boolean immutable) {
221                 this.immutable = immutable;
222         }
223         
224         
225         public String getParentUniqueId() {
226                 return getOwnerId();
227         }
228
229         public void setParentUniqueId(String parentUniqueId) {
230                 setOwnerId(parentUniqueId);
231         }
232
233
234         public List<GetInputValueDataDefinition> getGetInputValues() {
235                 return getInputValues;
236         }
237
238         public void setGetInputValues(List<GetInputValueDataDefinition> getInputValues) {
239                 this.getInputValues = getInputValues;
240         }
241         
242         public String getStatus() {
243                 return status;
244     }
245
246         public void setStatus(String status) {
247                 this.status = status;
248         }
249         
250         @Override
251         public String toString() {
252                 return "PropertyDataDefinition [uniqueId=" + uniqueId + ", type=" + type + ", required=" + required + ", defaultValue=" + defaultValue + ", description=" + description + ", entrySchema=" + schema + ", parentUniqueId=" + parentUniqueId + ", password=" + password + "]";
253         }
254
255         @Override
256         public int hashCode() {
257                 final int prime = 31;
258                 int result = 1;
259                 result = prime * result + ((defaultValue == null) ? 0 : defaultValue.hashCode());
260                 result = prime * result + (definition ? 1231 : 1237);
261                 result = prime * result + ((description == null) ? 0 : description.hashCode());
262                 result = prime * result + (password ? 1231 : 1237);
263                 result = prime * result + ((required == null) ? 0 : required.hashCode());
264                 result = prime * result + ((schema == null) ? 0 : schema.hashCode());
265                 result = prime * result + ((type == null) ? 0 : type.hashCode());
266                 result = prime * result + ((uniqueId == null) ? 0 : uniqueId.hashCode());
267                 result = prime * result + ((parentUniqueId == null) ? 0 : parentUniqueId.hashCode());
268                 result = prime * result + ((status == null) ? 0 : status.hashCode());
269                 return result;
270         }
271
272         @Override
273         public boolean equals(Object obj) {
274                 if (this == obj)
275                         return true;
276                 if (obj == null)
277                         return false;
278                 if (getClass() != obj.getClass())
279                         return false;
280                 PropertyDataDefinition other = (PropertyDataDefinition) obj;
281                 if (defaultValue == null) {
282                         if (other.defaultValue != null)
283                                 return false;
284                 } else if (!defaultValue.equals(other.defaultValue))
285                         return false;
286                 if (definition != other.definition)
287                         return false;
288                 if (description == null) {
289                         if (other.description != null)
290                                 return false;
291                 } else if (!description.equals(other.description))
292                         return false;
293                 if (password != other.password)
294                         return false;
295                 if (required == null) {
296                         if (other.required != null)
297                                 return false;
298                 } else if (!required.equals(other.required))
299                         return false;
300                 if (schema == null) {
301                         if (other.schema != null)
302                                 return false;
303                 } else if (!schema.equals(other.schema))
304                         return false;
305                 if (type == null) {
306                         if (other.type != null)
307                                 return false;
308                 } else if (!type.equals(other.type))
309                         return false;
310                 if (parentUniqueId == null) {
311                         if (other.parentUniqueId != null)
312                                 return false;
313                 } else if (!parentUniqueId.equals(other.parentUniqueId))
314                         return false;
315                 if (uniqueId == null) {
316                         if (other.uniqueId != null)
317                                 return false;
318                 } else if (!uniqueId.equals(other.uniqueId))
319                         return false;
320                 if (status == null) {
321                         if (other.status != null)
322                                 return false;
323                 } else if (!status.equals(other.status))
324                         return false;
325                 return true;
326         }
327
328         @Override
329         public Object getToscaPresentationValue(JsonPresentationFields field) {
330                 switch (field) {
331                 case NAME:
332                         return name;
333                 case UNIQUE_ID:
334                         return uniqueId;
335                 case PASSWORD:
336                         return password;
337                 case TYPE:
338                         return type;
339                 case DEFINITION:
340                         return definition;
341                 case VALUE:
342                         return value;
343                 case DEFAULT_VALUE:
344                         return defaultValue;
345                 default:
346                         return super.getToscaPresentationValue(field);
347                 }
348         }
349
350         private <T extends ToscaDataDefinition> boolean compareSchemaType(T other){
351                 return !"list".equals(type) && !"map".equals(type) || this.getSchema().getProperty().getType().equals(((PropertyDataDefinition)other).getSchema().getProperty().getType());
352         }
353         
354         
355         @Override
356         public <T extends ToscaDataDefinition> T mergeFunction(T other, boolean allowDefaultValueOverride){
357                 if(this.getType().equals(other.getToscaPresentationValue(JsonPresentationFields.TYPE)) && compareSchemaType(other)){
358                         other.setOwnerId(getOwnerId());
359                         if(allowDefaultValueOverride)
360                                 other.setToscaPresentationValue(JsonPresentationFields.DEFAULT_VALUE, getDefaultValue());
361                         return other;
362                 }
363                 return null;
364         }
365                 
366
367
368
369 }