2 * ============LICENSE_START==========================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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============================================
21 package org.onap.aai.champcore.model;
23 import java.util.HashMap;
25 import java.util.Map.Entry;
26 import java.util.Objects;
27 import java.util.Optional;
29 import org.onap.aai.champcore.model.fluent.relationship.CreateChampRelationshipable;
30 import org.onap.aai.champcore.model.fluent.relationship.impl.CreateChampRelationshipableImpl;
32 import com.fasterxml.jackson.annotation.JsonIgnore;
33 import com.fasterxml.jackson.annotation.JsonProperty;
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;
42 public static CreateChampRelationshipable create() {
43 return new CreateChampRelationshipableImpl();
46 public ChampRelationship() { //Not instantiable
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;
58 public Optional<Object> getKey() {
60 return Optional.empty ();
67 public String getKeyValue() {
68 return (getKey().isPresent() ? getKey().get() : "").toString();
71 public ChampObject getSource() {
75 public ChampObject getTarget() {
79 public String getType() {
83 @SuppressWarnings("unchecked")
84 public <T> Optional<T> getProperty(String key) {
85 if (!properties.containsKey(key)) return Optional.empty();
87 return Optional.of((T) properties.get(key));
90 public Map<String, Object> getProperties() {
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;
100 private Optional<Object> key = Optional.empty();
102 public Builder(ChampObject source, ChampObject target, String type) {
103 this.source = source;
104 this.target = target;
108 public Builder(ChampRelationship relationship) {
109 this.source = relationship.source;
110 this.target = relationship.target;
111 this.type = relationship.type;
113 properties.putAll(relationship.getProperties());
116 public Builder key(Object key) {
117 this.key = Optional.of(key);
121 public Builder properties(Map<String, Object> properties) {
122 for (Entry<String, Object> property : properties.entrySet()) {
123 property(property.getKey(), property.getValue());
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);
132 properties.put(key, value);
136 public ChampRelationship build() {
137 return new ChampRelationship(this);
142 public boolean equals(Object object) {
143 if (this == object) return true;
144 if (object instanceof ChampRelationship) {
145 final ChampRelationship champRelationship = (ChampRelationship) object;
147 if (getKey().isPresent() && champRelationship.getKey().isPresent()) {
148 if (getKey().get().equals(champRelationship.getKey().get())) return true;
156 public int hashCode() {
157 return Objects.hashCode(getKey());
161 public String toString() {
162 return "{key: " + (getKey().isPresent() ? getKey().get() : "")
163 + ", type: " + getType()
164 + ", source: " + getSource()
165 + ", target: " + getTarget()
166 + ", properties: " + getProperties() + "}";
169 public enum ReservedPropertyKeys {
170 CHAMP_RELATIONSHIP_TYPE ("relationshipType"),
171 CHAMP_RELATIONSHIP_KEY ("key");
173 private final String text;
175 private ReservedPropertyKeys(final String text) {
179 public String toString() {
183 public static boolean contains(String key) {
184 for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
185 if (choice.toString().equals(key)) return true;
192 public enum ReservedTypes {
195 private final String text;
197 private ReservedTypes(final String text) {
201 public String toString() {
207 public boolean isObject() {
212 public ChampObject asObject() {
213 throw new UnsupportedOperationException("Cannot call asObject() on ChampRelationship");
217 public boolean isRelationship() {
222 public ChampRelationship asRelationship() {