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