blob: c45c3d9d1955e3356ee0b57356585686b4191b10 [file] [log] [blame]
Thomas Vachuskaedc944c2014-11-04 15:42:25 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuskaedc944c2014-11-04 15:42:25 -08003 *
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;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080021import org.onosproject.net.intent.ResourceContext;
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
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080060 // doesn't use LinkResourceService
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080061 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080062 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 Vachuskaedc944c2014-11-04 15:42:25 -080068 boolean contains = types.contains(link.type());
Sho SHIMIZU36ef79f2016-02-15 18:47:08 -080069 return isInclusive == contains;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080070 }
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}