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