Merge "Added @Override annotation above signature"
[aai/champ.git] / src / main / java / org / onap / aai / champ / model / ChampSchema.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
21  */
22 package org.onap.aai.champ.model;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
28 import org.onap.aai.champ.model.fluent.schema.CreateChampSchemable;
29 import org.onap.aai.champ.model.fluent.schema.impl.CreateChampSchemableImpl;
30
31 import java.util.Optional;
32
33 import com.fasterxml.jackson.annotation.JsonIgnore;
34 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
35 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
36
37 @JsonDeserialize(builder = ChampSchema.Builder.class)
38 public final class ChampSchema {
39
40         private final Map<String, ChampObjectConstraint> objectConstraints;
41         private final Map<String, ChampRelationshipConstraint> relationshipConstraints;
42
43         private ChampSchema() {
44                 throw new RuntimeException("Cannot call ChampSchema() constructor");
45         }
46         
47         private ChampSchema(Builder builder) {
48                 this.objectConstraints = builder.objectConstraints;
49                 this.relationshipConstraints = builder.relationshipConstraints;
50         }
51
52         public static CreateChampSchemable create() {
53                 return new CreateChampSchemableImpl();
54         }
55
56         public Optional<ChampObjectConstraint> getObjectConstraint(String type) {
57                 if (!getObjectConstraints().containsKey(type)) return Optional.empty();
58                 
59                 return Optional.of(getObjectConstraints().get(type));
60         }
61         
62         public Optional<ChampRelationshipConstraint> getRelationshipConstraint(String type) {
63                 if (!getRelationshipConstraints().containsKey(type)) return Optional.empty();
64                 
65                 return Optional.of(getRelationshipConstraints().get(type));
66         }
67
68         public Map<String, ChampObjectConstraint> getObjectConstraints() {
69                 return objectConstraints;
70         }
71
72         public Map<String, ChampRelationshipConstraint> getRelationshipConstraints() {
73                 return relationshipConstraints;
74         }
75
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> ();
80         
81                 public Builder() {}
82
83                 public Builder(ChampSchema schema) {
84                         objectConstraints.putAll(schema.getObjectConstraints());
85                         relationshipConstraints.putAll(schema.getRelationshipConstraints());
86                 }
87
88                 public Builder objectConstraints(Map<String, ChampObjectConstraint> objectConstraints) {
89                         this.objectConstraints.putAll(objectConstraints);
90                         return this;
91                 }
92
93                 public Builder relationshipConstraints(Map<String, ChampRelationshipConstraint> relationshipConstraints) {
94                         this.relationshipConstraints.putAll(relationshipConstraints);
95                         return this;
96                 }
97
98                 @JsonIgnore
99                 public Builder constraint(ChampObjectConstraint objConstraint) {
100                         objectConstraints.put(objConstraint.getType(), objConstraint);
101                         return this;
102                 }
103
104                 @JsonIgnore
105                 public Builder constraint(ChampRelationshipConstraint relConstraint) {
106                         relationshipConstraints.put(relConstraint.getType(), relConstraint);
107                         return this;
108                 }
109
110                 public ChampSchema build() {
111                         return new ChampSchema(this);
112                 }
113         }
114
115         @Override
116         public boolean equals(Object schema) {
117                 if (schema instanceof ChampSchema) {
118
119                         for (Entry<String, ChampObjectConstraint> objConstraint : getObjectConstraints().entrySet()) {
120                                 final Optional<ChampObjectConstraint> champObjConstraint = ((ChampSchema) schema).getObjectConstraint(objConstraint.getKey());
121                                 
122                                 if (!champObjConstraint.isPresent() ||
123                                         !champObjConstraint.get().equals(objConstraint.getValue())) return false;
124                         }
125                         
126                         for (Entry<String, ChampRelationshipConstraint> relConstraint : getRelationshipConstraints().entrySet()) {
127                                 final Optional<ChampRelationshipConstraint> champRelConstraint = ((ChampSchema) schema).getRelationshipConstraint(relConstraint.getKey());
128                                 
129                                 if (!champRelConstraint.isPresent() ||
130                                         !champRelConstraint.get().equals(relConstraint.getValue())) return false;
131                         }
132                         
133                         return true;
134                 }
135                 
136                 return false;
137         }
138
139         @Override
140         public String toString() {
141                 return "{objectConstraints: " + getObjectConstraints() +
142                                 ", relationshipConstraints: " + getRelationshipConstraints() +
143                                 "}";
144         }
145
146         public static ChampSchema emptySchema() {
147                 return new ChampSchema.Builder().build();
148         }
149 }