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.object.CreateChampObjectable;
29 import org.onap.aai.champ.model.fluent.object.impl.CreateChampObjectableImpl;
31 import java.util.Optional;
33 import com.fasterxml.jackson.annotation.JsonIgnore;
34 import com.fasterxml.jackson.annotation.JsonProperty;
36 public final class ChampObject implements ChampElement {
38 private final String type;
39 private final Optional<Object> key;
40 private final Map<String, Object> properties;
42 public static CreateChampObjectable create() {
43 return new CreateChampObjectableImpl();
46 private ChampObject() {
47 throw new RuntimeException("Attempted to call private AAIObject() constructor");
50 private ChampObject(Builder builder) {
51 this.type = builder.type;
52 this.key = builder.key;
53 this.properties = builder.properties;
56 @SuppressWarnings("unchecked")
57 public <T> Optional<T> getProperty(String key) {
58 if (!properties.containsKey(key)) return Optional.empty();
60 return Optional.of((T) properties.get(key));
63 public String getType() { return type; }
66 public Optional<Object> getKey() { return key; }
67 public Map<String, Object> getProperties() { return properties; }
70 public Object getKeyValue() {
71 return key.orElse("");
75 public static class Builder {
76 private final String type;
77 private final Map<String, Object> properties = new HashMap<String, Object> ();
79 private Optional<Object> key = Optional.empty();
81 public Builder(String type) {
82 if (type == null) throw new IllegalArgumentException("Type cannot be null");
87 public Builder(ChampObject object) {
88 type = object.getType();
89 key = object.getKey();
90 properties(object.getProperties());
93 public Builder key(Object key) {
94 if (key == null) throw new IllegalArgumentException("Key cannot be set to null");
96 this.key = Optional.of(key);
100 public Builder property(String key, Object value) {
101 if (key == null) throw new IllegalArgumentException("Property key cannot be null");
102 if (value == null) throw new IllegalArgumentException("Property value cannot be null");
104 if (ReservedPropertyKeys.contains(key)) throw new IllegalArgumentException("Property key " + key + " is reserved");
106 properties.put(key, value);
110 public Builder properties(Map<String, Object> properties) {
111 for (Entry<String, Object> property : properties.entrySet()) {
112 property(property.getKey(), property.getValue());
118 public ChampObject build() {
119 return new ChampObject(this);
124 public boolean equals(Object object) {
125 if (this == object) return true;
127 if (object instanceof ChampObject) {
128 final ChampObject champObj = (ChampObject) object;
130 if (getKey().isPresent() && champObj.getKey().isPresent()) {
132 if (getKey().get().equals(champObj.getKey().get())) return true;
134 } else if (!getKey().isPresent() && !champObj.getKey().isPresent()) {
135 if (getType().equals(champObj.getType()) &&
136 getProperties().equals(champObj.getProperties())) return true;
144 public int hashCode() {
145 if (getKey().isPresent()) return getKey().get().hashCode();
147 final int returnValue = 31 * (getType().hashCode() + getProperties().hashCode());
152 public String toString() {
153 return "{key: " + (getKey().isPresent() ? getKey().get() : "")
154 + ", type: " + getType()
155 + ", properties: " + getProperties() + "}";
158 public enum ReservedPropertyKeys {
159 CHAMP_OBJECT_TYPE ("aai_node_type"),
160 CHAMP_OBJECT_KEY ("key");
162 private final String text;
164 private ReservedPropertyKeys(final String text) {
169 public String toString() {
173 public static boolean contains(String key) {
174 for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
175 if (choice.toString().equals(key)) return true;
182 public enum IgnoreOnReadPropertyKeys {
183 CHAMP_IMPORT_ASSIGNED_ID ("importAssignedId");
185 private final String text;
187 private IgnoreOnReadPropertyKeys(final String text) {
192 public String toString() {
196 public static boolean contains(String key) {
197 for (IgnoreOnReadPropertyKeys choice : IgnoreOnReadPropertyKeys.values()) {
198 if (choice.toString().equals(key)) return true;
205 public enum ReservedTypes {
208 private final String text;
210 private ReservedTypes(final String text) {
215 public String toString() {
221 public boolean isObject() {
226 public ChampObject asObject() {
231 public boolean isRelationship() {
236 public ChampRelationship asRelationship() {
237 throw new UnsupportedOperationException("Cannot call asRelationship() on ChampObject");