blob: 7d44b4de21ca4db020bec9f8c52f7152c6c46a0b [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
Thomas Vachuska7d0032b2014-11-04 17:39:57 -080052 // Constructor for serialization
53 private LinkTypeConstraint() {
54 this.types = null;
55 this.isInclusive = false;
56 }
57
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080058 @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}