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 lombok.NoArgsConstructor;
31 import org.apache.commons.collections.CollectionUtils;
32 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
33 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
36 public class PropertyDefinition extends PropertyDataDefinition implements IOperationParameter, IComplexDefaultValue, ToscaPropertyData {
38 private List<PropertyConstraint> constraints;
40 public String getToscaSubPath() {
44 public void setToscaSubPath(String toscaSubPath) {
45 this.toscaSubPath = toscaSubPath;
48 private String toscaSubPath;
50 public PropertyDefinition(PropertyDataDefinition p) {
55 public PropertyDefinition(PropertyDefinition pd) {
57 if (pd.getSchema() != null && pd.getSchema().getProperty() instanceof PropertyDefinition) {
58 this.getSchema().setProperty(new PropertyDefinition(pd.getSchema().getProperty()));
60 setConstraints(pd.getConstraints());
63 public List<PropertyConstraint> getConstraints() {
64 if (CollectionUtils.isEmpty(constraints)) {
65 constraints = deserializePropertyConstraints(findConstraints());
70 public void setConstraints(List<PropertyConstraint> constraints) {
71 setPropertyConstraints(serializePropertyConstraints(constraints));
72 this.constraints = constraints;
75 public List<PropertyConstraint> safeGetConstraints() {
76 return safeGetList(constraints);
79 private List<PropertyConstraint> deserializePropertyConstraints(List<String> constraints) {
80 if (CollectionUtils.isNotEmpty(constraints)) {
81 Type constraintType = new TypeToken<PropertyConstraint>() {
83 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintDeserialiser()).create();
84 return constraints.stream().map(c -> (PropertyConstraint) gson.fromJson(c, constraintType)).collect(Collectors.toList());
89 private List<String> serializePropertyConstraints(List<PropertyConstraint> constraints) {
90 if (CollectionUtils.isNotEmpty(constraints)) {
91 Type constraintType = new TypeToken<PropertyConstraint>() {
93 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintSerialiser()).create();
94 return constraints.stream().map(gson::toJson).collect(Collectors.toList());
99 private List<String> findConstraints() {
100 if (CollectionUtils.isNotEmpty(getPropertyConstraints())) {
101 return getPropertyConstraints();
103 if (getSchemaProperty() != null) {
104 return getSchemaProperty().getPropertyConstraints();
110 public String toString() {
111 return "PropertyDefinition [ " + super.toString() + ", name=" + getName() + ", constraints=" + constraints + "]]";
115 public boolean isDefinition() {
120 public int hashCode() {
121 final int prime = 31;
122 int result = super.hashCode();
123 result = prime * result + ((constraints == null) ? 0 : constraints.hashCode());
124 result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
129 public boolean equals(Object obj) {
133 if (!super.equals(obj)) {
136 if (getClass() != obj.getClass()) {
139 PropertyDefinition other = (PropertyDefinition) obj;
140 if (constraints == null) {
141 if (other.constraints != null) {
144 } else if (!constraints.equals(other.constraints)) {
147 if (getName() == null) {
148 if (other.getName() != null) {
151 } else if (!getName().equals(other.getName())) {
158 * The enumeration presents the list of property names with specific behavior
162 public enum PropertyNames {
164 MIN_INSTANCES("min_vf_module_instances", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
165 MAX_INSTANCES("max_vf_module_instances", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
166 INITIAL_COUNT("initial_count", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
167 VF_MODULE_LABEL("vf_module_label", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_RESOURCE_LEVEL),
168 VF_MODULE_DESCRIPTION("vf_module_description", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_RESOURCE_LEVEL),
169 NETWORK_ROLE("network_role", GroupInstancePropertyValueUpdateBehavior.NOT_RELEVANT),
170 AVAILABILTY_ZONE_COUNT("availability_zone_count", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
171 VFC_LIST("vfc_list", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL);
174 private String propertyName;
175 private GroupInstancePropertyValueUpdateBehavior updateBehavior;
177 PropertyNames(String propertyName, GroupInstancePropertyValueUpdateBehavior updateBehavior) {
178 this.propertyName = propertyName;
179 this.updateBehavior = updateBehavior;
183 * finds PropertyNames according received string name
185 * @param name of the property
186 * @return PropertyNames found by received property name
188 public static PropertyNames findName(String name) {
189 for (PropertyNames e : PropertyNames.values()) {
190 if (e.getPropertyName().equals(name)) {
197 public String getPropertyName() {
201 public GroupInstancePropertyValueUpdateBehavior getUpdateBehavior() {
202 return updateBehavior;
207 * The enumeration presents the list of highest levels for which update property value is allowed
211 public enum GroupInstancePropertyValueUpdateBehavior {
212 NOT_RELEVANT("NOT_RELEVANT", -1), UPDATABLE_ON_RESOURCE_LEVEL("UPDATABLE_ON_VF_LEVEL", 0), UPDATABLE_ON_SERVICE_LEVEL(
213 "UPDATABLE_ON_SERVICE_LEVEL", 1);
217 GroupInstancePropertyValueUpdateBehavior(String name, int levelNumber) {
218 this.levelName = name;
219 this.levelNumber = levelNumber;
222 public String getLevelName() {
226 public int getLevelNumber() {