Fix Service/VF set value to list/map properties
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / SchemaDefinition.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.datatypes.elements;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import org.apache.commons.collections4.CollectionUtils;
29 import org.apache.commons.collections4.MapUtils;
30 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
31
32 /**
33  * Schema allows to create new types that can be used along TOSCA definitions.
34  */
35 public class SchemaDefinition extends ToscaDataDefinition {
36
37     private String derivedFrom;
38     private List<String> constraints;
39     private Map<String, PropertyDataDefinition> properties;
40
41     private PropertyDataDefinition property;
42
43
44     public SchemaDefinition() {
45     }
46
47     public SchemaDefinition(String derivedFrom, List<String> constraints,
48                             Map<String, PropertyDataDefinition> properties) {
49         this.setDerivedFrom(derivedFrom);
50         this.setConstraints(constraints);
51         this.setProperties(properties);
52     }
53
54     public SchemaDefinition(final SchemaDefinition schemaDefinition) {
55         if (schemaDefinition == null) {
56             return;
57         }
58         this.derivedFrom = schemaDefinition.getDerivedFrom();
59         if (CollectionUtils.isNotEmpty(schemaDefinition.getConstraints())) {
60             this.constraints = new ArrayList<>(schemaDefinition.getConstraints());
61         }
62         if (schemaDefinition.getProperty() != null) {
63             this.property = new PropertyDataDefinition(schemaDefinition.getProperty());
64         }
65         if (MapUtils.isNotEmpty(schemaDefinition.getProperties())) {
66             this.properties = new HashMap<>();
67             for (final Entry<String, PropertyDataDefinition> propertyEntry : schemaDefinition.getProperties().entrySet()) {
68                 this.properties.put(propertyEntry.getKey(), new PropertyDataDefinition(propertyEntry.getValue()));
69             }
70         }
71     }
72
73     public String getDerivedFrom() {
74         return derivedFrom;
75     }
76
77     public void setProperty(PropertyDataDefinition property) {
78         this.property = property;
79     }
80
81     public PropertyDataDefinition getProperty() {
82         return this.property;
83     }
84
85     public void setDerivedFrom(String derivedFrom) {
86         this.derivedFrom = derivedFrom;
87     }
88
89     public List<String> getConstraints() {
90         return constraints;
91     }
92
93     public void setConstraints(List<String> constraints) {
94         this.constraints = constraints;
95     }
96
97     public Map<String, PropertyDataDefinition> getProperties() {
98         return properties;
99     }
100
101     public void setProperties(Map<String, PropertyDataDefinition> properties) {
102         this.properties = properties;
103     }
104
105     public void addProperty(String key, PropertyDataDefinition property) {
106
107         properties.put(key, property);
108     }
109
110     @Override
111     public int hashCode() {
112         final int prime = 31;
113         int result = 1;
114         result = prime * result + ((constraints == null) ? 0 : constraints.hashCode());
115         result = prime * result + ((derivedFrom == null) ? 0 : derivedFrom.hashCode());
116         result = prime * result + ((properties == null) ? 0 : properties.hashCode());
117         result = prime * result + ((property == null) ? 0 : property.hashCode());
118         return result;
119     }
120
121     @Override
122     public boolean equals(Object obj) {
123         if (this == obj) {
124             return true;
125         }
126         if (obj == null) {
127             return false;
128         }
129         if (getClass() != obj.getClass()) {
130             return false;
131         }
132         SchemaDefinition other = (SchemaDefinition) obj;
133         if (constraints == null) {
134             if (other.constraints != null) {
135                 return false;
136             }
137         } else if (!constraints.equals(other.constraints)) {
138             return false;
139         }
140         if (derivedFrom == null) {
141             if (other.derivedFrom != null) {
142                 return false;
143             }
144         } else if (!derivedFrom.equals(other.derivedFrom)) {
145             return false;
146         }
147         if (properties == null) {
148             if (other.properties != null) {
149                 return false;
150             }
151         } else if (!properties.equals(other.properties)) {
152             return false;
153         }
154         if (property == null) {
155             return other.property == null;
156         } else {
157             return property.equals(other.property);
158         }
159     }
160
161     @Override
162     public String toString() {
163         return "SchemaDefinition [" + "derivedFrom='" + derivedFrom + ", constraints=" + constraints + ", properties="
164                 + properties + ", property=" + property + ']';
165     }
166 }