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.schema.CreateChampSchemable;
29 import org.onap.aai.champ.model.fluent.schema.impl.CreateChampSchemableImpl;
31 import java.util.Optional;
33 import com.fasterxml.jackson.annotation.JsonIgnore;
34 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
35 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
37 @JsonDeserialize(builder = ChampSchema.Builder.class)
38 public final class ChampSchema {
40 private final Map<String, ChampObjectConstraint> objectConstraints;
41 private final Map<String, ChampRelationshipConstraint> relationshipConstraints;
43 private ChampSchema() {
44 throw new RuntimeException("Cannot call ChampSchema() constructor");
47 private ChampSchema(Builder builder) {
48 this.objectConstraints = builder.objectConstraints;
49 this.relationshipConstraints = builder.relationshipConstraints;
52 public static CreateChampSchemable create() {
53 return new CreateChampSchemableImpl();
56 public Optional<ChampObjectConstraint> getObjectConstraint(String type) {
57 if (!getObjectConstraints().containsKey(type)) return Optional.empty();
59 return Optional.of(getObjectConstraints().get(type));
62 public Optional<ChampRelationshipConstraint> getRelationshipConstraint(String type) {
63 if (!getRelationshipConstraints().containsKey(type)) return Optional.empty();
65 return Optional.of(getRelationshipConstraints().get(type));
68 public Map<String, ChampObjectConstraint> getObjectConstraints() {
69 return objectConstraints;
72 public Map<String, ChampRelationshipConstraint> getRelationshipConstraints() {
73 return relationshipConstraints;
76 @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "")
77 public static class Builder {
78 private Map<String, ChampObjectConstraint> objectConstraints = new HashMap<String, ChampObjectConstraint> ();
79 private Map<String, ChampRelationshipConstraint> relationshipConstraints = new HashMap<String, ChampRelationshipConstraint> ();
83 public Builder(ChampSchema schema) {
84 objectConstraints.putAll(schema.getObjectConstraints());
85 relationshipConstraints.putAll(schema.getRelationshipConstraints());
88 public Builder objectConstraints(Map<String, ChampObjectConstraint> objectConstraints) {
89 this.objectConstraints.putAll(objectConstraints);
93 public Builder relationshipConstraints(Map<String, ChampRelationshipConstraint> relationshipConstraints) {
94 this.relationshipConstraints.putAll(relationshipConstraints);
99 public Builder constraint(ChampObjectConstraint objConstraint) {
100 objectConstraints.put(objConstraint.getType(), objConstraint);
105 public Builder constraint(ChampRelationshipConstraint relConstraint) {
106 relationshipConstraints.put(relConstraint.getType(), relConstraint);
110 public ChampSchema build() {
111 return new ChampSchema(this);
116 public boolean equals(Object schema) {
117 if (schema instanceof ChampSchema) {
119 for (Entry<String, ChampObjectConstraint> objConstraint : getObjectConstraints().entrySet()) {
120 final Optional<ChampObjectConstraint> champObjConstraint = ((ChampSchema) schema).getObjectConstraint(objConstraint.getKey());
122 if (!champObjConstraint.isPresent() ||
123 !champObjConstraint.get().equals(objConstraint.getValue())) return false;
126 for (Entry<String, ChampRelationshipConstraint> relConstraint : getRelationshipConstraints().entrySet()) {
127 final Optional<ChampRelationshipConstraint> champRelConstraint = ((ChampSchema) schema).getRelationshipConstraint(relConstraint.getKey());
129 if (!champRelConstraint.isPresent() ||
130 !champRelConstraint.get().equals(relConstraint.getValue())) return false;
140 public String toString() {
141 return "{objectConstraints: " + getObjectConstraints() +
142 ", relationshipConstraints: " + getRelationshipConstraints() +
146 public static ChampSchema emptySchema() {
147 return new ChampSchema.Builder().build();