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