Merge "Added @Override annotation above signature"
[aai/champ.git] / src / main / java / org / onap / aai / champ / model / ChampRelationshipConstraint.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.HashSet;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.Set;
29
30 import com.fasterxml.jackson.annotation.JsonIgnore;
31 import com.fasterxml.jackson.annotation.JsonProperty;
32 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
33 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
34
35 @JsonDeserialize(builder = ChampRelationshipConstraint.Builder.class)
36 public final class ChampRelationshipConstraint {
37
38         private final String type;
39         private final Map<String, ChampPropertyConstraint> propertyConstraints;
40         private final Set<ChampConnectionConstraint> connectionConstraints;
41
42         private ChampRelationshipConstraint() {
43                 throw new RuntimeException("Cannot call ChampObjectConstraint() constructor");
44         }
45         
46         private ChampRelationshipConstraint(Builder builder) {
47                 this.type = builder.type;
48                 this.propertyConstraints = builder.propertyConstraints;
49                 this.connectionConstraints = builder.connectionConstraints;
50         }
51         
52         public String getType() { return type; }
53         public Set<ChampPropertyConstraint> getPropertyConstraints() { return new HashSet<ChampPropertyConstraint> (propertyConstraints.values()); }
54         
55         public Optional<ChampPropertyConstraint> getPropertyConstraint(String fieldName) {
56                 if (!propertyConstraints.containsKey(fieldName)) return Optional.empty();
57
58                 return Optional.of(propertyConstraints.get(fieldName));
59         }
60
61         public Set<ChampConnectionConstraint> getConnectionConstraints() { return connectionConstraints; }
62
63         public Optional<ChampConnectionConstraint> getConnectionConstraint(ChampRelationship incidentRelationship) {
64                 if (!incidentRelationship.getType().equals(getType())) return Optional.empty();
65
66                 final String sourceType = incidentRelationship.getSource().getType();
67                 final String targetType = incidentRelationship.getTarget().getType();
68
69                 for (ChampConnectionConstraint connConstraint : getConnectionConstraints()) {
70                         if (connConstraint.getSourceType().equals(sourceType) &&
71                                 connConstraint.getTargetType().equals(targetType)) return Optional.of(connConstraint);
72                 }
73
74                 return Optional.empty();
75         }
76
77         @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "")
78         public static class Builder {
79                 private final String type;
80                 private final Map<String, ChampPropertyConstraint> propertyConstraints;
81                 private final Set<ChampConnectionConstraint> connectionConstraints;
82
83                 public Builder(@JsonProperty("type") String type) {
84                         this.type = type;
85                         this.propertyConstraints = new HashMap<String, ChampPropertyConstraint> ();
86                         this.connectionConstraints = new HashSet<ChampConnectionConstraint> ();
87                 }
88
89                 public Builder propertyConstraints(Set<ChampPropertyConstraint> propertyConstraints) {
90
91                         for (ChampPropertyConstraint propConstraint : propertyConstraints) {
92                                 constraint(propConstraint);
93                         }
94
95                         return this;
96                 }
97
98                 public Builder connectionConstraints(Set<ChampConnectionConstraint> connectionConstraints) {
99                         this.connectionConstraints.addAll(connectionConstraints);
100                         return this;
101                 }
102
103                 @JsonIgnore
104                 public Builder constraint(ChampPropertyConstraint propConstraint) {
105                         propertyConstraints.put(propConstraint.getField().getName(), propConstraint);
106                         return this;
107                 }
108
109                 @JsonIgnore
110                 public Builder constraint(ChampConnectionConstraint connConstraint) {
111                         connectionConstraints.add(connConstraint);
112                         return this;
113                 }
114                 public ChampRelationshipConstraint build() {
115                         return new ChampRelationshipConstraint(this);
116                 }
117         }
118
119         @Override
120         public String toString() {
121                 return "{type: " + getType() +
122                                 ", connectionConstraints: " + getConnectionConstraints() +
123                                 ", propertyConstraints: " + getPropertyConstraints() +
124                                 "}";
125         }
126  
127         @Override
128         public boolean equals(Object o) {
129                 if (o instanceof ChampRelationshipConstraint) {
130                         final ChampRelationshipConstraint relConstraint = (ChampRelationshipConstraint) o;
131
132                         if (relConstraint.getType().equals(getType()) &&
133                                 relConstraint.getConnectionConstraints().equals(getConnectionConstraints()) &&
134                                 relConstraint.getPropertyConstraints().equals(getPropertyConstraints())) return true;
135                 }
136                 
137                 return false;
138         }
139 }