Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 23 | import java.util.Objects; |
| 24 | |
| 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 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; |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 46 | |
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, |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 50 | TrafficTreatment treatment, int priority, long flowId, |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 51 | 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; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 64 | } |
| 65 | |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 66 | @Deprecated |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 67 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 68 | TrafficTreatment treatment, int priority, long flowId, |
| 69 | int timeout, boolean permanent, Type tableType) { |
| 70 | this.deviceId = deviceId; |
| 71 | this.priority = priority; |
| 72 | this.selector = selector; |
| 73 | this.treatment = treatment; |
| 74 | this.timeout = timeout; |
| 75 | this.permanent = permanent; |
| 76 | this.created = System.currentTimeMillis(); |
| 77 | |
| 78 | this.appId = (short) (flowId >>> 48); |
| 79 | this.groupId = new DefaultGroupId((short) ((flowId >>> 32) & 0xFFFF)); |
| 80 | this.id = FlowId.valueOf(flowId); |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 81 | this.tableId = tableType.ordinal(); |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 82 | |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 83 | } |
| 84 | |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 85 | @Deprecated |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 86 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 87 | TrafficTreatment treatment, int priority, ApplicationId appId, |
| 88 | int timeout, boolean permanent) { |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 89 | this(deviceId, selector, treatment, priority, appId, new DefaultGroupId(0), |
| 90 | timeout, permanent); |
| 91 | } |
| 92 | |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 93 | @Deprecated |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 94 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
| 95 | TrafficTreatment treatment, int priority, ApplicationId appId, |
| 96 | int timeout, boolean permanent, Type type) { |
| 97 | |
| 98 | if (priority < FlowRule.MIN_PRIORITY) { |
| 99 | throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY); |
| 100 | } |
| 101 | |
| 102 | this.deviceId = deviceId; |
| 103 | this.priority = priority; |
| 104 | this.selector = selector; |
| 105 | this.treatment = treatment; |
| 106 | this.appId = appId.id(); |
| 107 | this.groupId = new DefaultGroupId(0); |
| 108 | this.timeout = timeout; |
| 109 | this.permanent = permanent; |
| 110 | this.created = System.currentTimeMillis(); |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 111 | this.tableId = type.ordinal(); |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * id consists of the following. |
| 115 | * | appId (16 bits) | groupId (16 bits) | flowId (32 bits) | |
| 116 | */ |
| 117 | this.id = FlowId.valueOf((((long) this.appId) << 48) | (((long) this.groupId.id()) << 32) |
| 118 | | (this.hash() & 0xffffffffL)); |
| 119 | |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 120 | } |
| 121 | |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 122 | @Deprecated |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 123 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
| 124 | TrafficTreatment treatment, int priority, ApplicationId appId, |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 125 | GroupId groupId, int timeout, boolean permanent) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 126 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 127 | if (priority < FlowRule.MIN_PRIORITY) { |
alshabib | a0e0498 | 2014-10-03 13:03:19 -0700 | [diff] [blame] | 128 | throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY); |
| 129 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 130 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 131 | this.deviceId = deviceId; |
| 132 | this.priority = priority; |
| 133 | this.selector = selector; |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 134 | this.treatment = treatment; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 135 | this.appId = appId.id(); |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 136 | this.groupId = groupId; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 137 | this.timeout = timeout; |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 138 | this.permanent = permanent; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 139 | this.created = System.currentTimeMillis(); |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 140 | this.tableId = 0; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 141 | |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 142 | /* |
| 143 | * id consists of the following. |
| 144 | * | appId (16 bits) | groupId (16 bits) | flowId (32 bits) | |
| 145 | */ |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 146 | this.id = FlowId.valueOf((((long) this.appId) << 48) | (((long) this.groupId.id()) << 32) |
| 147 | | (this.hash() & 0xffffffffL)); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 148 | } |
| 149 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 150 | public DefaultFlowRule(FlowRule rule) { |
| 151 | this.deviceId = rule.deviceId(); |
| 152 | this.priority = rule.priority(); |
| 153 | this.selector = rule.selector(); |
| 154 | this.treatment = rule.treatment(); |
| 155 | this.appId = rule.appId(); |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 156 | this.groupId = rule.groupId(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 157 | this.id = rule.id(); |
| 158 | this.timeout = rule.timeout(); |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 159 | this.permanent = rule.isPermanent(); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 160 | this.created = System.currentTimeMillis(); |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 161 | this.tableId = rule.tableId(); |
| 162 | |
| 163 | } |
| 164 | |
| 165 | private DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
| 166 | TrafficTreatment treatment, Integer priority, |
| 167 | FlowId flowId, Boolean permanent, Integer timeout, |
| 168 | Integer tableId) { |
| 169 | |
| 170 | this.deviceId = deviceId; |
| 171 | this.selector = selector; |
| 172 | this.treatment = treatment; |
| 173 | this.priority = priority; |
| 174 | this.appId = (short) (flowId.value() >>> 48); |
| 175 | this.id = flowId; |
| 176 | this.permanent = permanent; |
| 177 | this.timeout = timeout; |
| 178 | this.tableId = tableId; |
| 179 | this.created = System.currentTimeMillis(); |
| 180 | |
| 181 | |
| 182 | //FIXME: fields below will be removed. |
| 183 | this.groupId = null; |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 184 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 185 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 188 | @Override |
| 189 | public FlowId id() { |
| 190 | return id; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 194 | public short appId() { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 195 | return appId; |
| 196 | } |
| 197 | |
| 198 | @Override |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 199 | public GroupId groupId() { |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 200 | return groupId; |
| 201 | } |
| 202 | |
| 203 | @Override |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 204 | public int priority() { |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 205 | return priority; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public DeviceId deviceId() { |
| 210 | return deviceId; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public TrafficSelector selector() { |
| 215 | return selector; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public TrafficTreatment treatment() { |
| 220 | return treatment; |
| 221 | } |
| 222 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 223 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 224 | /* |
| 225 | * The priority and statistics can change on a given treatment and selector |
| 226 | * |
| 227 | * (non-Javadoc) |
| 228 | * @see java.lang.Object#equals(java.lang.Object) |
| 229 | */ |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 230 | public int hashCode() { |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 231 | return Objects.hash(deviceId, selector, priority, tableId); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | public int hash() { |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 235 | return Objects.hash(deviceId, selector, treatment, tableId); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | /* |
| 240 | * The priority and statistics can change on a given treatment and selector |
| 241 | * |
| 242 | * (non-Javadoc) |
| 243 | * @see java.lang.Object#equals(java.lang.Object) |
| 244 | */ |
| 245 | public boolean equals(Object obj) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 246 | if (this == obj) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 247 | return true; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 248 | } |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 249 | if (obj instanceof DefaultFlowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 250 | DefaultFlowRule that = (DefaultFlowRule) obj; |
| 251 | return Objects.equals(deviceId, that.deviceId) && |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 252 | Objects.equals(priority, that.priority) && |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 253 | Objects.equals(selector, that.selector) && |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 254 | Objects.equals(tableId, that.tableId); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 255 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 256 | } |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 257 | return false; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 260 | @Override |
| 261 | public String toString() { |
| 262 | return toStringHelper(this) |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 263 | .add("id", Long.toHexString(id.value())) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 264 | .add("deviceId", deviceId) |
| 265 | .add("priority", priority) |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 266 | .add("selector", selector.criteria()) |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 267 | .add("treatment", treatment == null ? "N/A" : treatment.allInstructions()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 268 | .add("tableId", tableId) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 269 | .add("created", created) |
| 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 | |
| 288 | public static Builder builder() { |
| 289 | return new Builder(); |
| 290 | } |
| 291 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 292 | public static final class Builder implements FlowRule.Builder { |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 293 | |
| 294 | private FlowId flowId; |
| 295 | private Integer priority; |
| 296 | private DeviceId deviceId; |
| 297 | private Integer tableId = 0; |
| 298 | private TrafficSelector selector; |
| 299 | private TrafficTreatment treatment; |
| 300 | private Integer timeout; |
| 301 | private Boolean permanent; |
| 302 | |
| 303 | @Override |
| 304 | public FlowRule.Builder withCookie(long cookie) { |
| 305 | this.flowId = FlowId.valueOf(cookie); |
| 306 | return this; |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public FlowRule.Builder fromApp(ApplicationId appId) { |
| 311 | this.flowId = computeFlowId(appId); |
| 312 | return this; |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public FlowRule.Builder withPriority(int priority) { |
| 317 | this.priority = priority; |
| 318 | return this; |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public FlowRule.Builder forDevice(DeviceId deviceId) { |
| 323 | this.deviceId = deviceId; |
| 324 | return this; |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public FlowRule.Builder forTable(int tableId) { |
| 329 | this.tableId = tableId; |
| 330 | return this; |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public FlowRule.Builder withSelector(TrafficSelector selector) { |
| 335 | this.selector = selector; |
| 336 | return this; |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public FlowRule.Builder withTreatment(TrafficTreatment treatment) { |
| 341 | this.treatment = treatment; |
| 342 | return this; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public FlowRule.Builder makePermanent() { |
| 347 | this.timeout = 0; |
| 348 | this.permanent = true; |
| 349 | return this; |
| 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public FlowRule.Builder makeTemporary(int timeout) { |
| 354 | this.permanent = false; |
| 355 | this.timeout = timeout; |
| 356 | return this; |
| 357 | } |
| 358 | |
| 359 | @Override |
| 360 | public FlowRule build() { |
| 361 | checkNotNull(flowId != null, "Either an application" + |
| 362 | " id or a cookie must be supplied"); |
| 363 | checkNotNull(selector != null, "Traffic selector cannot be null"); |
| 364 | checkNotNull(timeout != null || permanent != null, "Must either have " + |
| 365 | "a timeout or be permanent"); |
| 366 | checkNotNull(deviceId != null, "Must refer to a device"); |
| 367 | checkNotNull(priority != null, "Priority cannot be null"); |
| 368 | checkArgument(priority < MIN_PRIORITY, "Priority cannot be less than " + |
| 369 | MIN_PRIORITY); |
| 370 | |
| 371 | return new DefaultFlowRule(deviceId, selector, treatment, priority, |
| 372 | flowId, permanent, timeout, tableId); |
| 373 | } |
| 374 | |
| 375 | private FlowId computeFlowId(ApplicationId appId) { |
| 376 | return FlowId.valueOf((((long) appId.id()) << 48) |
| 377 | | (hash() & 0xffffffffL)); |
| 378 | } |
| 379 | |
| 380 | private int hash() { |
| 381 | return Objects.hash(deviceId, selector, treatment, tableId); |
| 382 | } |
| 383 | |
| 384 | } |
| 385 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 386 | } |