2 * ============LICENSE_START==========================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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============================================
21 package org.onap.aai.champcore.model;
23 import java.util.HashMap;
24 import java.util.HashSet;
26 import java.util.Objects;
27 import java.util.Optional;
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;
35 @JsonDeserialize(builder = ChampRelationshipConstraint.Builder.class)
36 public final class ChampRelationshipConstraint {
38 private final String type;
39 private final Map<String, ChampPropertyConstraint> propertyConstraints;
40 private final Set<ChampConnectionConstraint> connectionConstraints;
42 private ChampRelationshipConstraint() {
43 throw new RuntimeException("Cannot call ChampObjectConstraint() constructor");
46 private ChampRelationshipConstraint(Builder builder) {
47 this.type = builder.type;
48 this.propertyConstraints = builder.propertyConstraints;
49 this.connectionConstraints = builder.connectionConstraints;
52 public String getType() { return type; }
53 public Set<ChampPropertyConstraint> getPropertyConstraints() { return new HashSet<ChampPropertyConstraint> (propertyConstraints.values()); }
55 public Optional<ChampPropertyConstraint> getPropertyConstraint(String fieldName) {
56 if (!propertyConstraints.containsKey(fieldName)) return Optional.empty();
58 return Optional.of(propertyConstraints.get(fieldName));
61 public Set<ChampConnectionConstraint> getConnectionConstraints() { return connectionConstraints; }
63 public Optional<ChampConnectionConstraint> getConnectionConstraint(ChampRelationship incidentRelationship) {
64 if (!incidentRelationship.getType().equals(getType())) return Optional.empty();
66 final String sourceType = incidentRelationship.getSource().getType();
67 final String targetType = incidentRelationship.getTarget().getType();
69 for (ChampConnectionConstraint connConstraint : getConnectionConstraints()) {
70 if (connConstraint.getSourceType().equals(sourceType) &&
71 connConstraint.getTargetType().equals(targetType)) return Optional.of(connConstraint);
74 return Optional.empty();
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;
83 public Builder(@JsonProperty("type") String type) {
85 this.propertyConstraints = new HashMap<String, ChampPropertyConstraint> ();
86 this.connectionConstraints = new HashSet<ChampConnectionConstraint> ();
89 public Builder propertyConstraints(Set<ChampPropertyConstraint> propertyConstraints) {
91 for (ChampPropertyConstraint propConstraint : propertyConstraints) {
92 constraint(propConstraint);
98 public Builder connectionConstraints(Set<ChampConnectionConstraint> connectionConstraints) {
99 this.connectionConstraints.addAll(connectionConstraints);
104 public Builder constraint(ChampPropertyConstraint propConstraint) {
105 propertyConstraints.put(propConstraint.getField().getName(), propConstraint);
110 public Builder constraint(ChampConnectionConstraint connConstraint) {
111 connectionConstraints.add(connConstraint);
114 public ChampRelationshipConstraint build() {
115 return new ChampRelationshipConstraint(this);
120 public String toString() {
121 return "{type: " + getType() +
122 ", connectionConstraints: " + getConnectionConstraints() +
123 ", propertyConstraints: " + getPropertyConstraints() +
128 public boolean equals(Object o) {
129 if (o instanceof ChampRelationshipConstraint) {
130 final ChampRelationshipConstraint relConstraint = (ChampRelationshipConstraint) o;
132 if (relConstraint.getType().equals(getType()) &&
133 relConstraint.getConnectionConstraints().equals(getConnectionConstraints()) &&
134 relConstraint.getPropertyConstraints().equals(getPropertyConstraints())) return true;
141 public int hashCode() {
142 return Objects.hash(getType(), getConnectionConstraints(), getPropertyConstraints());