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