2d7f033b76529d87864662978858f21aa7e0263f
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / model / ChampRelationship.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 java.util.HashMap;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.Optional;
28
29 import org.onap.aai.champcore.model.fluent.relationship.CreateChampRelationshipable;
30 import org.onap.aai.champcore.model.fluent.relationship.impl.CreateChampRelationshipableImpl;
31
32 import com.fasterxml.jackson.annotation.JsonIgnore;
33 import com.fasterxml.jackson.annotation.JsonProperty;
34
35 public final class ChampRelationship implements ChampElement {
36         private String type; //AKA edge label
37         private  Optional<Object> key;
38         private  Map<String, Object> properties;
39         private  ChampObject source;
40         private  ChampObject target;
41
42         public static CreateChampRelationshipable create() {
43                 return new CreateChampRelationshipableImpl();
44         }
45
46         public ChampRelationship() { //Not instantiable
47         }
48         
49         private ChampRelationship(Builder builder) {
50                 this.properties = builder.properties;
51                 this.source = builder.source;
52                 this.target = builder.target;
53                 this.type = builder.type;
54                 this.key = builder.key;
55         }
56
57         @JsonIgnore
58         public Optional<Object> getKey() {
59                 if (key == null) {
60                         return Optional.empty ();
61                 } else {
62                         return key;
63                 }
64         }
65
66     @JsonProperty("key")
67     public String getKeyValue() {
68       return (getKey().isPresent() ? getKey().get() : "").toString();
69     }
70            
71         public ChampObject getSource() {
72                 return source;
73         }
74         
75         public ChampObject getTarget() {
76                 return target;
77         }
78         
79         public String getType() {
80                 return type;
81         }
82         
83         @SuppressWarnings("unchecked")
84         public <T> Optional<T> getProperty(String key) {
85                 if (!properties.containsKey(key)) return Optional.empty();
86                 
87                 return Optional.of((T) properties.get(key));
88         }
89
90         public Map<String, Object> getProperties() {
91                 return properties;
92         }
93
94         public static class Builder {
95                 private final Map<String, Object> properties = new HashMap<String, Object> ();
96                 private final ChampObject source;
97                 private final ChampObject target;
98                 private final String type;
99
100                 private Optional<Object> key = Optional.empty();
101
102                 public Builder(ChampObject source, ChampObject target, String type) {
103                         this.source = source;
104                         this.target = target;
105                         this.type = type;
106                 }
107                 
108                 public Builder(ChampRelationship relationship) {
109                         this.source = relationship.source;
110                         this.target = relationship.target;
111                         this.type = relationship.type;
112
113                         properties.putAll(relationship.getProperties());
114                 }
115
116                 public Builder key(Object key) {
117                         this.key = Optional.of(key);
118                         return this;
119                 }
120
121                 public Builder properties(Map<String, Object> properties) {
122                         for (Entry<String, Object> property : properties.entrySet()) {
123                                 property(property.getKey(), property.getValue());
124                         }
125
126                         return this;
127                 }
128
129                 public Builder property(String key, Object value) {
130                         if (ChampRelationship.ReservedPropertyKeys.contains(key)) throw new IllegalArgumentException("Cannot make use of reserved property key " + key);
131
132                         properties.put(key, value);
133                         return this;
134                 }
135                 
136                 public ChampRelationship build() {
137                         return new ChampRelationship(this);
138                 }
139         }
140
141         @Override
142         public boolean equals(Object object) {
143                 if (this == object) return true;
144                 if (object instanceof ChampRelationship) {
145                         final ChampRelationship champRelationship = (ChampRelationship) object;
146
147                         if (getKey().isPresent() && champRelationship.getKey().isPresent()) {
148                                 if (getKey().get().equals(champRelationship.getKey().get())) return true;
149                         }
150                 }
151                 
152                 return false;
153         }
154
155         @Override
156         public String toString() {
157                 return "{key: " + (getKey().isPresent() ? getKey().get() : "") 
158                                 + ", type: " + getType()
159                                 + ", source: " + getSource()
160                                 + ", target: " + getTarget()
161                                 + ", properties: " + getProperties() + "}";
162         }
163
164         public enum ReservedPropertyKeys {
165                 CHAMP_RELATIONSHIP_TYPE ("relationshipType"),
166                 CHAMP_RELATIONSHIP_KEY ("key");
167
168                 private final String text;
169
170                 private ReservedPropertyKeys(final String text) {
171                         this.text = text;
172                 } 
173                 
174                 public String toString() {
175                         return text;
176                 }
177
178                 public static boolean contains(String key) {
179                         for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
180                                 if (choice.toString().equals(key)) return true;
181                         }
182
183                         return false;
184                 }
185         }
186
187         public enum ReservedTypes {
188                 ANY ("ANY");
189
190                 private final String text;
191                 
192                 private ReservedTypes(final String text) {
193                         this.text = text;
194                 }
195                 
196                 public String toString() {
197                         return text;
198                 }
199         }
200
201         @Override
202         public boolean isObject() {
203                 return false;
204         }
205
206         @Override
207         public ChampObject asObject() {
208                 throw new UnsupportedOperationException("Cannot call asObject() on ChampRelationship");
209         }
210
211         @Override
212         public boolean isRelationship() {
213                 return true;
214         }
215
216         @Override
217         public ChampRelationship asRelationship() {
218                 return this;
219         }
220 }