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