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.Optional;
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;
34 @JsonDeserialize(builder = ChampRelationshipConstraint.Builder.class)
35 public final class ChampRelationshipConstraint {
37 private final String type;
38 private final Map<String, ChampPropertyConstraint> propertyConstraints;
39 private final Set<ChampConnectionConstraint> connectionConstraints;
41 private ChampRelationshipConstraint() {
42 throw new RuntimeException("Cannot call ChampObjectConstraint() constructor");
45 private ChampRelationshipConstraint(Builder builder) {
46 this.type = builder.type;
47 this.propertyConstraints = builder.propertyConstraints;
48 this.connectionConstraints = builder.connectionConstraints;
51 public String getType() { return type; }
52 public Set<ChampPropertyConstraint> getPropertyConstraints() { return new HashSet<ChampPropertyConstraint> (propertyConstraints.values()); }
54 public Optional<ChampPropertyConstraint> getPropertyConstraint(String fieldName) {
55 if (!propertyConstraints.containsKey(fieldName)) return Optional.empty();
57 return Optional.of(propertyConstraints.get(fieldName));
60 public Set<ChampConnectionConstraint> getConnectionConstraints() { return connectionConstraints; }
62 public Optional<ChampConnectionConstraint> getConnectionConstraint(ChampRelationship incidentRelationship) {
63 if (!incidentRelationship.getType().equals(getType())) return Optional.empty();
65 final String sourceType = incidentRelationship.getSource().getType();
66 final String targetType = incidentRelationship.getTarget().getType();
68 for (ChampConnectionConstraint connConstraint : getConnectionConstraints()) {
69 if (connConstraint.getSourceType().equals(sourceType) &&
70 connConstraint.getTargetType().equals(targetType)) return Optional.of(connConstraint);
73 return Optional.empty();
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;
82 public Builder(@JsonProperty("type") String type) {
84 this.propertyConstraints = new HashMap<String, ChampPropertyConstraint> ();
85 this.connectionConstraints = new HashSet<ChampConnectionConstraint> ();
88 public Builder propertyConstraints(Set<ChampPropertyConstraint> propertyConstraints) {
90 for (ChampPropertyConstraint propConstraint : propertyConstraints) {
91 constraint(propConstraint);
97 public Builder connectionConstraints(Set<ChampConnectionConstraint> connectionConstraints) {
98 this.connectionConstraints.addAll(connectionConstraints);
103 public Builder constraint(ChampPropertyConstraint propConstraint) {
104 propertyConstraints.put(propConstraint.getField().getName(), propConstraint);
109 public Builder constraint(ChampConnectionConstraint connConstraint) {
110 connectionConstraints.add(connConstraint);
113 public ChampRelationshipConstraint build() {
114 return new ChampRelationshipConstraint(this);
119 public String toString() {
120 return "{type: " + getType() +
121 ", connectionConstraints: " + getConnectionConstraints() +
122 ", propertyConstraints: " + getPropertyConstraints() +
127 public boolean equals(Object o) {
128 if (o instanceof ChampRelationshipConstraint) {
129 final ChampRelationshipConstraint relConstraint = (ChampRelationshipConstraint) o;
131 if (relConstraint.getType().equals(getType()) &&
132 relConstraint.getConnectionConstraints().equals(getConnectionConstraints()) &&
133 relConstraint.getPropertyConstraints().equals(getPropertyConstraints())) return true;