2 * ============LICENSE_START==========================================
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
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============================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.champ.model;
24 import java.util.HashMap;
26 import java.util.Map.Entry;
28 import org.onap.aai.champ.model.fluent.relationship.CreateChampRelationshipable;
29 import org.onap.aai.champ.model.fluent.relationship.impl.CreateChampRelationshipableImpl;
31 import java.util.Optional;
33 import com.fasterxml.jackson.annotation.JsonIgnore;
34 import com.fasterxml.jackson.annotation.JsonProperty;
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;
43 public static CreateChampRelationshipable create() {
44 return new CreateChampRelationshipableImpl();
47 private ChampRelationship() { //Not instantiable
48 throw new RuntimeException("Cannot call ChampRelationship() constructor");
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;
60 public Optional<Object> getKey() {
65 public Object getKeyValue() {
66 return key.orElse("");
69 public ChampObject getSource() {
73 public ChampObject getTarget() {
77 public String getType() {
81 @SuppressWarnings("unchecked")
82 public <T> Optional<T> getProperty(String key) {
83 if (!properties.containsKey(key)) return Optional.empty();
85 return Optional.of((T) properties.get(key));
88 public Map<String, Object> getProperties() {
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;
98 private Optional<Object> key = Optional.empty();
100 public Builder(ChampObject source, ChampObject target, String type) {
101 this.source = source;
102 this.target = target;
106 public Builder(ChampRelationship relationship) {
107 this.source = relationship.source;
108 this.target = relationship.target;
109 this.type = relationship.type;
111 properties.putAll(relationship.getProperties());
114 public Builder key(Object key) {
115 this.key = Optional.of(key);
119 public Builder properties(Map<String, Object> properties) {
120 for (Entry<String, Object> property : properties.entrySet()) {
121 property(property.getKey(), property.getValue());
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);
130 properties.put(key, value);
134 public ChampRelationship build() {
135 return new ChampRelationship(this);
140 public boolean equals(Object object) {
141 if (this == object) return true;
142 if (object instanceof ChampRelationship) {
143 final ChampRelationship champRelationship = (ChampRelationship) object;
145 if (getKey().isPresent() && champRelationship.getKey().isPresent()) {
146 if (getKey().get().equals(champRelationship.getKey().get())) return true;
154 public String toString() {
155 return "{key: " + (getKey().isPresent() ? getKey().get() : "")
156 + ", type: " + getType()
157 + ", source: " + getSource()
158 + ", target: " + getTarget()
159 + ", properties: " + getProperties() + "}";
162 public enum ReservedPropertyKeys {
163 CHAMP_RELATIONSHIP_TYPE ("relationshipType"),
164 CHAMP_RELATIONSHIP_KEY ("key");
166 private final String text;
168 private ReservedPropertyKeys(final String text) {
172 public String toString() {
176 public static boolean contains(String key) {
177 for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
178 if (choice.toString().equals(key)) return true;
185 public enum ReservedTypes {
188 private final String text;
190 private ReservedTypes(final String text) {
194 public String toString() {
200 public boolean isObject() {
205 public ChampObject asObject() {
206 throw new UnsupportedOperationException("Cannot call asObject() on ChampRelationship");
210 public boolean isRelationship() {
215 public ChampRelationship asRelationship() {