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.openecomp.aai.champ.model;
24 import java.util.HashMap;
26 import java.util.Map.Entry;
27 import java.util.Optional;
29 import org.openecomp.aai.champ.model.fluent.relationship.CreateChampRelationshipable;
30 import org.openecomp.aai.champ.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 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;
42 public static CreateChampRelationshipable create() {
43 return new CreateChampRelationshipableImpl();
46 private ChampRelationship() { //Not instantiable
47 throw new RuntimeException("Cannot call ChampRelationship() constructor");
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;
59 public Optional<Object> getKey() {
64 public Object getKeyValue() {
65 return key.orElse("");
68 public ChampObject getSource() {
72 public ChampObject getTarget() {
76 public String getType() {
80 @SuppressWarnings("unchecked")
81 public <T> Optional<T> getProperty(String key) {
82 if (!properties.containsKey(key)) return Optional.empty();
84 return Optional.of((T) properties.get(key));
87 public Map<String, Object> getProperties() {
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;
97 private Optional<Object> key = Optional.empty();
99 public Builder(ChampObject source, ChampObject target, String type) {
100 this.source = source;
101 this.target = target;
105 public Builder(ChampRelationship relationship) {
106 this.source = relationship.source;
107 this.target = relationship.target;
108 this.type = relationship.type;
110 properties.putAll(relationship.getProperties());
113 public Builder key(Object key) {
114 this.key = Optional.of(key);
118 public Builder properties(Map<String, Object> properties) {
119 for (Entry<String, Object> property : properties.entrySet()) {
120 property(property.getKey(), property.getValue());
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);
129 properties.put(key, value);
133 public ChampRelationship build() {
134 return new ChampRelationship(this);
139 public boolean equals(Object object) {
140 if (this == object) return true;
141 if (object instanceof ChampRelationship) {
142 final ChampRelationship champRelationship = (ChampRelationship) object;
144 if (getKey().isPresent() && champRelationship.getKey().isPresent()) {
145 if (getKey().get().equals(champRelationship.getKey().get())) return true;
153 public String toString() {
154 return "{key: " + (getKey().isPresent() ? getKey().get() : "")
155 + ", type: " + getType()
156 + ", source: " + getSource()
157 + ", target: " + getTarget()
158 + ", properties: " + getProperties() + "}";
161 public enum ReservedPropertyKeys {
162 CHAMP_RELATIONSHIP_TYPE ("relationshipType"),
163 CHAMP_RELATIONSHIP_KEY ("key");
165 private final String text;
167 private ReservedPropertyKeys(final String text) {
171 public String toString() {
175 public static boolean contains(String key) {
176 for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
177 if (choice.toString().equals(key)) return true;
184 public enum ReservedTypes {
187 private final String text;
189 private ReservedTypes(final String text) {
193 public String toString() {
199 public boolean isObject() {
204 public ChampObject asObject() {
205 throw new UnsupportedOperationException("Cannot call asObject() on ChampRelationship");
209 public boolean isRelationship() {
214 public ChampRelationship asRelationship() {