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.object.CreateChampObjectable;
30 import org.openecomp.aai.champ.model.fluent.object.impl.CreateChampObjectableImpl;
32 import com.fasterxml.jackson.annotation.JsonIgnore;
33 import com.fasterxml.jackson.annotation.JsonProperty;
35 public final class ChampObject implements ChampElement {
37 private final String type;
38 private final Optional<Object> key;
39 private final Map<String, Object> properties;
41 public static CreateChampObjectable create() {
42 return new CreateChampObjectableImpl();
45 private ChampObject() {
46 throw new RuntimeException("Attempted to call private AAIObject() constructor");
49 private ChampObject(Builder builder) {
50 this.type = builder.type;
51 this.key = builder.key;
52 this.properties = builder.properties;
55 @SuppressWarnings("unchecked")
56 public <T> Optional<T> getProperty(String key) {
57 if (!properties.containsKey(key)) return Optional.empty();
59 return Optional.of((T) properties.get(key));
62 public String getType() { return type; }
65 public Optional<Object> getKey() { return key; }
66 public Map<String, Object> getProperties() { return properties; }
69 public Object getKeyValue() {
70 return key.orElse("");
74 public static class Builder {
75 private final String type;
76 private final Map<String, Object> properties = new HashMap<String, Object> ();
78 private Optional<Object> key = Optional.empty();
80 public Builder(String type) {
81 if (type == null) throw new IllegalArgumentException("Type cannot be null");
86 public Builder(ChampObject object) {
87 type = object.getType();
88 key = object.getKey();
89 properties(object.getProperties());
92 public Builder key(Object key) {
93 if (key == null) throw new IllegalArgumentException("Key cannot be set to null");
95 this.key = Optional.of(key);
99 public Builder property(String key, Object value) {
100 if (key == null) throw new IllegalArgumentException("Property key cannot be null");
101 if (value == null) throw new IllegalArgumentException("Property value cannot be null");
103 if (ReservedPropertyKeys.contains(key)) throw new IllegalArgumentException("Property key " + key + " is reserved");
105 properties.put(key, value);
109 public Builder properties(Map<String, Object> properties) {
110 for (Entry<String, Object> property : properties.entrySet()) {
111 property(property.getKey(), property.getValue());
117 public ChampObject build() {
118 return new ChampObject(this);
123 public boolean equals(Object object) {
124 if (this == object) return true;
126 if (object instanceof ChampObject) {
127 final ChampObject champObj = (ChampObject) object;
129 if (getKey().isPresent() && champObj.getKey().isPresent()) {
131 if (getKey().get().equals(champObj.getKey().get())) return true;
133 } else if (!getKey().isPresent() && !champObj.getKey().isPresent()) {
134 if (getType().equals(champObj.getType()) &&
135 getProperties().equals(champObj.getProperties())) return true;
143 public int hashCode() {
144 if (getKey().isPresent()) return getKey().get().hashCode();
146 final int returnValue = 31 * (getType().hashCode() + getProperties().hashCode());
151 public String toString() {
152 return "{key: " + (getKey().isPresent() ? getKey().get() : "")
153 + ", type: " + getType()
154 + ", properties: " + getProperties() + "}";
157 public enum ReservedPropertyKeys {
158 CHAMP_OBJECT_TYPE ("aai_node_type"),
159 CHAMP_OBJECT_KEY ("key");
161 private final String text;
163 private ReservedPropertyKeys(final String text) {
168 public String toString() {
172 public static boolean contains(String key) {
173 for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
174 if (choice.toString().equals(key)) return true;
181 public enum IgnoreOnReadPropertyKeys {
182 CHAMP_IMPORT_ASSIGNED_ID ("importAssignedId");
184 private final String text;
186 private IgnoreOnReadPropertyKeys(final String text) {
191 public String toString() {
195 public static boolean contains(String key) {
196 for (IgnoreOnReadPropertyKeys choice : IgnoreOnReadPropertyKeys.values()) {
197 if (choice.toString().equals(key)) return true;
204 public enum ReservedTypes {
207 private final String text;
209 private ReservedTypes(final String text) {
214 public String toString() {
220 public boolean isObject() {
225 public ChampObject asObject() {
230 public boolean isRelationship() {
235 public ChampRelationship asRelationship() {
236 throw new UnsupportedOperationException("Cannot call asRelationship() on ChampObject");