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 static org.openecomp.sdc.be.dao.utils.CollectionUtils.safeGetList;
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;
32 import lombok.NoArgsConstructor;
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;
39 public class PropertyDefinition extends PropertyDataDefinition implements IOperationParameter, IComplexDefaultValue, ToscaPropertyData {
41 private List<PropertyConstraint> constraints;
44 private String toscaSubPath;
46 public PropertyDefinition(PropertyDataDefinition p) {
51 public PropertyDefinition(PropertyDefinition pd) {
53 if (pd.getSchema() != null && pd.getSchema().getProperty() instanceof PropertyDefinition) {
54 this.getSchema().setProperty(new PropertyDefinition(pd.getSchema().getProperty()));
56 setConstraints(pd.getConstraints());
59 public List<PropertyConstraint> getConstraints() {
60 if (CollectionUtils.isEmpty(constraints)) {
61 setConstraints(deserializePropertyConstraints(findConstraints()));
66 public void setConstraints(List<PropertyConstraint> constraints) {
67 setPropertyConstraints(serializePropertyConstraints(constraints));
68 this.constraints = constraints;
71 public List<PropertyConstraint> safeGetConstraints() {
72 return safeGetList(constraints);
75 private List<PropertyConstraint> deserializePropertyConstraints(List<String> constraints) {
76 if (CollectionUtils.isNotEmpty(constraints)) {
77 Type constraintType = new TypeToken<PropertyConstraint>() {
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());
85 private List<String> serializePropertyConstraints(List<PropertyConstraint> constraints) {
86 if (CollectionUtils.isNotEmpty(constraints)) {
87 Type constraintType = new TypeToken<PropertyConstraint>() {
89 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintSerialiser()).create();
90 return constraints.stream().map(gson::toJson).collect(Collectors.toList());
95 private List<String> findConstraints() {
96 if (CollectionUtils.isNotEmpty(getPropertyConstraints())) {
97 return getPropertyConstraints();
99 if (getSchemaProperty() != null) {
100 return getSchemaProperty().getPropertyConstraints();
106 public String toString() {
107 return "PropertyDefinition [ " + super.toString() + ", name=" + getName() + ", constraints=" + constraints + "]]";
111 public boolean isDefinition() {
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());
125 public boolean equals(Object obj) {
129 if (!super.equals(obj)) {
132 if (getClass() != obj.getClass()) {
135 PropertyDefinition other = (PropertyDefinition) obj;
136 if (constraints == null) {
137 if (other.constraints != null) {
140 } else if (!constraints.equals(other.constraints)) {
143 if (getName() == null) {
144 if (other.getName() != null) {
147 } else if (!getName().equals(other.getName())) {
154 * The enumeration presents the list of property names with specific behavior
158 public enum PropertyNames {
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);
170 private String propertyName;
171 private GroupInstancePropertyValueUpdateBehavior updateBehavior;
173 PropertyNames(String propertyName, GroupInstancePropertyValueUpdateBehavior updateBehavior) {
174 this.propertyName = propertyName;
175 this.updateBehavior = updateBehavior;
179 * finds PropertyNames according received string name
181 * @param name of the property
182 * @return PropertyNames found by received property name
184 public static PropertyNames findName(String name) {
185 for (PropertyNames e : PropertyNames.values()) {
186 if (e.getPropertyName().equals(name)) {
193 public String getPropertyName() {
197 public GroupInstancePropertyValueUpdateBehavior getUpdateBehavior() {
198 return updateBehavior;
203 * The enumeration presents the list of highest levels for which update property value is allowed
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);
213 GroupInstancePropertyValueUpdateBehavior(String name, int levelNumber) {
214 this.levelName = name;
215 this.levelNumber = levelNumber;
218 public String getLevelName() {
222 public int getLevelNumber() {