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() { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 333 | return Objects.hash(deviceId, selector, priority, tableId, payLoad); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | public int hash() { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 337 | return Objects.hash(deviceId, selector, treatment, tableId, payLoad); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | @Override |
| 341 | /* |
| 342 | * The priority and statistics can change on a given treatment and selector |
| 343 | * |
| 344 | * (non-Javadoc) |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 345 | * |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 346 | * @see java.lang.Object#equals(java.lang.Object) |
| 347 | */ |
| 348 | public boolean equals(Object obj) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 349 | if (this == obj) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 350 | return true; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 351 | } |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 352 | if (obj instanceof DefaultFlowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 353 | DefaultFlowRule that = (DefaultFlowRule) obj; |
| 354 | return Objects.equals(deviceId, that.deviceId) && |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 355 | Objects.equals(priority, that.priority) && |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 356 | Objects.equals(selector, that.selector) && |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 357 | Objects.equals(tableId, that.tableId) |
| 358 | && Objects.equals(payLoad, that.payLoad); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 359 | } |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 360 | return false; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 363 | @Override |
| 364 | public String toString() { |
| 365 | return toStringHelper(this) |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 366 | .add("id", Long.toHexString(id.value())) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 367 | .add("deviceId", deviceId) |
| 368 | .add("priority", priority) |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 369 | .add("selector", selector.criteria()) |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 370 | .add("treatment", treatment == null ? "N/A" : treatment.allInstructions()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 371 | .add("tableId", tableId) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 372 | .add("created", created) |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 373 | .add("payLoad", payLoad).toString() |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 374 | .toString(); |
| 375 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 376 | |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 377 | @Override |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 378 | public int timeout() { |
alshabib | 58747a6 | 2014-10-07 11:05:30 -0700 | [diff] [blame] | 379 | return timeout; |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 382 | @Override |
| 383 | public boolean isPermanent() { |
| 384 | return permanent; |
| 385 | } |
| 386 | |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 387 | @Override |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 388 | public int tableId() { |
| 389 | return tableId; |
| 390 | } |
| 391 | |
| 392 | public static Builder builder() { |
| 393 | return new Builder(); |
| 394 | } |
| 395 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 396 | public static final class Builder implements FlowRule.Builder { |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 397 | |
| 398 | private FlowId flowId; |
| 399 | private Integer priority; |
| 400 | private DeviceId deviceId; |
| 401 | private Integer tableId = 0; |
| 402 | private TrafficSelector selector; |
| 403 | private TrafficTreatment treatment; |
| 404 | private Integer timeout; |
| 405 | private Boolean permanent; |
| 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) { |
| 415 | this.flowId = computeFlowId(appId); |
| 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) { |
| 445 | this.treatment = treatment; |
| 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 |
| 464 | public FlowRule build() { |
| 465 | checkNotNull(flowId != null, "Either an application" + |
| 466 | " id or a cookie must be supplied"); |
| 467 | checkNotNull(selector != null, "Traffic selector cannot be null"); |
| 468 | checkNotNull(timeout != null || permanent != null, "Must either have " + |
| 469 | "a timeout or be permanent"); |
| 470 | checkNotNull(deviceId != null, "Must refer to a device"); |
| 471 | checkNotNull(priority != null, "Priority cannot be null"); |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 472 | checkArgument(priority >= MIN_PRIORITY, "Priority cannot be less than " + |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 473 | MIN_PRIORITY); |
| 474 | |
| 475 | return new DefaultFlowRule(deviceId, selector, treatment, priority, |
| 476 | flowId, permanent, timeout, tableId); |
| 477 | } |
| 478 | |
| 479 | private FlowId computeFlowId(ApplicationId appId) { |
| 480 | return FlowId.valueOf((((long) appId.id()) << 48) |
| 481 | | (hash() & 0xffffffffL)); |
| 482 | } |
| 483 | |
| 484 | private int hash() { |
| 485 | return Objects.hash(deviceId, selector, treatment, tableId); |
| 486 | } |
| 487 | |
| 488 | } |
| 489 | |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 490 | @Override |
| 491 | public FlowRuleExtPayLoad payLoad() { |
| 492 | return payLoad; |
| 493 | } |
| 494 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 495 | } |