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