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