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.schema.CreateChampSchemable;
30 import org.openecomp.aai.champ.model.fluent.schema.impl.CreateChampSchemableImpl;
32 import com.fasterxml.jackson.annotation.JsonIgnore;
33 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
34 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
36 @JsonDeserialize(builder = ChampSchema.Builder.class)
37 public final class ChampSchema {
39 private final Map<String, ChampObjectConstraint> objectConstraints;
40 private final Map<String, ChampRelationshipConstraint> relationshipConstraints;
42 private ChampSchema() {
43 throw new RuntimeException("Cannot call ChampSchema() constructor");
46 private ChampSchema(Builder builder) {
47 this.objectConstraints = builder.objectConstraints;
48 this.relationshipConstraints = builder.relationshipConstraints;
51 public static CreateChampSchemable create() {
52 return new CreateChampSchemableImpl();
55 public Optional<ChampObjectConstraint> getObjectConstraint(String type) {
56 if (!getObjectConstraints().containsKey(type)) return Optional.empty();
58 return Optional.of(getObjectConstraints().get(type));
61 public Optional<ChampRelationshipConstraint> getRelationshipConstraint(String type) {
62 if (!getRelationshipConstraints().containsKey(type)) return Optional.empty();
64 return Optional.of(getRelationshipConstraints().get(type));
67 public Map<String, ChampObjectConstraint> getObjectConstraints() {
68 return objectConstraints;
71 public Map<String, ChampRelationshipConstraint> getRelationshipConstraints() {
72 return relationshipConstraints;
75 @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "")
76 public static class Builder {
77 private Map<String, ChampObjectConstraint> objectConstraints = new HashMap<String, ChampObjectConstraint> ();
78 private Map<String, ChampRelationshipConstraint> relationshipConstraints = new HashMap<String, ChampRelationshipConstraint> ();
82 public Builder(ChampSchema schema) {
83 objectConstraints.putAll(schema.getObjectConstraints());
84 relationshipConstraints.putAll(schema.getRelationshipConstraints());
87 public Builder objectConstraints(Map<String, ChampObjectConstraint> objectConstraints) {
88 this.objectConstraints.putAll(objectConstraints);
92 public Builder relationshipConstraints(Map<String, ChampRelationshipConstraint> relationshipConstraints) {
93 this.relationshipConstraints.putAll(relationshipConstraints);
98 public Builder constraint(ChampObjectConstraint objConstraint) {
99 objectConstraints.put(objConstraint.getType(), objConstraint);
104 public Builder constraint(ChampRelationshipConstraint relConstraint) {
105 relationshipConstraints.put(relConstraint.getType(), relConstraint);
109 public ChampSchema build() {
110 return new ChampSchema(this);
115 public boolean equals(Object schema) {
116 if (schema instanceof ChampSchema) {
118 for (Entry<String, ChampObjectConstraint> objConstraint : getObjectConstraints().entrySet()) {
119 final Optional<ChampObjectConstraint> champObjConstraint = ((ChampSchema) schema).getObjectConstraint(objConstraint.getKey());
121 if (!champObjConstraint.isPresent() ||
122 !champObjConstraint.get().equals(objConstraint.getValue())) return false;
125 for (Entry<String, ChampRelationshipConstraint> relConstraint : getRelationshipConstraints().entrySet()) {
126 final Optional<ChampRelationshipConstraint> champRelConstraint = ((ChampSchema) schema).getRelationshipConstraint(relConstraint.getKey());
128 if (!champRelConstraint.isPresent() ||
129 !champRelConstraint.get().equals(relConstraint.getValue())) return false;
139 public String toString() {
140 return "{objectConstraints: " + getObjectConstraints() +
141 ", relationshipConstraints: " + getRelationshipConstraints() +
145 public static ChampSchema emptySchema() {
146 return new ChampSchema.Builder().build();