b4e731178b6938bb91c4e68c51fbf84d0120eba7
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / model / ChampObject.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ===================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.champcore.model;
23
24 import com.fasterxml.jackson.annotation.JsonIgnore;
25 import com.fasterxml.jackson.annotation.JsonProperty;
26
27 import org.onap.aai.champcore.model.fluent.object.CreateChampObjectable;
28 import org.onap.aai.champcore.model.fluent.object.impl.CreateChampObjectableImpl;
29
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Optional;
35
36 public final class ChampObject implements ChampElement {
37
38   private String type;
39   private Optional<Object> key;
40   private Map<String, Object> properties;
41
42   public static CreateChampObjectable create() {
43     return new CreateChampObjectableImpl();
44   }
45
46   public ChampObject() {
47   }
48
49   private ChampObject(Builder builder) {
50     this.type = builder.type;
51     this.key = builder.key;
52     this.properties = builder.properties;
53   }
54
55   @SuppressWarnings("unchecked")
56   public <T> Optional<T> getProperty(String key) {
57     if (!properties.containsKey(key)) {
58       return Optional.empty();
59     }
60
61     return Optional.of((T) properties.get(key));
62   }
63
64   public String getType() {
65     return type;
66   }
67
68   @JsonIgnore
69   public Optional<Object> getKey() {
70     return key;
71   }
72
73   public Map<String, Object> getProperties() {
74     return properties;
75   }
76
77   @JsonProperty("key")
78   public Object getKeyValue() {
79     return key.orElse("");
80
81   }
82
83   public ChampObject dropProperties(HashSet<String> safeProperties) {
84     HashSet<String> propertyKeys = new HashSet<>(properties.keySet());
85     for (String key : propertyKeys) {
86       if (!safeProperties.contains(key)) {
87         properties.remove(key);
88       }
89     }
90
91     return this;
92   }
93
94   public static class Builder {
95     private final String type;
96     private final Map<String, Object> properties = new HashMap<String, Object>();
97
98     private Optional<Object> key = Optional.empty();
99
100     public Builder(String type) {
101       if (type == null) {
102         throw new IllegalArgumentException("Type cannot be null");
103       }
104
105       this.type = type;
106     }
107
108     public Builder(ChampObject object) {
109       type = object.getType();
110       key = object.getKey();
111       properties(object.getProperties());
112     }
113
114     public Builder key(Object key) {
115       if (key == null) {
116         throw new IllegalArgumentException("Key cannot be set to null");
117       }
118
119       this.key = Optional.of(key);
120       return this;
121     }
122
123     public Builder property(String key, Object value) {
124       if (key == null) {
125         throw new IllegalArgumentException("Property key cannot be null");
126       }
127       if (value == null) {
128         throw new IllegalArgumentException("Property value cannot be null");
129       }
130
131       if (ReservedPropertyKeys.contains(key)) {
132         throw new IllegalArgumentException("Property key " + key + " is reserved");
133       }
134
135       properties.put(key, value);
136       return this;
137     }
138
139     public Builder properties(Map<String, Object> properties) {
140       for (Entry<String, Object> property : properties.entrySet()) {
141         property(property.getKey(), property.getValue());
142       }
143
144       return this;
145     }
146
147     public ChampObject build() {
148       return new ChampObject(this);
149     }
150   }
151
152   @Override
153   public boolean equals(Object object) {
154     if (this == object) {
155       return true;
156     }
157
158     if (object instanceof ChampObject) {
159       final ChampObject champObj = (ChampObject) object;
160
161       if (getKey().isPresent() && champObj.getKey().isPresent()) {
162
163         if (getKey().get().equals(champObj.getKey().get())) {
164           return true;
165         }
166
167       } else if (!getKey().isPresent() && !champObj.getKey().isPresent()) {
168         if (getType().equals(champObj.getType()) &&
169             getProperties().equals(champObj.getProperties())) {
170           return true;
171         }
172       }
173     }
174
175     return false;
176   }
177
178   @Override
179   public int hashCode() {
180     if (getKey().isPresent()) {
181       return getKey().get().hashCode();
182     }
183
184     final int returnValue = 31 * (getType().hashCode() + getProperties().hashCode());
185     return returnValue;
186   }
187
188   @Override
189   public String toString() {
190     return "{key: " + (getKey().isPresent() ? getKey().get() : "")
191         + ", type: " + getType()
192         + ", properties: " + getProperties() + "}";
193   }
194
195   public enum ReservedPropertyKeys {
196     CHAMP_OBJECT_TYPE("aai_node_type"),
197     CHAMP_OBJECT_KEY("key");
198
199     private final String text;
200
201     private ReservedPropertyKeys(final String text) {
202       this.text = text;
203     }
204
205     @Override
206     public String toString() {
207       return text;
208     }
209
210     public static boolean contains(String key) {
211       for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
212         if (choice.toString().equals(key)) {
213           return true;
214         }
215       }
216
217       return false;
218     }
219   }
220
221   public enum IgnoreOnReadPropertyKeys {
222     CHAMP_IMPORT_ASSIGNED_ID("importAssignedId");
223
224     private final String text;
225
226     private IgnoreOnReadPropertyKeys(final String text) {
227       this.text = text;
228     }
229
230     @Override
231     public String toString() {
232       return text;
233     }
234
235     public static boolean contains(String key) {
236       for (IgnoreOnReadPropertyKeys choice : IgnoreOnReadPropertyKeys.values()) {
237         if (choice.toString().equals(key)) {
238           return true;
239         }
240       }
241
242       return false;
243     }
244   }
245
246   public enum ReservedTypes {
247     ANY("ANY");
248
249     private final String text;
250
251     private ReservedTypes(final String text) {
252       this.text = text;
253     }
254
255     @Override
256     public String toString() {
257       return text;
258     }
259   }
260
261   @Override
262   public boolean isObject() {
263     return true;
264   }
265
266   @Override
267   public ChampObject asObject() {
268     return this;
269   }
270
271   @Override
272   public boolean isRelationship() {
273     return false;
274   }
275
276   @Override
277   public ChampRelationship asRelationship() {
278     throw new UnsupportedOperationException("Cannot call asRelationship() on ChampObject");
279   }
280 }