Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.Link; |
| 20 | import org.onosproject.net.resource.LinkResourceService; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 21 | |
| 22 | import java.util.Objects; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * Constraint that evaluates links based on their type. |
| 31 | */ |
| 32 | public class LinkTypeConstraint extends BooleanConstraint { |
| 33 | |
| 34 | private final Set<Link.Type> types; |
| 35 | private final boolean isInclusive; |
| 36 | |
| 37 | /** |
| 38 | * Creates a new constraint for requesting connectivity using or avoiding |
| 39 | * the specified link types. |
| 40 | * |
| 41 | * @param inclusive indicates whether the given link types are to be |
| 42 | * permitted or avoided |
| 43 | * @param types link types |
| 44 | */ |
| 45 | public LinkTypeConstraint(boolean inclusive, Link.Type... types) { |
| 46 | checkNotNull(types, "Link types cannot be null"); |
| 47 | checkArgument(types.length > 0, "There must be more than one type"); |
| 48 | this.types = ImmutableSet.copyOf(types); |
| 49 | this.isInclusive = inclusive; |
| 50 | } |
| 51 | |
Thomas Vachuska | 7d0032b | 2014-11-04 17:39:57 -0800 | [diff] [blame] | 52 | // Constructor for serialization |
| 53 | private LinkTypeConstraint() { |
| 54 | this.types = null; |
| 55 | this.isInclusive = false; |
| 56 | } |
| 57 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 58 | @Override |
| 59 | public boolean isValid(Link link, LinkResourceService resourceService) { |
| 60 | boolean contains = types.contains(link.type()); |
| 61 | return isInclusive ? contains : !contains; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the set of link types. |
| 66 | * |
| 67 | * @return set of link types |
| 68 | */ |
| 69 | public Set<Link.Type> types() { |
| 70 | return types; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Indicates if the constraint is inclusive or exclusive. |
| 75 | * |
| 76 | * @return true if inclusive |
| 77 | */ |
| 78 | public boolean isInclusive() { |
| 79 | return isInclusive; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public int hashCode() { |
| 84 | return Objects.hash(types, isInclusive); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public boolean equals(Object obj) { |
| 89 | if (this == obj) { |
| 90 | return true; |
| 91 | } |
| 92 | if (obj == null || getClass() != obj.getClass()) { |
| 93 | return false; |
| 94 | } |
| 95 | final LinkTypeConstraint other = (LinkTypeConstraint) obj; |
| 96 | return Objects.equals(this.types, other.types) && Objects.equals(this.isInclusive, other.isInclusive); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public String toString() { |
| 101 | return toStringHelper(this) |
| 102 | .add("inclusive", isInclusive) |
| 103 | .add("types", types) |
| 104 | .toString(); |
| 105 | } |
| 106 | } |