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 {
36 private List<PropertyConstraint> constraints;
38 public PropertyDefinition() {
42 public PropertyDefinition(PropertyDataDefinition p) {
47 public PropertyDefinition(PropertyDefinition pd) {
49 setConstraints(pd.getConstraints());
52 public List<PropertyConstraint> getConstraints() {
53 if (CollectionUtils.isEmpty(constraints)) {
54 constraints = deserializePropertyConstraints(findConstraints());
59 public void setConstraints(List<PropertyConstraint> constraints) {
60 setPropertyConstraints(serializePropertyConstraints(constraints));
61 this.constraints = constraints;
64 public List<PropertyConstraint> safeGetConstraints() {
65 return safeGetList(constraints);
68 private List<PropertyConstraint> deserializePropertyConstraints(List<String> constraints) {
69 if (CollectionUtils.isNotEmpty(constraints)) {
70 Type constraintType = new TypeToken<PropertyConstraint>() {
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());
78 private List<String> serializePropertyConstraints(List<PropertyConstraint> constraints) {
79 if (CollectionUtils.isNotEmpty(constraints)) {
80 Type constraintType = new TypeToken<PropertyConstraint>() {
82 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintSerialiser()).create();
83 return constraints.stream().map(gson::toJson).collect(Collectors.toList());
88 private List<String> findConstraints() {
89 if (CollectionUtils.isNotEmpty(getPropertyConstraints())) {
90 return getPropertyConstraints();
92 if (getSchemaProperty() != null) {
93 return getSchemaProperty().getPropertyConstraints();
99 public String toString() {
100 return "PropertyDefinition [ " + super.toString() + ", name=" + getName() + ", constraints=" + constraints + "]]";
104 public boolean isDefinition() {
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());
118 public boolean equals(Object obj) {
122 if (!super.equals(obj)) {
125 if (getClass() != obj.getClass()) {
128 PropertyDefinition other = (PropertyDefinition) obj;
129 if (constraints == null) {
130 if (other.constraints != null) {
133 } else if (!constraints.equals(other.constraints)) {
136 if (getName() == null) {
137 if (other.getName() != null) {
140 } else if (!getName().equals(other.getName())) {
147 * The enumeration presents the list of property names with specific behavior
151 public enum PropertyNames {
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);
163 private String propertyName;
164 private GroupInstancePropertyValueUpdateBehavior updateBehavior;
166 PropertyNames(String propertyName, GroupInstancePropertyValueUpdateBehavior updateBehavior) {
167 this.propertyName = propertyName;
168 this.updateBehavior = updateBehavior;
172 * finds PropertyNames according received string name
174 * @param name of the property
175 * @return PropertyNames found by received property name
177 public static PropertyNames findName(String name) {
178 for (PropertyNames e : PropertyNames.values()) {
179 if (e.getPropertyName().equals(name)) {
186 public String getPropertyName() {
190 public GroupInstancePropertyValueUpdateBehavior getUpdateBehavior() {
191 return updateBehavior;
196 * The enumeration presents the list of highest levels for which update property value is allowed
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);
206 GroupInstancePropertyValueUpdateBehavior(String name, int levelNumber) {
207 this.levelName = name;
208 this.levelNumber = levelNumber;
211 public String getLevelName() {
215 public int getLevelNumber() {