Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.constraint; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.Link; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 21 | import org.onosproject.net.intent.ResourceContext; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 22 | |
| 23 | import java.util.Objects; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
| 31 | * Constraint that evaluates links based on their type. |
| 32 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 33 | @Beta |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 34 | public class LinkTypeConstraint extends BooleanConstraint { |
| 35 | |
| 36 | private final Set<Link.Type> types; |
| 37 | private final boolean isInclusive; |
| 38 | |
| 39 | /** |
| 40 | * Creates a new constraint for requesting connectivity using or avoiding |
| 41 | * the specified link types. |
| 42 | * |
| 43 | * @param inclusive indicates whether the given link types are to be |
| 44 | * permitted or avoided |
| 45 | * @param types link types |
| 46 | */ |
| 47 | public LinkTypeConstraint(boolean inclusive, Link.Type... types) { |
| 48 | checkNotNull(types, "Link types cannot be null"); |
| 49 | checkArgument(types.length > 0, "There must be more than one type"); |
| 50 | this.types = ImmutableSet.copyOf(types); |
| 51 | this.isInclusive = inclusive; |
| 52 | } |
| 53 | |
Thomas Vachuska | 7d0032b | 2014-11-04 17:39:57 -0800 | [diff] [blame] | 54 | // Constructor for serialization |
| 55 | private LinkTypeConstraint() { |
| 56 | this.types = null; |
| 57 | this.isInclusive = false; |
| 58 | } |
| 59 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 60 | // doesn't use LinkResourceService |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 61 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 62 | public boolean isValid(Link link, ResourceContext context) { |
| 63 | // explicitly call a method not depending on LinkResourceService |
| 64 | return isValid(link); |
| 65 | } |
| 66 | |
| 67 | private boolean isValid(Link link) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 68 | boolean contains = types.contains(link.type()); |
Sho SHIMIZU | 36ef79f | 2016-02-15 18:47:08 -0800 | [diff] [blame] | 69 | return isInclusive == contains; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the set of link types. |
| 74 | * |
| 75 | * @return set of link types |
| 76 | */ |
| 77 | public Set<Link.Type> types() { |
| 78 | return types; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Indicates if the constraint is inclusive or exclusive. |
| 83 | * |
| 84 | * @return true if inclusive |
| 85 | */ |
| 86 | public boolean isInclusive() { |
| 87 | return isInclusive; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int hashCode() { |
| 92 | return Objects.hash(types, isInclusive); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean equals(Object obj) { |
| 97 | if (this == obj) { |
| 98 | return true; |
| 99 | } |
| 100 | if (obj == null || getClass() != obj.getClass()) { |
| 101 | return false; |
| 102 | } |
| 103 | final LinkTypeConstraint other = (LinkTypeConstraint) obj; |
| 104 | return Objects.equals(this.types, other.types) && Objects.equals(this.isInclusive, other.isInclusive); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | return toStringHelper(this) |
| 110 | .add("inclusive", isInclusive) |
| 111 | .add("types", types) |
| 112 | .toString(); |
| 113 | } |
| 114 | } |