Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | 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; |
| 26 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 27 | public class DefaultFlowRule implements FlowRule { |
| 28 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 29 | private final DeviceId deviceId; |
| 30 | private final int priority; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 31 | private final TrafficSelector selector; |
| 32 | private final TrafficTreatment treatment; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 33 | private final long created; |
| 34 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 35 | private final FlowId id; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 36 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 37 | private final short appId; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 38 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 39 | private final int timeout; |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 40 | private final boolean permanent; |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 41 | private final GroupId groupId; |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 42 | |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 43 | private final Type type; |
| 44 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 45 | |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 46 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 47 | TrafficTreatment treatment, int priority, long flowId, |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 48 | int timeout, boolean permanent) { |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 49 | this.deviceId = deviceId; |
| 50 | this.priority = priority; |
| 51 | this.selector = selector; |
| 52 | this.treatment = treatment; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 53 | this.timeout = timeout; |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 54 | this.permanent = permanent; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 55 | this.created = System.currentTimeMillis(); |
| 56 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 57 | this.appId = (short) (flowId >>> 48); |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 58 | this.groupId = new DefaultGroupId((short) ((flowId >>> 32) & 0xFFFF)); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 59 | this.id = FlowId.valueOf(flowId); |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 60 | this.type = Type.DEFAULT; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 61 | } |
| 62 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 63 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 64 | TrafficTreatment treatment, int priority, long flowId, |
| 65 | int timeout, boolean permanent, Type tableType) { |
| 66 | this.deviceId = deviceId; |
| 67 | this.priority = priority; |
| 68 | this.selector = selector; |
| 69 | this.treatment = treatment; |
| 70 | this.timeout = timeout; |
| 71 | this.permanent = permanent; |
| 72 | this.created = System.currentTimeMillis(); |
| 73 | |
| 74 | this.appId = (short) (flowId >>> 48); |
| 75 | this.groupId = new DefaultGroupId((short) ((flowId >>> 32) & 0xFFFF)); |
| 76 | this.id = FlowId.valueOf(flowId); |
| 77 | this.type = tableType; |
| 78 | } |
| 79 | |
| 80 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 81 | TrafficTreatment treatment, int priority, ApplicationId appId, |
| 82 | int timeout, boolean permanent) { |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 83 | this(deviceId, selector, treatment, priority, appId, new DefaultGroupId(0), |
| 84 | timeout, permanent); |
| 85 | } |
| 86 | |
| 87 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
| 88 | TrafficTreatment treatment, int priority, ApplicationId appId, |
| 89 | int timeout, boolean permanent, Type type) { |
| 90 | |
| 91 | if (priority < FlowRule.MIN_PRIORITY) { |
| 92 | throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY); |
| 93 | } |
| 94 | |
| 95 | this.deviceId = deviceId; |
| 96 | this.priority = priority; |
| 97 | this.selector = selector; |
| 98 | this.treatment = treatment; |
| 99 | this.appId = appId.id(); |
| 100 | this.groupId = new DefaultGroupId(0); |
| 101 | this.timeout = timeout; |
| 102 | this.permanent = permanent; |
| 103 | this.created = System.currentTimeMillis(); |
| 104 | this.type = type; |
| 105 | |
| 106 | /* |
| 107 | * id consists of the following. |
| 108 | * | appId (16 bits) | groupId (16 bits) | flowId (32 bits) | |
| 109 | */ |
| 110 | this.id = FlowId.valueOf((((long) this.appId) << 48) | (((long) this.groupId.id()) << 32) |
| 111 | | (this.hash() & 0xffffffffL)); |
| 112 | |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 113 | } |
| 114 | |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 115 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
| 116 | TrafficTreatment treatment, int priority, ApplicationId appId, |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 117 | GroupId groupId, int timeout, boolean permanent) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 118 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 119 | if (priority < FlowRule.MIN_PRIORITY) { |
alshabib | a0e0498 | 2014-10-03 13:03:19 -0700 | [diff] [blame] | 120 | throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY); |
| 121 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 122 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 123 | this.deviceId = deviceId; |
| 124 | this.priority = priority; |
| 125 | this.selector = selector; |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 126 | this.treatment = treatment; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 127 | this.appId = appId.id(); |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 128 | this.groupId = groupId; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 129 | this.timeout = timeout; |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 130 | this.permanent = permanent; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 131 | this.created = System.currentTimeMillis(); |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 132 | this.type = Type.DEFAULT; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 133 | |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 134 | /* |
| 135 | * id consists of the following. |
| 136 | * | appId (16 bits) | groupId (16 bits) | flowId (32 bits) | |
| 137 | */ |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 138 | this.id = FlowId.valueOf((((long) this.appId) << 48) | (((long) this.groupId.id()) << 32) |
| 139 | | (this.hash() & 0xffffffffL)); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 140 | } |
| 141 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 142 | public DefaultFlowRule(FlowRule rule) { |
| 143 | this.deviceId = rule.deviceId(); |
| 144 | this.priority = rule.priority(); |
| 145 | this.selector = rule.selector(); |
| 146 | this.treatment = rule.treatment(); |
| 147 | this.appId = rule.appId(); |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 148 | this.groupId = rule.groupId(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 149 | this.id = rule.id(); |
| 150 | this.timeout = rule.timeout(); |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 151 | this.permanent = rule.isPermanent(); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 152 | this.created = System.currentTimeMillis(); |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 153 | this.type = rule.type(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 154 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 157 | @Override |
| 158 | public FlowId id() { |
| 159 | return id; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 163 | public short appId() { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 164 | return appId; |
| 165 | } |
| 166 | |
| 167 | @Override |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 168 | public GroupId groupId() { |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 169 | return groupId; |
| 170 | } |
| 171 | |
| 172 | @Override |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 173 | public int priority() { |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 174 | return priority; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public DeviceId deviceId() { |
| 179 | return deviceId; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public TrafficSelector selector() { |
| 184 | return selector; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public TrafficTreatment treatment() { |
| 189 | return treatment; |
| 190 | } |
| 191 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 192 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 193 | /* |
| 194 | * The priority and statistics can change on a given treatment and selector |
| 195 | * |
| 196 | * (non-Javadoc) |
| 197 | * @see java.lang.Object#equals(java.lang.Object) |
| 198 | */ |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 199 | public int hashCode() { |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 200 | return Objects.hash(deviceId, selector, priority, type); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | public int hash() { |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 204 | return Objects.hash(deviceId, selector, treatment, type); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | /* |
| 209 | * The priority and statistics can change on a given treatment and selector |
| 210 | * |
| 211 | * (non-Javadoc) |
| 212 | * @see java.lang.Object#equals(java.lang.Object) |
| 213 | */ |
| 214 | public boolean equals(Object obj) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 215 | if (this == obj) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 216 | return true; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 217 | } |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 218 | if (obj instanceof DefaultFlowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 219 | DefaultFlowRule that = (DefaultFlowRule) obj; |
| 220 | return Objects.equals(deviceId, that.deviceId) && |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 221 | Objects.equals(priority, that.priority) && |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 222 | Objects.equals(selector, that.selector) && |
| 223 | Objects.equals(type, that.type); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 224 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 225 | } |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 226 | return false; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 229 | @Override |
| 230 | public String toString() { |
| 231 | return toStringHelper(this) |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 232 | .add("id", Long.toHexString(id.value())) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 233 | .add("deviceId", deviceId) |
| 234 | .add("priority", priority) |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 235 | .add("selector", selector.criteria()) |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 236 | .add("treatment", treatment == null ? "N/A" : treatment.allInstructions()) |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 237 | .add("table type", type) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 238 | .add("created", created) |
| 239 | .toString(); |
| 240 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 241 | |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 242 | @Override |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 243 | public int timeout() { |
alshabib | 58747a6 | 2014-10-07 11:05:30 -0700 | [diff] [blame] | 244 | return timeout; |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 247 | @Override |
| 248 | public boolean isPermanent() { |
| 249 | return permanent; |
| 250 | } |
| 251 | |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 252 | @Override |
| 253 | public Type type() { |
| 254 | return type; |
| 255 | } |
| 256 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 257 | } |