alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.flow; |
| 2 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 3 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 4 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 5 | |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 6 | import java.util.Objects; |
| 7 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 8 | import org.onlab.onos.ApplicationId; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.DeviceId; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 10 | import org.slf4j.Logger; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 11 | |
| 12 | public class DefaultFlowRule implements FlowRule { |
| 13 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 14 | private final Logger log = getLogger(getClass()); |
| 15 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 16 | private final DeviceId deviceId; |
| 17 | private final int priority; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 18 | private final TrafficSelector selector; |
| 19 | private final TrafficTreatment treatment; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 20 | private final long created; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 21 | private final long life; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 22 | private final long packets; |
| 23 | private final long bytes; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 24 | private final FlowRuleState state; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 25 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 26 | private final FlowId id; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 27 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 28 | private final ApplicationId appId; |
| 29 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 30 | private final int timeout; |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 31 | |
alshabib | a0e0498 | 2014-10-03 13:03:19 -0700 | [diff] [blame^] | 32 | /** |
| 33 | * Creates a flow rule given the following paremeters. |
| 34 | * @param deviceId the device where the rule should be installed |
| 35 | * @param selector the traffic selection |
| 36 | * @param treatment how the seleted traffic should be handled |
| 37 | * @param priority the rule priority cannot be less than FlowRule.MIN_PRIORITY |
| 38 | * @param state the state in which the rule is |
| 39 | * @param life how long it has existed for (ms) |
| 40 | * @param packets number of packets it has seen |
| 41 | * @param bytes number of bytes it has seen |
| 42 | * @param flowId the identifier |
| 43 | * @param timeout the rule's timeout (idle) not to exceed |
| 44 | * FlowRule.MAX_TIMEOUT of idle time |
| 45 | */ |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 46 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 47 | TrafficTreatment treatment, int priority, FlowRuleState state, |
alshabib | a0e0498 | 2014-10-03 13:03:19 -0700 | [diff] [blame^] | 48 | long life, long packets, long bytes, long flowId, |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 49 | int timeout) { |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 50 | this.deviceId = deviceId; |
| 51 | this.priority = priority; |
| 52 | this.selector = selector; |
| 53 | this.treatment = treatment; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 54 | this.state = state; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 55 | this.appId = ApplicationId.valueOf((int) (flowId >> 32)); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 56 | this.id = FlowId.valueOf(flowId); |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 57 | this.life = life; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 58 | this.packets = packets; |
| 59 | this.bytes = bytes; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 60 | this.created = System.currentTimeMillis(); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 61 | this.timeout = timeout; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 62 | } |
| 63 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 64 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 65 | TrafficTreatment treatement, int priority, ApplicationId appId, |
| 66 | int timeout) { |
alshabib | a0e0498 | 2014-10-03 13:03:19 -0700 | [diff] [blame^] | 67 | this(deviceId, selector, treatement, priority, |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 68 | FlowRuleState.CREATED, appId, timeout); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | public DefaultFlowRule(FlowRule rule, FlowRuleState state) { |
| 72 | this(rule.deviceId(), rule.selector(), rule.treatment(), |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 73 | rule.priority(), state, rule.id(), rule.appId(), |
| 74 | rule.timeout()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | private DefaultFlowRule(DeviceId deviceId, |
| 78 | TrafficSelector selector, TrafficTreatment treatment, |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 79 | int priority, FlowRuleState state, ApplicationId appId, |
| 80 | int timeout) { |
alshabib | a0e0498 | 2014-10-03 13:03:19 -0700 | [diff] [blame^] | 81 | if (priority < MIN_PRIORITY) { |
| 82 | throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY); |
| 83 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 84 | this.deviceId = deviceId; |
| 85 | this.priority = priority; |
| 86 | this.selector = selector; |
| 87 | this.treatment = treatment; |
| 88 | this.state = state; |
| 89 | this.life = 0; |
| 90 | this.packets = 0; |
| 91 | this.bytes = 0; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 92 | this.appId = appId; |
| 93 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 94 | this.timeout = timeout; |
| 95 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 96 | this.id = FlowId.valueOf((((long) appId().id()) << 32) | (this.hash() & 0xffffffffL)); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 97 | this.created = System.currentTimeMillis(); |
| 98 | } |
| 99 | |
| 100 | private DefaultFlowRule(DeviceId deviceId, |
| 101 | TrafficSelector selector, TrafficTreatment treatment, |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 102 | int priority, FlowRuleState state, FlowId flowId, ApplicationId appId, |
| 103 | int timeout) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 104 | this.deviceId = deviceId; |
| 105 | this.priority = priority; |
| 106 | this.selector = selector; |
| 107 | this.treatment = treatment; |
| 108 | this.state = state; |
| 109 | this.life = 0; |
| 110 | this.packets = 0; |
| 111 | this.bytes = 0; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 112 | this.appId = appId; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 113 | this.id = flowId; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 114 | this.timeout = timeout; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 115 | this.created = System.currentTimeMillis(); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 116 | } |
| 117 | |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 118 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 119 | @Override |
| 120 | public FlowId id() { |
| 121 | return id; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 125 | public ApplicationId appId() { |
| 126 | return appId; |
| 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 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 149 | @Override |
alshabib | a0e0498 | 2014-10-03 13:03:19 -0700 | [diff] [blame^] | 150 | public long life() { |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 151 | return life; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 155 | public long packets() { |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 156 | return packets; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public long bytes() { |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 161 | return bytes; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 165 | public FlowRuleState state() { |
| 166 | return this.state; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 171 | /* |
| 172 | * The priority and statistics can change on a given treatment and selector |
| 173 | * |
| 174 | * (non-Javadoc) |
| 175 | * @see java.lang.Object#equals(java.lang.Object) |
| 176 | */ |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 177 | public int hashCode() { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 178 | return Objects.hash(deviceId, selector, priority); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | public int hash() { |
| 182 | return Objects.hash(deviceId, selector, id); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | /* |
| 187 | * The priority and statistics can change on a given treatment and selector |
| 188 | * |
| 189 | * (non-Javadoc) |
| 190 | * @see java.lang.Object#equals(java.lang.Object) |
| 191 | */ |
| 192 | public boolean equals(Object obj) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 193 | if (this == obj) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 194 | return true; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 195 | } |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 196 | if (obj instanceof DefaultFlowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 197 | DefaultFlowRule that = (DefaultFlowRule) obj; |
| 198 | return Objects.equals(deviceId, that.deviceId) && |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 199 | //Objects.equals(id, that.id) && |
| 200 | Objects.equals(priority, that.priority) && |
| 201 | Objects.equals(selector, that.selector); |
| 202 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 203 | } |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 204 | return false; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 207 | @Override |
| 208 | public String toString() { |
| 209 | return toStringHelper(this) |
| 210 | .add("id", id) |
| 211 | .add("deviceId", deviceId) |
| 212 | .add("priority", priority) |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 213 | .add("selector", selector.criteria()) |
| 214 | .add("treatment", treatment == null ? "N/A" : treatment.instructions()) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 215 | .add("created", created) |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 216 | .add("state", state) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 217 | .toString(); |
| 218 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 219 | |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 220 | @Override |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 221 | public int timeout() { |
| 222 | return timeout > MAX_TIMEOUT ? MAX_TIMEOUT : this.timeout; |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 223 | } |
| 224 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 225 | } |