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