Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.flow; |
| 17 | |
Jonathan Hart | d87aeca | 2014-10-21 10:42:52 -0700 | [diff] [blame] | 18 | import java.util.HashMap; |
| 19 | import java.util.Map; |
| 20 | import java.util.Objects; |
| 21 | import java.util.Set; |
| 22 | |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.PortNumber; |
| 24 | import org.onlab.onos.net.flow.criteria.Criteria; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.flow.criteria.Criterion; |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IpPrefix; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 29 | |
Jonathan Hart | d87aeca | 2014-10-21 10:42:52 -0700 | [diff] [blame] | 30 | import com.google.common.base.MoreObjects; |
| 31 | import com.google.common.collect.ImmutableSet; |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Default traffic selector implementation. |
| 35 | */ |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 36 | public final class DefaultTrafficSelector implements TrafficSelector { |
| 37 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 38 | private final Set<Criterion> criteria; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 39 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Creates a new traffic selector with the specified criteria. |
| 42 | * |
| 43 | * @param criteria criteria |
| 44 | */ |
| 45 | private DefaultTrafficSelector(Set<Criterion> criteria) { |
Yuta HIGUCHI | 2809bf3 | 2014-10-20 22:44:12 -0700 | [diff] [blame] | 46 | this.criteria = ImmutableSet.copyOf(criteria); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 50 | public Set<Criterion> criteria() { |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 51 | return criteria; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 52 | } |
| 53 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 54 | @Override |
Jonathan Hart | 936c49d | 2014-10-23 16:38:59 -0700 | [diff] [blame] | 55 | public Criterion getCriterion(Criterion.Type type) { |
| 56 | for (Criterion c : criteria) { |
| 57 | if (c.type() == type) { |
| 58 | return c; |
| 59 | } |
| 60 | } |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | @Override |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 65 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 66 | return criteria.hashCode(); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public boolean equals(Object obj) { |
| 71 | if (this == obj) { |
| 72 | return true; |
| 73 | } |
| 74 | if (obj instanceof DefaultTrafficSelector) { |
| 75 | DefaultTrafficSelector that = (DefaultTrafficSelector) obj; |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 76 | return Objects.equals(criteria, that.criteria); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 77 | |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
Jonathan Hart | d87aeca | 2014-10-21 10:42:52 -0700 | [diff] [blame] | 82 | @Override |
| 83 | public String toString() { |
| 84 | return MoreObjects.toStringHelper(getClass()) |
| 85 | .add("criteria", criteria) |
| 86 | .toString(); |
| 87 | } |
| 88 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Returns a new traffic selector builder. |
| 91 | * |
| 92 | * @return traffic selector builder |
| 93 | */ |
| 94 | public static TrafficSelector.Builder builder() { |
| 95 | return new Builder(); |
| 96 | } |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 97 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Returns a new traffic selector builder primed to produce entities |
| 100 | * patterned after the supplied selector. |
| 101 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 102 | * @param selector base selector |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 103 | * @return traffic selector builder |
| 104 | */ |
| 105 | public static TrafficSelector.Builder builder(TrafficSelector selector) { |
| 106 | return new Builder(selector); |
| 107 | } |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 108 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 109 | /** |
| 110 | * Builder of traffic selector entities. |
| 111 | */ |
| 112 | public static final class Builder implements TrafficSelector.Builder { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 113 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 114 | private final Map<Criterion.Type, Criterion> selector = new HashMap<>(); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 115 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 116 | private Builder() { |
| 117 | } |
| 118 | |
| 119 | private Builder(TrafficSelector selector) { |
| 120 | for (Criterion c : selector.criteria()) { |
| 121 | add(c); |
| 122 | } |
| 123 | } |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 124 | |
| 125 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 126 | public Builder add(Criterion criterion) { |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 127 | selector.put(criterion.type(), criterion); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 128 | return this; |
| 129 | } |
| 130 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 131 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 132 | public Builder matchInport(PortNumber port) { |
| 133 | return add(Criteria.matchInPort(port)); |
| 134 | } |
| 135 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 136 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 137 | public Builder matchEthSrc(MacAddress addr) { |
| 138 | return add(Criteria.matchEthSrc(addr)); |
| 139 | } |
| 140 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 141 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 142 | public Builder matchEthDst(MacAddress addr) { |
| 143 | return add(Criteria.matchEthDst(addr)); |
| 144 | } |
| 145 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 146 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 147 | public Builder matchEthType(short ethType) { |
| 148 | return add(Criteria.matchEthType(ethType)); |
| 149 | } |
| 150 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 151 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 152 | public Builder matchVlanId(VlanId vlanId) { |
| 153 | return add(Criteria.matchVlanId(vlanId)); |
| 154 | } |
| 155 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 156 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 157 | public Builder matchVlanPcp(Byte vlanPcp) { |
| 158 | return add(Criteria.matchVlanPcp(vlanPcp)); |
| 159 | } |
| 160 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 161 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 162 | public Builder matchIPProtocol(Byte proto) { |
| 163 | return add(Criteria.matchIPProtocol(proto)); |
| 164 | } |
| 165 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 166 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 167 | public Builder matchIPSrc(IpPrefix ip) { |
| 168 | return add(Criteria.matchIPSrc(ip)); |
| 169 | } |
| 170 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 171 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 172 | public Builder matchIPDst(IpPrefix ip) { |
| 173 | return add(Criteria.matchIPDst(ip)); |
| 174 | } |
| 175 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 176 | @Override |
Toshio Koide | 9c44c9a | 2014-10-09 11:44:36 -0700 | [diff] [blame] | 177 | public Builder matchTcpSrc(Short tcpPort) { |
| 178 | return add(Criteria.matchTcpSrc(tcpPort)); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public Builder matchTcpDst(Short tcpPort) { |
| 183 | return add(Criteria.matchTcpDst(tcpPort)); |
| 184 | } |
| 185 | |
| 186 | @Override |
Praseed Balakrishnan | 64369da | 2014-10-23 15:55:20 -0700 | [diff] [blame] | 187 | public Builder matchLambda(Short lambda) { |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 188 | return add(Criteria.matchLambda(lambda)); |
| 189 | } |
| 190 | |
| 191 | @Override |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 192 | public Builder matchOpticalSignalType(Short signalType) { |
Praseed Balakrishnan | 64369da | 2014-10-23 15:55:20 -0700 | [diff] [blame] | 193 | return add(Criteria.matchOpticalSignalType(signalType)); |
| 194 | |
| 195 | } |
| 196 | |
| 197 | @Override |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 198 | public TrafficSelector build() { |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 199 | return new DefaultTrafficSelector(ImmutableSet.copyOf(selector.values())); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | } |
| 203 | |
| 204 | } |