Merge "Tests for neutron-ovsdb"
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / dto / EndpointConstraint.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.groupbasedpolicy.dto;
10
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import javax.annotation.concurrent.Immutable;
19
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.EndpointIdentificationConstraints;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.l3.endpoint.identification.constraints.PrefixConstraint;
24
25 /**
26  * Represents constraints for an endpoint.
27  */
28 @Immutable
29 public class EndpointConstraint {
30
31     private final ConditionSet conditionSet;
32     private final Set<PrefixConstraint> l3EpPrefixes;
33
34     public EndpointConstraint(
35             @Nullable ConditionSet conditionSet,
36             @Nullable EndpointIdentificationConstraints consEpIdentificationConstraint) {
37         if (conditionSet == null) {
38             this.conditionSet = ConditionSet.EMPTY;
39         } else {
40             this.conditionSet = conditionSet;
41         }
42         if (consEpIdentificationConstraint == null
43                 || consEpIdentificationConstraint
44                         .getL3EndpointIdentificationConstraints() == null
45                 || consEpIdentificationConstraint
46                         .getL3EndpointIdentificationConstraints()
47                         .getPrefixConstraint() == null) {
48             l3EpPrefixes = Collections.emptySet();
49         } else {
50             l3EpPrefixes = new HashSet<>(consEpIdentificationConstraint
51                     .getL3EndpointIdentificationConstraints()
52                     .getPrefixConstraint());
53         }
54     }
55
56     /**
57      * @param epConditions
58      *            {@code null} means empty list
59      * @return {@code true} endpoint's conditions match against conditions from
60      *         condition-matchers
61      */
62     public boolean conditionsMatch(
63             @Nullable final List<ConditionName> epConditions) {
64         if (epConditions == null) {
65             return conditionSet
66                     .matches(Collections.<ConditionName> emptyList());
67         }
68         return conditionSet.matches(epConditions);
69     }
70
71     public @Nonnull ConditionSet getConditionSet() {
72         return conditionSet;
73     }
74
75     public @Nonnull Set<PrefixConstraint> getL3EpPrefixes() {
76         return l3EpPrefixes;
77     }
78
79     public static Set<IpPrefix> getIpPrefixesFrom(
80             Set<PrefixConstraint> prefixConstraints) {
81         Set<IpPrefix> ipPrefixes = new HashSet<>();
82         for (PrefixConstraint prefixConstraint : prefixConstraints) {
83             ipPrefixes.add(prefixConstraint.getIpPrefix());
84         }
85         return ipPrefixes;
86     }
87
88     @Override
89     public int hashCode() {
90         final int prime = 31;
91         int result = 1;
92         result = prime * result
93                 + ((conditionSet == null) ? 0 : conditionSet.hashCode());
94         result = prime * result
95                 + ((l3EpPrefixes == null) ? 0 : l3EpPrefixes.hashCode());
96         return result;
97     }
98
99     @Override
100     public boolean equals(Object obj) {
101         if (this == obj)
102             return true;
103         if (obj == null)
104             return false;
105         if (getClass() != obj.getClass())
106             return false;
107         EndpointConstraint other = (EndpointConstraint) obj;
108         if (conditionSet == null) {
109             if (other.conditionSet != null)
110                 return false;
111         } else if (!conditionSet.equals(other.conditionSet))
112             return false;
113         if (l3EpPrefixes == null) {
114             if (other.l3EpPrefixes != null)
115                 return false;
116         } else if (!l3EpPrefixes.equals(other.l3EpPrefixes))
117             return false;
118         return true;
119     }
120
121 }