[SDC-29] catalog 1707 rebase commit.
[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.List;
25 import java.util.Map;
26 import java.util.stream.Collectors;
27
28 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
29 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
30
31 import fj.data.Either;
32
33
34 public class PropertyDataDefinition extends ToscaDataDefinition implements Serializable {
35
36         /**
37          * 
38          */
39         private static final long serialVersionUID = 5798685557528432389L;
40
41         private String uniqueId;
42
43         // "boolean", "string", "float", "integer", "version" })
44         private String type;
45
46         private Boolean required = Boolean.FALSE;
47
48         protected boolean definition = false;
49
50         private String defaultValue;
51
52         private String description;
53
54         private SchemaDefinition schema;
55
56         private boolean password;
57
58         private String name;
59
60         private String value;
61
62         private String label;
63         private Boolean hidden = Boolean.FALSE;;
64         private Boolean immutable = Boolean.FALSE;
65         
66         private String inputPath;
67         
68
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
108         }
109         
110         public String getInputPath() {
111                 return inputPath;
112         }
113
114         public void setInputPath(String inputPath) {
115                 this.inputPath = inputPath;
116         }
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         @Override
242         public String toString() {
243                 return "PropertyDataDefinition [uniqueId=" + uniqueId + ", type=" + type + ", required=" + required + ", defaultValue=" + defaultValue + ", description=" + description + ", entrySchema=" + schema + ", parentUniqueId=" + parentUniqueId + ", password=" + password + "]";
244         }
245
246         @Override
247         public int hashCode() {
248                 final int prime = 31;
249                 int result = 1;
250                 result = prime * result + ((defaultValue == null) ? 0 : defaultValue.hashCode());
251                 result = prime * result + (definition ? 1231 : 1237);
252                 result = prime * result + ((description == null) ? 0 : description.hashCode());
253                 result = prime * result + (password ? 1231 : 1237);
254                 result = prime * result + ((required == null) ? 0 : required.hashCode());
255                 result = prime * result + ((schema == null) ? 0 : schema.hashCode());
256                 result = prime * result + ((type == null) ? 0 : type.hashCode());
257                 result = prime * result + ((uniqueId == null) ? 0 : uniqueId.hashCode());
258                 result = prime * result + ((parentUniqueId == null) ? 0 : parentUniqueId.hashCode());
259                 return result;
260         }
261
262         @Override
263         public boolean equals(Object obj) {
264                 if (this == obj)
265                         return true;
266                 if (obj == null)
267                         return false;
268                 if (getClass() != obj.getClass())
269                         return false;
270                 PropertyDataDefinition other = (PropertyDataDefinition) obj;
271                 if (defaultValue == null) {
272                         if (other.defaultValue != null)
273                                 return false;
274                 } else if (!defaultValue.equals(other.defaultValue))
275                         return false;
276                 if (definition != other.definition)
277                         return false;
278                 if (description == null) {
279                         if (other.description != null)
280                                 return false;
281                 } else if (!description.equals(other.description))
282                         return false;
283                 if (password != other.password)
284                         return false;
285                 if (required == null) {
286                         if (other.required != null)
287                                 return false;
288                 } else if (!required.equals(other.required))
289                         return false;
290                 if (schema == null) {
291                         if (other.schema != null)
292                                 return false;
293                 } else if (!schema.equals(other.schema))
294                         return false;
295                 if (type == null) {
296                         if (other.type != null)
297                                 return false;
298                 } else if (!type.equals(other.type))
299                         return false;
300                 if (parentUniqueId == null) {
301                         if (other.parentUniqueId != null)
302                                 return false;
303                 } else if (!parentUniqueId.equals(other.parentUniqueId))
304                         return false;
305                 if (uniqueId == null) {
306                         if (other.uniqueId != null)
307                                 return false;
308                 } else if (!uniqueId.equals(other.uniqueId))
309                         return false;
310                 return true;
311         }
312
313         @Override
314         public Object getToscaPresentationValue(JsonPresentationFields field) {
315                 switch (field) {
316                 case NAME:
317                         return name;
318                 case UNIQUE_ID:
319                         return uniqueId;
320                 case PASSWORD:
321                         return password;
322                 case TYPE:
323                         return type;
324                 case DEFINITION:
325                         return definition;
326                 case VALUE:
327                         return value;
328                 case DEFAULT_VALUE:
329                         return defaultValue;
330                 default:
331                         return super.getToscaPresentationValue(field);
332                 }
333         }
334
335         protected <T extends PropertyDataDefinition> T overrideDataDefinition(T other, boolean allowDefaultValueOverride){
336                 if(this.getType().equals(other.getType())){
337                         other.setOwnerId(getOwnerId());
338                         if(allowDefaultValueOverride)
339                                 other.setDefaultValue(getDefaultValue());
340                         return other;
341                 }
342                 return null;
343         }
344         
345         
346         //return Either.right(propertyName) if an illegal merge was attempted (overriding data type is forbidden)
347         public static <T extends PropertyDataDefinition> Either<Map<String, T>, String> mergeProperties(Map<String, T> derivedProps, Map<String, T> importedProps, boolean allowDefaultValueOverride){
348                 for(T property : importedProps.values()){
349                         derivedProps.merge(property.getName(), property, (derivedProp, importedProp) -> derivedProp.overrideDataDefinition(importedProp, allowDefaultValueOverride));
350                     //validate merge success
351                     if(!derivedProps.containsKey(property.getName()))
352                         return Either.right(property.getName());
353                 }
354                 return Either.left(derivedProps);
355         }
356         
357         
358                 
359         public static <T extends PropertyDataDefinition> Map<String, T> listToMapByName(List<T> propertiesList) {
360                 return propertiesList.stream()
361                 .collect(Collectors.toMap(p -> p.getName(), p -> p));
362         }
363
364
365 }