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