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