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