[AAI-80 Amsterdam] checking in source code
[aai/champ.git] / src / main / java / org / openecomp / aai / champ / 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.openecomp.aai.champ.model;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.Optional;
28
29 import org.openecomp.aai.champ.model.fluent.object.CreateChampObjectable;
30 import org.openecomp.aai.champ.model.fluent.object.impl.CreateChampObjectableImpl;
31
32 import com.fasterxml.jackson.annotation.JsonIgnore;
33 import com.fasterxml.jackson.annotation.JsonProperty;
34
35 public final class ChampObject implements ChampElement {
36
37         private final String type;
38         private final Optional<Object> key;
39         private final Map<String, Object> properties;
40
41         public static CreateChampObjectable create() {
42                 return new CreateChampObjectableImpl();
43         }
44
45         private ChampObject() {
46                 throw new RuntimeException("Attempted to call private AAIObject() constructor");
47         } //Not instantiable
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)) return Optional.empty();
58
59                 return Optional.of((T) properties.get(key));
60         }
61
62         public String getType() { return type; }
63         
64         @JsonIgnore
65         public Optional<Object> getKey() { return key; }
66         public Map<String, Object> getProperties() { return properties; }
67
68         @JsonProperty("key")
69         public Object getKeyValue() {
70           return key.orElse("");
71           
72         }
73         
74         public static class Builder {
75                 private final String type;
76                 private final Map<String, Object> properties = new HashMap<String, Object> ();
77                 
78                 private Optional<Object> key = Optional.empty();
79
80                 public Builder(String type) {
81                         if (type == null) throw new IllegalArgumentException("Type cannot be null");
82
83                         this.type = type;
84                 }
85
86                 public Builder(ChampObject object) {
87                         type = object.getType();
88                         key = object.getKey();
89                         properties(object.getProperties());
90                 }
91
92                 public Builder key(Object key) {
93                         if (key == null) throw new IllegalArgumentException("Key cannot be set to null");
94
95                         this.key = Optional.of(key);
96                         return this;
97                 }
98
99                 public Builder property(String key, Object value) {
100                         if (key == null) throw new IllegalArgumentException("Property key cannot be null");
101                         if (value == null) throw new IllegalArgumentException("Property value cannot be null");
102
103                         if (ReservedPropertyKeys.contains(key)) throw new IllegalArgumentException("Property key " + key + " is reserved");
104
105                         properties.put(key, value);
106                         return this;
107                 }
108
109                 public Builder properties(Map<String, Object> properties) {
110                         for (Entry<String, Object> property : properties.entrySet()) {
111                                 property(property.getKey(), property.getValue());
112                         }
113                         
114                         return this;
115                 }
116                 
117                 public ChampObject build() {
118                         return new ChampObject(this);
119                 }
120         }
121         
122         @Override
123         public boolean equals(Object object) {
124                 if (this == object) return true;
125
126                 if (object instanceof ChampObject) {
127                         final ChampObject champObj = (ChampObject) object;
128
129                         if (getKey().isPresent() && champObj.getKey().isPresent()) {
130
131                                 if (getKey().get().equals(champObj.getKey().get())) return true;
132
133                         } else if (!getKey().isPresent() && !champObj.getKey().isPresent()) {
134                                 if (getType().equals(champObj.getType()) &&
135                                         getProperties().equals(champObj.getProperties())) return true;
136                         }
137                 }
138                 
139                 return false;
140         }
141
142         @Override
143         public int hashCode() {
144                 if (getKey().isPresent()) return getKey().get().hashCode();
145
146                 final int returnValue = 31 * (getType().hashCode() + getProperties().hashCode());
147                 return returnValue;
148         }
149
150         @Override
151         public String toString() {
152                 return "{key: " + (getKey().isPresent() ? getKey().get() : "")
153                         + ", type: " + getType()
154                         + ", properties: " + getProperties() + "}";
155         }
156         
157         public enum ReservedPropertyKeys {
158                 CHAMP_OBJECT_TYPE ("aai_node_type"),
159                 CHAMP_OBJECT_KEY ("key");
160
161                 private final String text;
162
163                 private ReservedPropertyKeys(final String text) {
164                         this.text = text;
165                 } 
166                 
167                 public String toString() {
168                         return text;
169                 }
170
171                 public static boolean contains(String key) {
172                         for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
173                                 if (choice.toString().equals(key)) return true;
174                         }
175
176                         return false;
177                 }
178         }
179
180         public enum IgnoreOnReadPropertyKeys {
181                 CHAMP_IMPORT_ASSIGNED_ID ("importAssignedId");
182                 
183                 private final String text;
184
185                 private IgnoreOnReadPropertyKeys(final String text) {
186                         this.text = text;
187                 } 
188                 
189                 public String toString() {
190                         return text;
191                 }
192
193                 public static boolean contains(String key) {
194                         for (IgnoreOnReadPropertyKeys choice : IgnoreOnReadPropertyKeys.values()) {
195                                 if (choice.toString().equals(key)) return true;
196                         }
197
198                         return false;
199                 }
200         }
201
202         public enum ReservedTypes {
203                 ANY ("ANY");
204
205                 private final String text;
206
207                 private ReservedTypes(final String text) {
208                         this.text = text;
209                 }
210
211                 public String toString() {
212                         return text;
213                 }
214         }
215
216         @Override
217         public boolean isObject() {
218                 return true;
219         }
220
221         @Override
222         public ChampObject asObject() {
223                 return this;
224         }
225
226         @Override
227         public boolean isRelationship() {
228                 return false;
229         }
230
231         @Override
232         public ChampRelationship asRelationship() {
233                 throw new UnsupportedOperationException("Cannot call asRelationship() on ChampObject");
234         }
235 }