blob: ffa4405b2281d675c4bf522c2e4422e8637fda97 [file] [log] [blame]
Thomas Vachuskaedc944c2014-11-04 15:42:25 -08001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.constraint;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080017
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080019import com.google.common.collect.ImmutableSet;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.Link;
Brian O'Connor6de2e202015-05-21 14:30:41 -070021import org.onosproject.net.resource.link.LinkResourceService;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080022
23import java.util.Objects;
24import java.util.Set;
25
26import static com.google.common.base.MoreObjects.toStringHelper;
27import static com.google.common.base.Preconditions.checkArgument;
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Constraint that evaluates links based on their type.
32 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040033@Beta
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080034public 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 Vachuska7d0032b2014-11-04 17:39:57 -080054 // Constructor for serialization
55 private LinkTypeConstraint() {
56 this.types = null;
57 this.isInclusive = false;
58 }
59
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080060 @Override
61 public boolean isValid(Link link, LinkResourceService resourceService) {
62 boolean contains = types.contains(link.type());
63 return isInclusive ? contains : !contains;
64 }
65
66 /**
67 * Returns the set of link types.
68 *
69 * @return set of link types
70 */
71 public Set<Link.Type> types() {
72 return types;
73 }
74
75 /**
76 * Indicates if the constraint is inclusive or exclusive.
77 *
78 * @return true if inclusive
79 */
80 public boolean isInclusive() {
81 return isInclusive;
82 }
83
84 @Override
85 public int hashCode() {
86 return Objects.hash(types, isInclusive);
87 }
88
89 @Override
90 public boolean equals(Object obj) {
91 if (this == obj) {
92 return true;
93 }
94 if (obj == null || getClass() != obj.getClass()) {
95 return false;
96 }
97 final LinkTypeConstraint other = (LinkTypeConstraint) obj;
98 return Objects.equals(this.types, other.types) && Objects.equals(this.isInclusive, other.isInclusive);
99 }
100
101 @Override
102 public String toString() {
103 return toStringHelper(this)
104 .add("inclusive", isInclusive)
105 .add("types", types)
106 .toString();
107 }
108}