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=========================================================
20 package org.openecomp.sdc.be.model;
22 import static org.openecomp.sdc.be.dao.utils.CollectionUtils.safeGetList;
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;
34 public class PropertyDefinition extends PropertyDataDefinition implements IOperationParameter, IComplexDefaultValue, ToscaPropertyData {
36 private List<PropertyConstraint> constraints;
38 public PropertyDefinition() {
42 public PropertyDefinition(PropertyDataDefinition p) {
47 public PropertyDefinition(PropertyDefinition pd) {
49 if (pd.getSchema() != null && pd.getSchema().getProperty() instanceof PropertyDefinition) {
50 this.getSchema().setProperty(new PropertyDefinition(pd.getSchema().getProperty()));
52 setConstraints(pd.getConstraints());
55 public List<PropertyConstraint> getConstraints() {
56 if (CollectionUtils.isEmpty(constraints)) {
57 constraints = deserializePropertyConstraints(findConstraints());
62 public void setConstraints(List<PropertyConstraint> constraints) {
63 setPropertyConstraints(serializePropertyConstraints(constraints));
64 this.constraints = constraints;
67 public List<PropertyConstraint> safeGetConstraints() {
68 return safeGetList(constraints);
71 private List<PropertyConstraint> deserializePropertyConstraints(List<String> constraints) {
72 if (CollectionUtils.isNotEmpty(constraints)) {
73 Type constraintType = new TypeToken<PropertyConstraint>() {
75 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintDeserialiser()).create();
76 return constraints.stream().map(c -> (PropertyConstraint) gson.fromJson(c, constraintType)).collect(Collectors.toList());
81 private List<String> serializePropertyConstraints(List<PropertyConstraint> constraints) {
82 if (CollectionUtils.isNotEmpty(constraints)) {
83 Type constraintType = new TypeToken<PropertyConstraint>() {
85 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintSerialiser()).create();
86 return constraints.stream().map(gson::toJson).collect(Collectors.toList());
91 private List<String> findConstraints() {
92 if (CollectionUtils.isNotEmpty(getPropertyConstraints())) {
93 return getPropertyConstraints();
95 if (getSchemaProperty() != null) {
96 return getSchemaProperty().getPropertyConstraints();
102 public String toString() {
103 return "PropertyDefinition [ " + super.toString() + ", name=" + getName() + ", constraints=" + constraints + "]]";
107 public boolean isDefinition() {
112 public int hashCode() {
113 final int prime = 31;
114 int result = super.hashCode();
115 result = prime * result + ((constraints == null) ? 0 : constraints.hashCode());
116 result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
121 public boolean equals(Object obj) {
125 if (!super.equals(obj)) {
128 if (getClass() != obj.getClass()) {
131 PropertyDefinition other = (PropertyDefinition) obj;
132 if (constraints == null) {
133 if (other.constraints != null) {
136 } else if (!constraints.equals(other.constraints)) {
139 if (getName() == null) {
140 if (other.getName() != null) {
143 } else if (!getName().equals(other.getName())) {
150 * The enumeration presents the list of property names with specific behavior
154 public enum PropertyNames {
156 MIN_INSTANCES("min_vf_module_instances", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
157 MAX_INSTANCES("max_vf_module_instances", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
158 INITIAL_COUNT("initial_count", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
159 VF_MODULE_LABEL("vf_module_label", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_RESOURCE_LEVEL),
160 VF_MODULE_DESCRIPTION("vf_module_description", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_RESOURCE_LEVEL),
161 NETWORK_ROLE("network_role", GroupInstancePropertyValueUpdateBehavior.NOT_RELEVANT),
162 AVAILABILTY_ZONE_COUNT("availability_zone_count", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
163 VFC_LIST("vfc_list", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL);
166 private String propertyName;
167 private GroupInstancePropertyValueUpdateBehavior updateBehavior;
169 PropertyNames(String propertyName, GroupInstancePropertyValueUpdateBehavior updateBehavior) {
170 this.propertyName = propertyName;
171 this.updateBehavior = updateBehavior;
175 * finds PropertyNames according received string name
177 * @param name of the property
178 * @return PropertyNames found by received property name
180 public static PropertyNames findName(String name) {
181 for (PropertyNames e : PropertyNames.values()) {
182 if (e.getPropertyName().equals(name)) {
189 public String getPropertyName() {
193 public GroupInstancePropertyValueUpdateBehavior getUpdateBehavior() {
194 return updateBehavior;
199 * The enumeration presents the list of highest levels for which update property value is allowed
203 public enum GroupInstancePropertyValueUpdateBehavior {
204 NOT_RELEVANT("NOT_RELEVANT", -1), UPDATABLE_ON_RESOURCE_LEVEL("UPDATABLE_ON_VF_LEVEL", 0), UPDATABLE_ON_SERVICE_LEVEL(
205 "UPDATABLE_ON_SERVICE_LEVEL", 1);
209 GroupInstancePropertyValueUpdateBehavior(String name, int levelNumber) {
210 this.levelName = name;
211 this.levelNumber = levelNumber;
214 public String getLevelName() {
218 public int getLevelNumber() {