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.Optional;
28 import org.onap.aai.champcore.model.fluent.relationship.CreateChampRelationshipable;
29 import org.onap.aai.champcore.model.fluent.relationship.impl.CreateChampRelationshipableImpl;
31 import com.fasterxml.jackson.annotation.JsonIgnore;
32 import com.fasterxml.jackson.annotation.JsonProperty;
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;
41 public static CreateChampRelationshipable create() {
42 return new CreateChampRelationshipableImpl();
45 public ChampRelationship() { //Not instantiable
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;
57 public Optional<Object> getKey() {
59 return Optional.empty ();
66 public String getKeyValue() {
67 return (getKey().isPresent() ? getKey().get() : "").toString();
70 public ChampObject getSource() {
74 public ChampObject getTarget() {
78 public String getType() {
82 @SuppressWarnings("unchecked")
83 public <T> Optional<T> getProperty(String key) {
84 if (!properties.containsKey(key)) return Optional.empty();
86 return Optional.of((T) properties.get(key));
89 public Map<String, Object> getProperties() {
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;
99 private Optional<Object> key = Optional.empty();
101 public Builder(ChampObject source, ChampObject target, String type) {
102 this.source = source;
103 this.target = target;
107 public Builder(ChampRelationship relationship) {
108 this.source = relationship.source;
109 this.target = relationship.target;
110 this.type = relationship.type;
112 properties.putAll(relationship.getProperties());
115 public Builder key(Object key) {
116 this.key = Optional.of(key);
120 public Builder properties(Map<String, Object> properties) {
121 for (Entry<String, Object> property : properties.entrySet()) {
122 property(property.getKey(), property.getValue());
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);
131 properties.put(key, value);
135 public ChampRelationship build() {
136 return new ChampRelationship(this);
141 public boolean equals(Object object) {
142 if (this == object) return true;
143 if (object instanceof ChampRelationship) {
144 final ChampRelationship champRelationship = (ChampRelationship) object;
146 if (getKey().isPresent() && champRelationship.getKey().isPresent()) {
147 if (getKey().get().equals(champRelationship.getKey().get())) return true;
155 public String toString() {
156 return "{key: " + (getKey().isPresent() ? getKey().get() : "")
157 + ", type: " + getType()
158 + ", source: " + getSource()
159 + ", target: " + getTarget()
160 + ", properties: " + getProperties() + "}";
163 public enum ReservedPropertyKeys {
164 CHAMP_RELATIONSHIP_TYPE ("relationshipType"),
165 CHAMP_RELATIONSHIP_KEY ("key");
167 private final String text;
169 private ReservedPropertyKeys(final String text) {
173 public String toString() {
177 public static boolean contains(String key) {
178 for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
179 if (choice.toString().equals(key)) return true;
186 public enum ReservedTypes {
189 private final String text;
191 private ReservedTypes(final String text) {
195 public String toString() {
201 public boolean isObject() {
206 public ChampObject asObject() {
207 throw new UnsupportedOperationException("Cannot call asObject() on ChampRelationship");
211 public boolean isRelationship() {
216 public ChampRelationship asRelationship() {