Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Hyunsun Moon | 7bfbc1c | 2016-04-05 16:40:43 -0700 | [diff] [blame] | 19 | import com.google.common.base.Charsets; |
| 20 | import com.google.common.hash.Funnel; |
| 21 | import com.google.common.hash.HashCode; |
| 22 | import com.google.common.hash.HashFunction; |
| 23 | import com.google.common.hash.Hashing; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.DefaultGroupId; |
| 26 | import org.onosproject.core.GroupId; |
| 27 | import org.onosproject.net.DeviceId; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 28 | |
Jonathan Hart | 607b1ff | 2015-05-05 10:55:51 -0700 | [diff] [blame] | 29 | import java.util.Objects; |
| 30 | |
| 31 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 32 | import static com.google.common.base.Preconditions.checkArgument; |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 35 | public class DefaultFlowRule implements FlowRule { |
| 36 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 37 | private final DeviceId deviceId; |
| 38 | private final int priority; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 39 | private final TrafficSelector selector; |
| 40 | private final TrafficTreatment treatment; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 41 | private final long created; |
| 42 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 43 | private final FlowId id; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 44 | |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 45 | private final Short appId; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 46 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 47 | private final int timeout; |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 48 | private final boolean permanent; |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 49 | private final int hardTimeout; |
| 50 | private final FlowRemoveReason reason; |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 51 | private final GroupId groupId; |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 52 | |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 53 | private final Integer tableId; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 54 | private final FlowRuleExtPayLoad payLoad; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 55 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 56 | public DefaultFlowRule(FlowRule rule) { |
| 57 | this.deviceId = rule.deviceId(); |
| 58 | this.priority = rule.priority(); |
| 59 | this.selector = rule.selector(); |
| 60 | this.treatment = rule.treatment(); |
| 61 | this.appId = rule.appId(); |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 62 | this.groupId = rule.groupId(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 63 | this.id = rule.id(); |
| 64 | this.timeout = rule.timeout(); |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 65 | this.hardTimeout = rule.hardTimeout(); |
| 66 | this.reason = rule.reason(); |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 67 | this.permanent = rule.isPermanent(); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 68 | this.created = System.currentTimeMillis(); |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 69 | this.tableId = rule.tableId(); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 70 | this.payLoad = rule.payLoad(); |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
| 74 | TrafficTreatment treatment, Integer priority, |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 75 | FlowId flowId, Boolean permanent, Integer timeout, Integer hardTimeout, |
| 76 | FlowRemoveReason reason, Integer tableId) { |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 77 | |
| 78 | this.deviceId = deviceId; |
| 79 | this.selector = selector; |
| 80 | this.treatment = treatment; |
| 81 | this.priority = priority; |
| 82 | this.appId = (short) (flowId.value() >>> 48); |
| 83 | this.id = flowId; |
| 84 | this.permanent = permanent; |
| 85 | this.timeout = timeout; |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 86 | this.hardTimeout = hardTimeout; |
| 87 | this.reason = reason; |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 88 | this.tableId = tableId; |
| 89 | this.created = System.currentTimeMillis(); |
| 90 | |
| 91 | |
| 92 | //FIXME: fields below will be removed. |
Jonathan Hart | 607b1ff | 2015-05-05 10:55:51 -0700 | [diff] [blame] | 93 | this.groupId = new DefaultGroupId(0); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 94 | this.payLoad = null; |
| 95 | } |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 96 | |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 97 | |
| 98 | |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 99 | /** |
| 100 | * Support for the third party flow rule. Creates a flow rule of flow table. |
| 101 | * |
| 102 | * @param deviceId the identity of the device where this rule applies |
| 103 | * @param selector the traffic selector that identifies what traffic this |
| 104 | * rule |
| 105 | * @param treatment the traffic treatment that applies to selected traffic |
| 106 | * @param priority the flow rule priority given in natural order |
| 107 | * @param appId the application id of this flow |
| 108 | * @param timeout the timeout for this flow requested by an application |
| 109 | * @param permanent whether the flow is permanent i.e. does not time out |
| 110 | * @param payLoad 3rd-party origin private flow |
| 111 | */ |
| 112 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
| 113 | TrafficTreatment treatment, int priority, |
| 114 | ApplicationId appId, int timeout, boolean permanent, |
| 115 | FlowRuleExtPayLoad payLoad) { |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 116 | this(deviceId, selector, treatment, priority, appId, timeout, 0, permanent, payLoad); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * Support for the third party flow rule. Creates a flow rule of flow table. |
| 122 | * |
| 123 | * @param deviceId the identity of the device where this rule applies |
| 124 | * @param selector the traffic selector that identifies what traffic this |
| 125 | * rule |
| 126 | * @param treatment the traffic treatment that applies to selected traffic |
| 127 | * @param priority the flow rule priority given in natural order |
| 128 | * @param appId the application id of this flow |
| 129 | * @param timeout the timeout for this flow requested by an application |
| 130 | * @param hardTimeout the hard timeout located switch's flow table for this flow requested by an application |
| 131 | * @param permanent whether the flow is permanent i.e. does not time out |
| 132 | * @param payLoad 3rd-party origin private flow |
| 133 | */ |
| 134 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
| 135 | TrafficTreatment treatment, int priority, |
| 136 | ApplicationId appId, int timeout, int hardTimeout, boolean permanent, |
| 137 | FlowRuleExtPayLoad payLoad) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 138 | |
Kavitha Alagesan | 14dc513 | 2016-06-13 17:25:18 +0530 | [diff] [blame] | 139 | checkArgument(priority >= MIN_PRIORITY, "Priority cannot be less than " + |
| 140 | MIN_PRIORITY); |
| 141 | checkArgument(priority <= MAX_PRIORITY, "Priority cannot be greater than " + |
| 142 | MAX_PRIORITY); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 143 | |
| 144 | this.deviceId = deviceId; |
| 145 | this.priority = priority; |
| 146 | this.selector = selector; |
| 147 | this.treatment = treatment; |
| 148 | this.appId = appId.id(); |
| 149 | this.groupId = new DefaultGroupId(0); |
| 150 | this.timeout = timeout; |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 151 | this.reason = FlowRemoveReason.NO_REASON; |
| 152 | this.hardTimeout = hardTimeout; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 153 | this.permanent = permanent; |
| 154 | this.tableId = 0; |
| 155 | this.created = System.currentTimeMillis(); |
| 156 | this.payLoad = payLoad; |
| 157 | |
| 158 | /* |
| 159 | * id consists of the following. | appId (16 bits) | groupId (16 bits) | |
| 160 | * flowId (32 bits) | |
| 161 | */ |
| 162 | this.id = FlowId.valueOf((((long) this.appId) << 48) |
| 163 | | (((long) this.groupId.id()) << 32) |
| 164 | | (this.hash() & 0xffffffffL)); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Support for the third party flow rule. Creates a flow rule of group |
| 169 | * table. |
| 170 | * |
| 171 | * @param deviceId the identity of the device where this rule applies |
| 172 | * @param selector the traffic selector that identifies what traffic this |
| 173 | * rule |
| 174 | * @param treatment the traffic treatment that applies to selected traffic |
| 175 | * @param priority the flow rule priority given in natural order |
| 176 | * @param appId the application id of this flow |
| 177 | * @param groupId the group id of this flow |
| 178 | * @param timeout the timeout for this flow requested by an application |
| 179 | * @param permanent whether the flow is permanent i.e. does not time out |
| 180 | * @param payLoad 3rd-party origin private flow |
| 181 | * |
| 182 | */ |
| 183 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
| 184 | TrafficTreatment treatment, int priority, |
| 185 | ApplicationId appId, GroupId groupId, int timeout, |
| 186 | boolean permanent, FlowRuleExtPayLoad payLoad) { |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 187 | this(deviceId, selector, treatment, priority, appId, groupId, timeout, 0, permanent, payLoad); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Support for the third party flow rule. Creates a flow rule of group |
| 192 | * table. |
| 193 | * |
| 194 | * @param deviceId the identity of the device where this rule applies |
| 195 | * @param selector the traffic selector that identifies what traffic this |
| 196 | * rule |
| 197 | * @param treatment the traffic treatment that applies to selected traffic |
| 198 | * @param priority the flow rule priority given in natural order |
| 199 | * @param appId the application id of this flow |
| 200 | * @param groupId the group id of this flow |
| 201 | * @param timeout the timeout for this flow requested by an application |
| 202 | * @param hardTimeout the hard timeout located switch's flow table for this flow requested by an application |
| 203 | * @param permanent whether the flow is permanent i.e. does not time out |
| 204 | * @param payLoad 3rd-party origin private flow |
| 205 | * |
| 206 | */ |
| 207 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
| 208 | TrafficTreatment treatment, int priority, |
| 209 | ApplicationId appId, GroupId groupId, int timeout, int hardTimeout, |
| 210 | boolean permanent, FlowRuleExtPayLoad payLoad) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 211 | |
Kavitha Alagesan | 14dc513 | 2016-06-13 17:25:18 +0530 | [diff] [blame] | 212 | checkArgument(priority >= MIN_PRIORITY, "Priority cannot be less than " + |
| 213 | MIN_PRIORITY); |
| 214 | checkArgument(priority <= MAX_PRIORITY, "Priority cannot be greater than " + |
| 215 | MAX_PRIORITY); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 216 | |
| 217 | this.deviceId = deviceId; |
| 218 | this.priority = priority; |
| 219 | this.selector = selector; |
| 220 | this.treatment = treatment; |
| 221 | this.appId = appId.id(); |
| 222 | this.groupId = groupId; |
| 223 | this.timeout = timeout; |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 224 | this.reason = FlowRemoveReason.NO_REASON; |
| 225 | this.hardTimeout = hardTimeout; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 226 | this.permanent = permanent; |
| 227 | this.created = System.currentTimeMillis(); |
| 228 | this.tableId = 0; |
| 229 | this.payLoad = payLoad; |
| 230 | |
| 231 | /* |
| 232 | * id consists of the following. | appId (16 bits) | groupId (16 bits) | |
| 233 | * flowId (32 bits) | |
| 234 | */ |
| 235 | this.id = FlowId.valueOf((((long) this.appId) << 48) |
| 236 | | (((long) this.groupId.id()) << 32) |
| 237 | | (this.hash() & 0xffffffffL)); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 240 | @Override |
| 241 | public FlowId id() { |
| 242 | return id; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | @Override |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 246 | public short appId() { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 247 | return appId; |
| 248 | } |
| 249 | |
| 250 | @Override |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 251 | public GroupId groupId() { |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 252 | return groupId; |
| 253 | } |
| 254 | |
| 255 | @Override |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 256 | public int priority() { |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 257 | return priority; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public DeviceId deviceId() { |
| 262 | return deviceId; |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public TrafficSelector selector() { |
| 267 | return selector; |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public TrafficTreatment treatment() { |
| 272 | return treatment; |
| 273 | } |
| 274 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 275 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 276 | /* |
| 277 | * The priority and statistics can change on a given treatment and selector |
| 278 | * |
| 279 | * (non-Javadoc) |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 280 | * |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 281 | * @see java.lang.Object#equals(java.lang.Object) |
| 282 | */ |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 283 | public int hashCode() { |
Thomas Vachuska | 711db55 | 2015-06-02 16:35:26 -0700 | [diff] [blame] | 284 | return Objects.hash(deviceId, selector, tableId, payLoad); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Thomas Vachuska | 711db55 | 2015-06-02 16:35:26 -0700 | [diff] [blame] | 287 | //FIXME do we need this method in addition to hashCode()? |
| 288 | private int hash() { |
| 289 | return Objects.hash(deviceId, selector, tableId, payLoad); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Override |
| 293 | /* |
| 294 | * The priority and statistics can change on a given treatment and selector |
| 295 | * |
| 296 | * (non-Javadoc) |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 297 | * |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 298 | * @see java.lang.Object#equals(java.lang.Object) |
| 299 | */ |
| 300 | public boolean equals(Object obj) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 301 | if (this == obj) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 302 | return true; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 303 | } |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 304 | if (obj instanceof DefaultFlowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 305 | DefaultFlowRule that = (DefaultFlowRule) obj; |
| 306 | return Objects.equals(deviceId, that.deviceId) && |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 307 | Objects.equals(priority, that.priority) && |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 308 | Objects.equals(selector, that.selector) && |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 309 | Objects.equals(tableId, that.tableId) |
| 310 | && Objects.equals(payLoad, that.payLoad); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 311 | } |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 312 | return false; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 315 | @Override |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 316 | public boolean exactMatch(FlowRule rule) { |
| 317 | return this.equals(rule) && |
| 318 | Objects.equals(this.id, rule.id()) && |
| 319 | Objects.equals(this.treatment, rule.treatment()); |
| 320 | } |
| 321 | |
| 322 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 323 | public String toString() { |
| 324 | return toStringHelper(this) |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 325 | .add("id", Long.toHexString(id.value())) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 326 | .add("deviceId", deviceId) |
| 327 | .add("priority", priority) |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 328 | .add("selector", selector.criteria()) |
Jian Li | f032511 | 2016-03-03 17:11:25 -0800 | [diff] [blame] | 329 | .add("treatment", treatment == null ? "N/A" : treatment) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 330 | .add("tableId", tableId) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 331 | .add("created", created) |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 332 | .add("payLoad", payLoad) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 333 | .toString(); |
| 334 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 335 | |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 336 | @Override |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 337 | public int timeout() { |
alshabib | 58747a6 | 2014-10-07 11:05:30 -0700 | [diff] [blame] | 338 | return timeout; |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 341 | |
| 342 | |
| 343 | @Override |
| 344 | public int hardTimeout() { |
| 345 | return hardTimeout; |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public FlowRemoveReason reason() { |
| 350 | return reason; |
| 351 | } |
| 352 | |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 353 | @Override |
| 354 | public boolean isPermanent() { |
| 355 | return permanent; |
| 356 | } |
| 357 | |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 358 | @Override |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 359 | public int tableId() { |
| 360 | return tableId; |
| 361 | } |
| 362 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 363 | @Beta |
| 364 | public long created() { |
| 365 | return created; |
| 366 | } |
| 367 | |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 368 | public static Builder builder() { |
| 369 | return new Builder(); |
| 370 | } |
| 371 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 372 | public static final class Builder implements FlowRule.Builder { |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 373 | |
| 374 | private FlowId flowId; |
Jonathan Hart | 65d551b | 2015-09-22 09:49:09 -0700 | [diff] [blame] | 375 | private ApplicationId appId; |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 376 | private Integer priority; |
| 377 | private DeviceId deviceId; |
| 378 | private Integer tableId = 0; |
Charles Chan | cc4754d | 2015-12-10 15:58:18 -0800 | [diff] [blame] | 379 | private TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 380 | private TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 381 | private Integer timeout; |
| 382 | private Boolean permanent; |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 383 | private Integer hardTimeout = 0; |
| 384 | private FlowRemoveReason reason = FlowRemoveReason.NO_REASON; |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 385 | |
| 386 | @Override |
| 387 | public FlowRule.Builder withCookie(long cookie) { |
| 388 | this.flowId = FlowId.valueOf(cookie); |
| 389 | return this; |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public FlowRule.Builder fromApp(ApplicationId appId) { |
Jonathan Hart | 65d551b | 2015-09-22 09:49:09 -0700 | [diff] [blame] | 394 | this.appId = appId; |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 395 | return this; |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public FlowRule.Builder withPriority(int priority) { |
| 400 | this.priority = priority; |
| 401 | return this; |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public FlowRule.Builder forDevice(DeviceId deviceId) { |
| 406 | this.deviceId = deviceId; |
| 407 | return this; |
| 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public FlowRule.Builder forTable(int tableId) { |
| 412 | this.tableId = tableId; |
| 413 | return this; |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public FlowRule.Builder withSelector(TrafficSelector selector) { |
| 418 | this.selector = selector; |
| 419 | return this; |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public FlowRule.Builder withTreatment(TrafficTreatment treatment) { |
Jonathan Hart | 24e296b | 2016-01-12 14:47:07 -0800 | [diff] [blame] | 424 | this.treatment = checkNotNull(treatment); |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 425 | return this; |
| 426 | } |
| 427 | |
| 428 | @Override |
| 429 | public FlowRule.Builder makePermanent() { |
| 430 | this.timeout = 0; |
| 431 | this.permanent = true; |
| 432 | return this; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public FlowRule.Builder makeTemporary(int timeout) { |
| 437 | this.permanent = false; |
| 438 | this.timeout = timeout; |
| 439 | return this; |
| 440 | } |
| 441 | |
| 442 | @Override |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 443 | public FlowRule.Builder withHardTimeout(int timeout) { |
| 444 | this.permanent = false; |
| 445 | this.hardTimeout = timeout; |
| 446 | this.timeout = timeout; |
| 447 | return this; |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public FlowRule.Builder withReason(FlowRemoveReason reason) { |
| 452 | this.reason = reason; |
| 453 | return this; |
| 454 | } |
| 455 | |
| 456 | @Override |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 457 | public FlowRule build() { |
alshabib | b05be2d | 2016-04-11 12:52:36 -0700 | [diff] [blame] | 458 | FlowId localFlowId; |
Jian Li | 8bac576 | 2016-02-09 17:23:09 -0800 | [diff] [blame] | 459 | checkArgument((flowId != null) ^ (appId != null), "Either an application" + |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 460 | " id or a cookie must be supplied"); |
Jonathan Hart | 65d551b | 2015-09-22 09:49:09 -0700 | [diff] [blame] | 461 | checkNotNull(selector, "Traffic selector cannot be null"); |
| 462 | checkArgument(timeout != null || permanent != null, "Must either have " + |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 463 | "a timeout or be permanent"); |
Jonathan Hart | 65d551b | 2015-09-22 09:49:09 -0700 | [diff] [blame] | 464 | checkNotNull(deviceId, "Must refer to a device"); |
| 465 | checkNotNull(priority, "Priority cannot be null"); |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 466 | checkArgument(priority >= MIN_PRIORITY, "Priority cannot be less than " + |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 467 | MIN_PRIORITY); |
Kavitha Alagesan | 14dc513 | 2016-06-13 17:25:18 +0530 | [diff] [blame] | 468 | checkArgument(priority <= MAX_PRIORITY, "Priority cannot be greater than " + |
| 469 | MAX_PRIORITY); |
Jonathan Hart | 65d551b | 2015-09-22 09:49:09 -0700 | [diff] [blame] | 470 | // Computing a flow ID based on appId takes precedence over setting |
| 471 | // the flow ID directly |
| 472 | if (appId != null) { |
alshabib | b05be2d | 2016-04-11 12:52:36 -0700 | [diff] [blame] | 473 | localFlowId = computeFlowId(appId); |
| 474 | } else { |
| 475 | localFlowId = flowId; |
Jonathan Hart | 65d551b | 2015-09-22 09:49:09 -0700 | [diff] [blame] | 476 | } |
| 477 | |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 478 | return new DefaultFlowRule(deviceId, selector, treatment, priority, |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 479 | localFlowId, permanent, timeout, hardTimeout, reason, tableId); |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | private FlowId computeFlowId(ApplicationId appId) { |
| 483 | return FlowId.valueOf((((long) appId.id()) << 48) |
| 484 | | (hash() & 0xffffffffL)); |
| 485 | } |
| 486 | |
| 487 | private int hash() { |
Hyunsun Moon | 7bfbc1c | 2016-04-05 16:40:43 -0700 | [diff] [blame] | 488 | Funnel<TrafficSelector> selectorFunnel = (from, into) -> from.criteria() |
Hyunsun Moon | 7bfbc1c | 2016-04-05 16:40:43 -0700 | [diff] [blame] | 489 | .forEach(c -> into.putString(c.toString(), Charsets.UTF_8)); |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 490 | |
Hyunsun Moon | 7bfbc1c | 2016-04-05 16:40:43 -0700 | [diff] [blame] | 491 | HashFunction hashFunction = Hashing.murmur3_32(); |
| 492 | HashCode hashCode = hashFunction.newHasher() |
| 493 | .putString(deviceId.toString(), Charsets.UTF_8) |
| 494 | .putObject(selector, selectorFunnel) |
| 495 | .putInt(priority) |
| 496 | .putInt(tableId) |
| 497 | .hash(); |
| 498 | |
| 499 | return hashCode.asInt(); |
| 500 | } |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 501 | } |
| 502 | |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 503 | @Override |
| 504 | public FlowRuleExtPayLoad payLoad() { |
| 505 | return payLoad; |
| 506 | } |
| 507 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 508 | } |