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