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