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