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