2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model;
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;
30 import java.lang.reflect.Type;
31 import java.util.List;
32 import java.util.stream.Collectors;
34 import static org.openecomp.sdc.be.dao.utils.CollectionUtils.safeGetList;
38 public class PropertyDefinition extends PropertyDataDefinition
39 implements IOperationParameter, IComplexDefaultValue {
42 /**The enumeration presents the list of property names with specific behavior
46 public enum PropertyNames {
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);
57 private String propertyName;
58 private GroupInstancePropertyValueUpdateBehavior updateBehavior;
60 PropertyNames(String propertyName,GroupInstancePropertyValueUpdateBehavior updateBehavior){
61 this.propertyName = propertyName;
62 this.updateBehavior = updateBehavior;
65 public String getPropertyName() {
69 public GroupInstancePropertyValueUpdateBehavior getUpdateBehavior() {
70 return updateBehavior;
73 * finds PropertyNames according received string name
74 * @param name of the property
75 * @return PropertyNames found by received property name
77 public static PropertyNames findName(String name){
78 for (PropertyNames e : PropertyNames.values()) {
79 if (e.getPropertyName().equals(name)) {
87 * The enumeration presents the list of highest levels for which update property value is allowed
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);
99 GroupInstancePropertyValueUpdateBehavior(String name, int levelNumber){
100 this.levelName = name;
101 this.levelNumber = levelNumber;
104 public String getLevelName() {
108 public int getLevelNumber() {
113 private List<PropertyConstraint> constraints;
115 public PropertyDefinition() {
119 public PropertyDefinition(PropertyDataDefinition p) {
124 public PropertyDefinition(PropertyDefinition pd) {
126 setConstraints(pd.getConstraints());
129 public List<PropertyConstraint> getConstraints() {
130 if(CollectionUtils.isEmpty(constraints)){
131 constraints = deserializePropertyConstraints(findConstraints());
136 public List<PropertyConstraint> safeGetConstraints() {
137 return safeGetList(constraints);
140 public void setConstraints(List<PropertyConstraint> constraints) {
141 setPropertyConstraints(serializePropertyConstraints(constraints));
142 this.constraints = constraints;
145 private List<PropertyConstraint> deserializePropertyConstraints(List<String> constraints) {
146 if(CollectionUtils.isNotEmpty(constraints)){
147 Type constraintType = new TypeToken<PropertyConstraint>() {
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());
155 private List<String> serializePropertyConstraints(List<PropertyConstraint> constraints) {
156 if(CollectionUtils.isNotEmpty(constraints)){
157 Type constraintType = new TypeToken<PropertyConstraint>() {
159 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintSerialiser()).create();
160 return constraints.stream().map(gson::toJson).collect(Collectors.toList());
165 private List<String> findConstraints() {
166 if(CollectionUtils.isNotEmpty(getPropertyConstraints())){
167 return getPropertyConstraints();
169 if(getSchemaProperty()!= null){
170 return getSchemaProperty().getPropertyConstraints();
176 public String toString() {
177 return "PropertyDefinition [ " + super.toString() + ", name=" + getName() + ", constraints="
178 + constraints + "]]";
182 public boolean isDefinition() {
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());
196 public boolean equals(Object obj) {
199 if (!super.equals(obj))
201 if (getClass() != obj.getClass())
203 PropertyDefinition other = (PropertyDefinition) obj;
204 if (constraints == null) {
205 if (other.constraints != null)
207 } else if (!constraints.equals(other.constraints))
209 if (getName() == null) {
210 if (other.getName() != null)
212 } else if (!getName().equals(other.getName()))