6e313eda6c73226b23cc173c044ff541b15c799b
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / PropertyDefinition.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.model;
21
22 import static org.openecomp.sdc.be.dao.utils.CollectionUtils.safeGetList;
23
24 import com.google.common.reflect.TypeToken;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import java.lang.reflect.Type;
28 import java.util.List;
29 import java.util.stream.Collectors;
30 import org.apache.commons.collections.CollectionUtils;
31 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
32 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
33
34 public class PropertyDefinition extends PropertyDataDefinition implements IOperationParameter, IComplexDefaultValue {
35
36     private List<PropertyConstraint> constraints;
37
38     public PropertyDefinition() {
39         super();
40     }
41
42     public PropertyDefinition(PropertyDataDefinition p) {
43         super(p);
44         getConstraints();
45     }
46
47     public PropertyDefinition(PropertyDefinition pd) {
48         super(pd);
49         if (pd.getSchema() != null && pd.getSchema().getProperty() instanceof PropertyDefinition) {
50             this.getSchema().setProperty(new PropertyDefinition(pd.getSchema().getProperty()));
51         }
52         setConstraints(pd.getConstraints());
53     }
54
55     public List<PropertyConstraint> getConstraints() {
56         if (CollectionUtils.isEmpty(constraints)) {
57             constraints = deserializePropertyConstraints(findConstraints());
58         }
59         return constraints;
60     }
61
62     public void setConstraints(List<PropertyConstraint> constraints) {
63         setPropertyConstraints(serializePropertyConstraints(constraints));
64         this.constraints = constraints;
65     }
66
67     public List<PropertyConstraint> safeGetConstraints() {
68         return safeGetList(constraints);
69     }
70
71     private List<PropertyConstraint> deserializePropertyConstraints(List<String> constraints) {
72         if (CollectionUtils.isNotEmpty(constraints)) {
73             Type constraintType = new TypeToken<PropertyConstraint>() {
74             }.getType();
75             Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintDeserialiser()).create();
76             return constraints.stream().map(c -> (PropertyConstraint) gson.fromJson(c, constraintType)).collect(Collectors.toList());
77         }
78         return null;
79     }
80
81     private List<String> serializePropertyConstraints(List<PropertyConstraint> constraints) {
82         if (CollectionUtils.isNotEmpty(constraints)) {
83             Type constraintType = new TypeToken<PropertyConstraint>() {
84             }.getType();
85             Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintSerialiser()).create();
86             return constraints.stream().map(gson::toJson).collect(Collectors.toList());
87         }
88         return null;
89     }
90
91     private List<String> findConstraints() {
92         if (CollectionUtils.isNotEmpty(getPropertyConstraints())) {
93             return getPropertyConstraints();
94         }
95         if (getSchemaProperty() != null) {
96             return getSchemaProperty().getPropertyConstraints();
97         }
98         return null;
99     }
100
101     @Override
102     public String toString() {
103         return "PropertyDefinition [ " + super.toString() + ", name=" + getName() + ", constraints=" + constraints + "]]";
104     }
105
106     @Override
107     public boolean isDefinition() {
108         return false;
109     }
110
111     @Override
112     public int hashCode() {
113         final int prime = 31;
114         int result = super.hashCode();
115         result = prime * result + ((constraints == null) ? 0 : constraints.hashCode());
116         result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
117         return result;
118     }
119
120     @Override
121     public boolean equals(Object obj) {
122         if (this == obj) {
123             return true;
124         }
125         if (!super.equals(obj)) {
126             return false;
127         }
128         if (getClass() != obj.getClass()) {
129             return false;
130         }
131         PropertyDefinition other = (PropertyDefinition) obj;
132         if (constraints == null) {
133             if (other.constraints != null) {
134                 return false;
135             }
136         } else if (!constraints.equals(other.constraints)) {
137             return false;
138         }
139         if (getName() == null) {
140             if (other.getName() != null) {
141                 return false;
142             }
143         } else if (!getName().equals(other.getName())) {
144             return false;
145         }
146         return true;
147     }
148
149     /**
150      * The enumeration presents the list of property names with specific behavior
151      *
152      * @author rbetzer
153      */
154     public enum PropertyNames {
155         // @formatter:off
156         MIN_INSTANCES("min_vf_module_instances", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
157         MAX_INSTANCES("max_vf_module_instances", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
158         INITIAL_COUNT("initial_count", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
159         VF_MODULE_LABEL("vf_module_label", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_RESOURCE_LEVEL),
160         VF_MODULE_DESCRIPTION("vf_module_description", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_RESOURCE_LEVEL),
161         NETWORK_ROLE("network_role", GroupInstancePropertyValueUpdateBehavior.NOT_RELEVANT),
162         AVAILABILTY_ZONE_COUNT("availability_zone_count", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
163         VFC_LIST("vfc_list", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL);
164         // @formatter:on
165
166         private String propertyName;
167         private GroupInstancePropertyValueUpdateBehavior updateBehavior;
168
169         PropertyNames(String propertyName, GroupInstancePropertyValueUpdateBehavior updateBehavior) {
170             this.propertyName = propertyName;
171             this.updateBehavior = updateBehavior;
172         }
173
174         /**
175          * finds PropertyNames according received string name
176          *
177          * @param name of the property
178          * @return PropertyNames found by received property name
179          */
180         public static PropertyNames findName(String name) {
181             for (PropertyNames e : PropertyNames.values()) {
182                 if (e.getPropertyName().equals(name)) {
183                     return e;
184                 }
185             }
186             return null;
187         }
188
189         public String getPropertyName() {
190             return propertyName;
191         }
192
193         public GroupInstancePropertyValueUpdateBehavior getUpdateBehavior() {
194             return updateBehavior;
195         }
196     }
197
198     /**
199      * The enumeration presents the list of highest levels for which update property value is allowed
200      *
201      * @author nsheshukov
202      */
203     public enum GroupInstancePropertyValueUpdateBehavior {
204         NOT_RELEVANT("NOT_RELEVANT", -1), UPDATABLE_ON_RESOURCE_LEVEL("UPDATABLE_ON_VF_LEVEL", 0), UPDATABLE_ON_SERVICE_LEVEL(
205             "UPDATABLE_ON_SERVICE_LEVEL", 1);
206         String levelName;
207         int levelNumber;
208
209         GroupInstancePropertyValueUpdateBehavior(String name, int levelNumber) {
210             this.levelName = name;
211             this.levelNumber = levelNumber;
212         }
213
214         public String getLevelName() {
215             return levelName;
216         }
217
218         public int getLevelNumber() {
219             return levelNumber;
220         }
221     }
222 }