aboutsummaryrefslogtreecommitdiffstats
path: root/champ-lib/champ-core/src/main/java/org/onap/aai/champcore/model/ChampRelationship.java
blob: 6083d522e8aacba9f620af0d9ab3379352c59c9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
 * ============LICENSE_START==========================================
 * org.onap.aai
 * ===================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * ===================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 */
package org.onap.aai.champcore.model;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

import org.onap.aai.champcore.model.fluent.relationship.CreateChampRelationshipable;
import org.onap.aai.champcore.model.fluent.relationship.impl.CreateChampRelationshipableImpl;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

public final class ChampRelationship implements ChampElement {
	private String type; //AKA edge label
	private  Optional<Object> key;
	private  Map<String, Object> properties;
	private  ChampObject source;
	private  ChampObject target;

	public static CreateChampRelationshipable create() {
		return new CreateChampRelationshipableImpl();
	}

	public ChampRelationship() { //Not instantiable
	}
	
	private ChampRelationship(Builder builder) {
		this.properties = builder.properties;
		this.source = builder.source;
		this.target = builder.target;
		this.type = builder.type;
		this.key = builder.key;
	}

	@JsonIgnore
	public Optional<Object> getKey() {
		if (key == null) {
			return Optional.empty ();
		} else {
			return key;
		}
	}

    @JsonProperty("key")
    public String getKeyValue() {
      return (getKey().isPresent() ? getKey().get() : "").toString();
    }
	   
	public ChampObject getSource() {
		return source;
	}
	
	public ChampObject getTarget() {
		return target;
	}
	
	public String getType() {
		return type;
	}
	
	@SuppressWarnings("unchecked")
	public <T> Optional<T> getProperty(String key) {
		if (!properties.containsKey(key)) return Optional.empty();
		
		return Optional.of((T) properties.get(key));
	}

	public Map<String, Object> getProperties() {
		return properties;
	}

	public static class Builder {
		private final Map<String, Object> properties = new HashMap<String, Object> ();
		private final ChampObject source;
		private final ChampObject target;
		private final String type;

		private Optional<Object> key = Optional.empty();

		public Builder(ChampObject source, ChampObject target, String type) {
			this.source = source;
			this.target = target;
			this.type = type;
		}
		
		public Builder(ChampRelationship relationship) {
			this.source = relationship.source;
			this.target = relationship.target;
			this.type = relationship.type;

			properties.putAll(relationship.getProperties());
		}

		public Builder key(Object key) {
			this.key = Optional.of(key);
			return this;
		}

		public Builder properties(Map<String, Object> properties) {
			for (Entry<String, Object> property : properties.entrySet()) {
				property(property.getKey(), property.getValue());
			}

			return this;
		}

		public Builder property(String key, Object value) {
			if (ChampRelationship.ReservedPropertyKeys.contains(key)) throw new IllegalArgumentException("Cannot make use of reserved property key " + key);

			properties.put(key, value);
			return this;
		}
		
		public ChampRelationship build() {
			return new ChampRelationship(this);
		}
	}

	@Override
	public boolean equals(Object object) {
		if (this == object) return true;
		if (object instanceof ChampRelationship) {
			final ChampRelationship champRelationship = (ChampRelationship) object;

			if (getKey().isPresent() && champRelationship.getKey().isPresent()) {
				if (getKey().get().equals(champRelationship.getKey().get())) return true;
			}
		}
		
		return false;
	}

	@Override
	public String toString() {
		return "{key: " + (getKey().isPresent() ? getKey().get() : "") 
				+ ", type: " + getType()
				+ ", source: " + getSource()
				+ ", target: " + getTarget()
				+ ", properties: " + getProperties() + "}";
	}

	public enum ReservedPropertyKeys {
		CHAMP_RELATIONSHIP_TYPE ("relationshipType"),
		CHAMP_RELATIONSHIP_KEY ("key");

		private final String text;

		private ReservedPropertyKeys(final String text) {
			this.text = text;
		} 
		
		public String toString() {
			return text;
		}

		public static boolean contains(String key) {
			for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
				if (choice.toString().equals(key)) return true;
			}

			return false;
		}
	}

	public enum ReservedTypes {
		ANY ("ANY");

		private final String text;
		
		private ReservedTypes(final String text) {
			this.text = text;
		}
		
		public String toString() {
			return text;
		}
	}

	@Override
	public boolean isObject() {
		return false;
	}

	@Override
	public ChampObject asObject() {
		throw new UnsupportedOperationException("Cannot call asObject() on ChampRelationship");
	}

	@Override
	public boolean isRelationship() {
		return true;
	}

	@Override
	public ChampRelationship asRelationship() {
		return this;
	}
}