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