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