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