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