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 com.fasterxml.jackson.annotation.JsonIgnore;
24 import com.fasterxml.jackson.annotation.JsonProperty;
26 import org.onap.aai.champcore.model.fluent.object.CreateChampObjectable;
27 import org.onap.aai.champcore.model.fluent.object.impl.CreateChampObjectableImpl;
29 import java.util.HashMap;
30 import java.util.HashSet;
32 import java.util.Map.Entry;
33 import java.util.Optional;
35 public final class ChampObject implements ChampElement {
38 private Optional<Object> key;
39 private Map<String, Object> properties;
41 public static CreateChampObjectable create() {
42 return new CreateChampObjectableImpl();
45 public ChampObject() {
48 private ChampObject(Builder builder) {
49 this.type = builder.type;
50 this.key = builder.key;
51 this.properties = builder.properties;
54 @SuppressWarnings("unchecked")
55 public <T> Optional<T> getProperty(String key) {
56 if (!properties.containsKey(key)) {
57 return Optional.empty();
60 return Optional.of((T) properties.get(key));
63 public String getType() {
68 public Optional<Object> getKey() {
72 public Map<String, Object> getProperties() {
77 public Object getKeyValue() {
78 return key.orElse("");
82 public ChampObject dropProperties(HashSet<String> safeProperties) {
83 HashSet<String> propertyKeys = new HashSet<>(properties.keySet());
84 for (String key : propertyKeys) {
85 if (!safeProperties.contains(key)) {
86 properties.remove(key);
93 public static class Builder {
94 private final String type;
95 private final Map<String, Object> properties = new HashMap<String, Object>();
97 private Optional<Object> key = Optional.empty();
99 public Builder(String type) {
101 throw new IllegalArgumentException("Type cannot be null");
107 public Builder(ChampObject object) {
108 type = object.getType();
109 key = object.getKey();
110 properties(object.getProperties());
113 public Builder key(Object key) {
115 throw new IllegalArgumentException("Key cannot be set to null");
118 this.key = Optional.of(key);
122 public Builder property(String key, Object value) {
124 throw new IllegalArgumentException("Property key cannot be null");
127 throw new IllegalArgumentException("Property value cannot be null");
130 if (ReservedPropertyKeys.contains(key)) {
131 throw new IllegalArgumentException("Property key " + key + " is reserved");
134 properties.put(key, value);
138 public Builder properties(Map<String, Object> properties) {
139 for (Entry<String, Object> property : properties.entrySet()) {
140 property(property.getKey(), property.getValue());
146 public ChampObject build() {
147 return new ChampObject(this);
152 public boolean equals(Object object) {
153 if (this == object) {
157 if (object instanceof ChampObject) {
158 final ChampObject champObj = (ChampObject) object;
160 if (getKey().isPresent() && champObj.getKey().isPresent()) {
162 if (getKey().get().equals(champObj.getKey().get())) {
166 } else if (!getKey().isPresent() && !champObj.getKey().isPresent()) {
167 if (getType().equals(champObj.getType()) &&
168 getProperties().equals(champObj.getProperties())) {
178 public int hashCode() {
179 if (getKey().isPresent()) {
180 return getKey().get().hashCode();
183 final int returnValue = 31 * (getType().hashCode() + getProperties().hashCode());
188 public String toString() {
189 return "{key: " + (getKey().isPresent() ? getKey().get() : "")
190 + ", type: " + getType()
191 + ", properties: " + getProperties() + "}";
194 public enum ReservedPropertyKeys {
195 CHAMP_OBJECT_TYPE("aai_node_type"),
196 CHAMP_OBJECT_KEY("key");
198 private final String text;
200 private ReservedPropertyKeys(final String text) {
205 public String toString() {
209 public static boolean contains(String key) {
210 for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
211 if (choice.toString().equals(key)) {
220 public enum IgnoreOnReadPropertyKeys {
221 CHAMP_IMPORT_ASSIGNED_ID("importAssignedId");
223 private final String text;
225 private IgnoreOnReadPropertyKeys(final String text) {
230 public String toString() {
234 public static boolean contains(String key) {
235 for (IgnoreOnReadPropertyKeys choice : IgnoreOnReadPropertyKeys.values()) {
236 if (choice.toString().equals(key)) {
245 public enum ReservedTypes {
248 private final String text;
250 private ReservedTypes(final String text) {
255 public String toString() {
261 public boolean isObject() {
266 public ChampObject asObject() {
271 public boolean isRelationship() {
276 public ChampRelationship asRelationship() {
277 throw new UnsupportedOperationException("Cannot call asRelationship() on ChampObject");