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 java.util.HashMap;
25 import java.util.Map.Entry;
26 import java.util.Optional;
28 import org.onap.aai.champcore.model.fluent.schema.CreateChampSchemable;
29 import org.onap.aai.champcore.model.fluent.schema.impl.CreateChampSchemableImpl;
31 import com.fasterxml.jackson.annotation.JsonIgnore;
32 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
33 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
35 @JsonDeserialize(builder = ChampSchema.Builder.class)
36 public final class ChampSchema {
38 private final Map<String, ChampObjectConstraint> objectConstraints;
39 private final Map<String, ChampRelationshipConstraint> relationshipConstraints;
41 private ChampSchema() {
42 throw new RuntimeException("Cannot call ChampSchema() constructor");
45 private ChampSchema(Builder builder) {
46 this.objectConstraints = builder.objectConstraints;
47 this.relationshipConstraints = builder.relationshipConstraints;
50 public static CreateChampSchemable create() {
51 return new CreateChampSchemableImpl();
54 public Optional<ChampObjectConstraint> getObjectConstraint(String type) {
55 if (!getObjectConstraints().containsKey(type)) return Optional.empty();
57 return Optional.of(getObjectConstraints().get(type));
60 public Optional<ChampRelationshipConstraint> getRelationshipConstraint(String type) {
61 if (!getRelationshipConstraints().containsKey(type)) return Optional.empty();
63 return Optional.of(getRelationshipConstraints().get(type));
66 public Map<String, ChampObjectConstraint> getObjectConstraints() {
67 return objectConstraints;
70 public Map<String, ChampRelationshipConstraint> getRelationshipConstraints() {
71 return relationshipConstraints;
74 @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "")
75 public static class Builder {
76 private Map<String, ChampObjectConstraint> objectConstraints = new HashMap<String, ChampObjectConstraint> ();
77 private Map<String, ChampRelationshipConstraint> relationshipConstraints = new HashMap<String, ChampRelationshipConstraint> ();
81 public Builder(ChampSchema schema) {
82 objectConstraints.putAll(schema.getObjectConstraints());
83 relationshipConstraints.putAll(schema.getRelationshipConstraints());
86 public Builder objectConstraints(Map<String, ChampObjectConstraint> objectConstraints) {
87 this.objectConstraints.putAll(objectConstraints);
91 public Builder relationshipConstraints(Map<String, ChampRelationshipConstraint> relationshipConstraints) {
92 this.relationshipConstraints.putAll(relationshipConstraints);
97 public Builder constraint(ChampObjectConstraint objConstraint) {
98 objectConstraints.put(objConstraint.getType(), objConstraint);
103 public Builder constraint(ChampRelationshipConstraint relConstraint) {
104 relationshipConstraints.put(relConstraint.getType(), relConstraint);
108 public ChampSchema build() {
109 return new ChampSchema(this);
114 public boolean equals(Object schema) {
115 if (schema instanceof ChampSchema) {
117 for (Entry<String, ChampObjectConstraint> objConstraint : getObjectConstraints().entrySet()) {
118 final Optional<ChampObjectConstraint> champObjConstraint = ((ChampSchema) schema).getObjectConstraint(objConstraint.getKey());
120 if (!champObjConstraint.isPresent() ||
121 !champObjConstraint.get().equals(objConstraint.getValue())) return false;
124 for (Entry<String, ChampRelationshipConstraint> relConstraint : getRelationshipConstraints().entrySet()) {
125 final Optional<ChampRelationshipConstraint> champRelConstraint = ((ChampSchema) schema).getRelationshipConstraint(relConstraint.getKey());
127 if (!champRelConstraint.isPresent() ||
128 !champRelConstraint.get().equals(relConstraint.getValue())) return false;
138 public String toString() {
139 return "{objectConstraints: " + getObjectConstraints() +
140 ", relationshipConstraints: " + getRelationshipConstraints() +
144 public static ChampSchema emptySchema() {
145 return new ChampSchema.Builder().build();