blob: 56405915099a26595f7f2857b63a41a09a78dc6e [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 */
16package org.onlab.onos.net.intent.constraint;
17
18import com.google.common.collect.ImmutableSet;
19import org.onlab.onos.net.Link;
20import org.onlab.onos.net.resource.LinkResourceService;
21
22import java.util.Objects;
23import java.util.Set;
24
25import static com.google.common.base.MoreObjects.toStringHelper;
26import static com.google.common.base.Preconditions.checkArgument;
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Constraint that evaluates links based on their type.
31 */
32public 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
52 @Override
53 public boolean isValid(Link link, LinkResourceService resourceService) {
54 boolean contains = types.contains(link.type());
55 return isInclusive ? contains : !contains;
56 }
57
58 /**
59 * Returns the set of link types.
60 *
61 * @return set of link types
62 */
63 public Set<Link.Type> types() {
64 return types;
65 }
66
67 /**
68 * Indicates if the constraint is inclusive or exclusive.
69 *
70 * @return true if inclusive
71 */
72 public boolean isInclusive() {
73 return isInclusive;
74 }
75
76 @Override
77 public int hashCode() {
78 return Objects.hash(types, isInclusive);
79 }
80
81 @Override
82 public boolean equals(Object obj) {
83 if (this == obj) {
84 return true;
85 }
86 if (obj == null || getClass() != obj.getClass()) {
87 return false;
88 }
89 final LinkTypeConstraint other = (LinkTypeConstraint) obj;
90 return Objects.equals(this.types, other.types) && Objects.equals(this.isInclusive, other.isInclusive);
91 }
92
93 @Override
94 public String toString() {
95 return toStringHelper(this)
96 .add("inclusive", isInclusive)
97 .add("types", types)
98 .toString();
99 }
100}