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.champcore.model;
24 import com.fasterxml.jackson.annotation.JsonIgnore;
25 import com.fasterxml.jackson.annotation.JsonProperty;
27 import org.onap.aai.champcore.model.fluent.object.CreateChampObjectable;
28 import org.onap.aai.champcore.model.fluent.object.impl.CreateChampObjectableImpl;
30 import java.util.HashMap;
31 import java.util.HashSet;
33 import java.util.Map.Entry;
34 import java.util.Optional;
36 public final class ChampObject implements ChampElement {
39 private Optional<Object> key;
40 private Map<String, Object> properties;
42 public static CreateChampObjectable create() {
43 return new CreateChampObjectableImpl();
46 public ChampObject() {
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)) {
58 return Optional.empty();
61 return Optional.of((T) properties.get(key));
64 public String getType() {
69 public Optional<Object> getKey() {
73 public Map<String, Object> getProperties() {
78 public Object getKeyValue() {
79 return key.orElse("");
83 public ChampObject dropProperties(HashSet<String> safeProperties) {
84 HashSet<String> propertyKeys = new HashSet<>(properties.keySet());
85 for (String key : propertyKeys) {
86 if (!safeProperties.contains(key)) {
87 properties.remove(key);
94 public static class Builder {
95 private final String type;
96 private final Map<String, Object> properties = new HashMap<String, Object>();
98 private Optional<Object> key = Optional.empty();
100 public Builder(String type) {
102 throw new IllegalArgumentException("Type cannot be null");
108 public Builder(ChampObject object) {
109 type = object.getType();
110 key = object.getKey();
111 properties(object.getProperties());
114 public Builder key(Object key) {
116 throw new IllegalArgumentException("Key cannot be set to null");
119 this.key = Optional.of(key);
123 public Builder property(String key, Object value) {
125 throw new IllegalArgumentException("Property key cannot be null");
128 throw new IllegalArgumentException("Property value cannot be null");
131 if (ReservedPropertyKeys.contains(key)) {
132 throw new IllegalArgumentException("Property key " + key + " is reserved");
135 properties.put(key, value);
139 public Builder properties(Map<String, Object> properties) {
140 for (Entry<String, Object> property : properties.entrySet()) {
141 property(property.getKey(), property.getValue());
147 public ChampObject build() {
148 return new ChampObject(this);
153 public boolean equals(Object object) {
154 if (this == object) {
158 if (object instanceof ChampObject) {
159 final ChampObject champObj = (ChampObject) object;
161 if (getKey().isPresent() && champObj.getKey().isPresent()) {
163 if (getKey().get().equals(champObj.getKey().get())) {
167 } else if (!getKey().isPresent() && !champObj.getKey().isPresent()) {
168 if (getType().equals(champObj.getType()) &&
169 getProperties().equals(champObj.getProperties())) {
179 public int hashCode() {
180 if (getKey().isPresent()) {
181 return getKey().get().hashCode();
184 final int returnValue = 31 * (getType().hashCode() + getProperties().hashCode());
189 public String toString() {
190 return "{key: " + (getKey().isPresent() ? getKey().get() : "")
191 + ", type: " + getType()
192 + ", properties: " + getProperties() + "}";
195 public enum ReservedPropertyKeys {
196 CHAMP_OBJECT_TYPE("aai_node_type"),
197 CHAMP_OBJECT_KEY("key");
199 private final String text;
201 private ReservedPropertyKeys(final String text) {
206 public String toString() {
210 public static boolean contains(String key) {
211 for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
212 if (choice.toString().equals(key)) {
221 public enum IgnoreOnReadPropertyKeys {
222 CHAMP_IMPORT_ASSIGNED_ID("importAssignedId");
224 private final String text;
226 private IgnoreOnReadPropertyKeys(final String text) {
231 public String toString() {
235 public static boolean contains(String key) {
236 for (IgnoreOnReadPropertyKeys choice : IgnoreOnReadPropertyKeys.values()) {
237 if (choice.toString().equals(key)) {
246 public enum ReservedTypes {
249 private final String text;
251 private ReservedTypes(final String text) {
256 public String toString() {
262 public boolean isObject() {
267 public ChampObject asObject() {
272 public boolean isRelationship() {
277 public ChampRelationship asRelationship() {
278 throw new UnsupportedOperationException("Cannot call asRelationship() on ChampObject");