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