aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyDefinition.java
blob: c06df6541fd539fe1a8b4fbd05de3c226e947cc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.sdc.be.model;

import static org.openecomp.sdc.be.dao.utils.CollectionUtils.safeGetList;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.lang.reflect.Type;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;

public class PropertyDefinition extends PropertyDataDefinition implements IOperationParameter, IComplexDefaultValue {

    private List<PropertyConstraint> constraints;

    public PropertyDefinition() {
        super();
    }

    public PropertyDefinition(PropertyDataDefinition p) {
        super(p);
        getConstraints();
    }

    public PropertyDefinition(PropertyDefinition pd) {
        super(pd);
        setConstraints(pd.getConstraints());
    }

    public List<PropertyConstraint> getConstraints() {
        if (CollectionUtils.isEmpty(constraints)) {
            constraints = deserializePropertyConstraints(findConstraints());
        }
        return constraints;
    }

    public void setConstraints(List<PropertyConstraint> constraints) {
        setPropertyConstraints(serializePropertyConstraints(constraints));
        this.constraints = constraints;
    }

    public List<PropertyConstraint> safeGetConstraints() {
        return safeGetList(constraints);
    }

    private List<PropertyConstraint> deserializePropertyConstraints(List<String> constraints) {
        if (CollectionUtils.isNotEmpty(constraints)) {
            Type constraintType = new TypeToken<PropertyConstraint>() {
            }.getType();
            Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintDeserialiser()).create();
            return constraints.stream().map(c -> (PropertyConstraint) gson.fromJson(c, constraintType)).collect(Collectors.toList());
        }
        return null;
    }

    private List<String> serializePropertyConstraints(List<PropertyConstraint> constraints) {
        if (CollectionUtils.isNotEmpty(constraints)) {
            Type constraintType = new TypeToken<PropertyConstraint>() {
            }.getType();
            Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintSerialiser()).create();
            return constraints.stream().map(gson::toJson).collect(Collectors.toList());
        }
        return null;
    }

    private List<String> findConstraints() {
        if (CollectionUtils.isNotEmpty(getPropertyConstraints())) {
            return getPropertyConstraints();
        }
        if (getSchemaProperty() != null) {
            return getSchemaProperty().getPropertyConstraints();
        }
        return null;
    }

    @Override
    public String toString() {
        return "PropertyDefinition [ " + super.toString() + ", name=" + getName() + ", constraints=" + constraints + "]]";
    }

    @Override
    public boolean isDefinition() {
        return false;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ((constraints == null) ? 0 : constraints.hashCode());
        result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!super.equals(obj)) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        PropertyDefinition other = (PropertyDefinition) obj;
        if (constraints == null) {
            if (other.constraints != null) {
                return false;
            }
        } else if (!constraints.equals(other.constraints)) {
            return false;
        }
        if (getName() == null) {
            if (other.getName() != null) {
                return false;
            }
        } else if (!getName().equals(other.getName())) {
            return false;
        }
        return true;
    }

    /**
     * The enumeration presents the list of property names with specific behavior
     *
     * @author rbetzer
     */
    public enum PropertyNames {
        // @formatter:off
        MIN_INSTANCES("min_vf_module_instances", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
        MAX_INSTANCES("max_vf_module_instances", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
        INITIAL_COUNT("initial_count", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
        VF_MODULE_LABEL("vf_module_label", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_RESOURCE_LEVEL),
        VF_MODULE_DESCRIPTION("vf_module_description", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_RESOURCE_LEVEL),
        NETWORK_ROLE("network_role", GroupInstancePropertyValueUpdateBehavior.NOT_RELEVANT),
        AVAILABILTY_ZONE_COUNT("availability_zone_count", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL),
        VFC_LIST("vfc_list", GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL);
        // @formatter:on

        private String propertyName;
        private GroupInstancePropertyValueUpdateBehavior updateBehavior;

        PropertyNames(String propertyName, GroupInstancePropertyValueUpdateBehavior updateBehavior) {
            this.propertyName = propertyName;
            this.updateBehavior = updateBehavior;
        }

        /**
         * finds PropertyNames according received string name
         *
         * @param name of the property
         * @return PropertyNames found by received property name
         */
        public static PropertyNames findName(String name) {
            for (PropertyNames e : PropertyNames.values()) {
                if (e.getPropertyName().equals(name)) {
                    return e;
                }
            }
            return null;
        }

        public String getPropertyName() {
            return propertyName;
        }

        public GroupInstancePropertyValueUpdateBehavior getUpdateBehavior() {
            return updateBehavior;
        }
    }

    /**
     * The enumeration presents the list of highest levels for which update property value is allowed
     *
     * @author nsheshukov
     */
    public enum GroupInstancePropertyValueUpdateBehavior {
        NOT_RELEVANT("NOT_RELEVANT", -1), UPDATABLE_ON_RESOURCE_LEVEL("UPDATABLE_ON_VF_LEVEL", 0), UPDATABLE_ON_SERVICE_LEVEL(
            "UPDATABLE_ON_SERVICE_LEVEL", 1);
        String levelName;
        int levelNumber;

        GroupInstancePropertyValueUpdateBehavior(String name, int levelNumber) {
            this.levelName = name;
            this.levelNumber = levelNumber;
        }

        public String getLevelName() {
            return levelName;
        }

        public int getLevelNumber() {
            return levelNumber;
        }
    }
}