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