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 | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 32 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 33 | TrafficTreatment treatment, int priority, FlowRuleState state, |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 34 | long life, long packets, long bytes, long flowId, boolean expired, |
| 35 | int timeout) { |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 36 | this.deviceId = deviceId; |
| 37 | this.priority = priority; |
| 38 | this.selector = selector; |
| 39 | this.treatment = treatment; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 40 | this.state = state; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 41 | this.appId = ApplicationId.valueOf((int) (flowId >> 32)); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 42 | this.id = FlowId.valueOf(flowId); |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 43 | this.life = life; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 44 | this.packets = packets; |
| 45 | this.bytes = bytes; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 46 | this.created = System.currentTimeMillis(); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 47 | this.timeout = timeout; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 48 | } |
| 49 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 50 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 51 | TrafficTreatment treatement, int priority, ApplicationId appId, |
| 52 | int timeout) { |
| 53 | this(deviceId, selector, treatement, priority, |
| 54 | FlowRuleState.CREATED, appId, timeout); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | public DefaultFlowRule(FlowRule rule, FlowRuleState state) { |
| 58 | this(rule.deviceId(), rule.selector(), rule.treatment(), |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 59 | rule.priority(), state, rule.id(), rule.appId(), |
| 60 | rule.timeout()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private DefaultFlowRule(DeviceId deviceId, |
| 64 | TrafficSelector selector, TrafficTreatment treatment, |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 65 | int priority, FlowRuleState state, ApplicationId appId, |
| 66 | int timeout) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 67 | this.deviceId = deviceId; |
| 68 | this.priority = priority; |
| 69 | this.selector = selector; |
| 70 | this.treatment = treatment; |
| 71 | this.state = state; |
| 72 | this.life = 0; |
| 73 | this.packets = 0; |
| 74 | this.bytes = 0; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 75 | this.appId = appId; |
| 76 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 77 | this.timeout = timeout; |
| 78 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 79 | this.id = FlowId.valueOf((((long) appId().id()) << 32) | (this.hash() & 0xffffffffL)); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 80 | this.created = System.currentTimeMillis(); |
| 81 | } |
| 82 | |
| 83 | private DefaultFlowRule(DeviceId deviceId, |
| 84 | TrafficSelector selector, TrafficTreatment treatment, |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 85 | int priority, FlowRuleState state, FlowId flowId, ApplicationId appId, |
| 86 | int timeout) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 87 | this.deviceId = deviceId; |
| 88 | this.priority = priority; |
| 89 | this.selector = selector; |
| 90 | this.treatment = treatment; |
| 91 | this.state = state; |
| 92 | this.life = 0; |
| 93 | this.packets = 0; |
| 94 | this.bytes = 0; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 95 | this.appId = appId; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 96 | this.id = flowId; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 97 | this.timeout = timeout; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 98 | this.created = System.currentTimeMillis(); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 99 | } |
| 100 | |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 101 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 102 | @Override |
| 103 | public FlowId id() { |
| 104 | return id; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 108 | public ApplicationId appId() { |
| 109 | return appId; |
| 110 | } |
| 111 | |
| 112 | @Override |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 113 | public int priority() { |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 114 | return priority; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public DeviceId deviceId() { |
| 119 | return deviceId; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public TrafficSelector selector() { |
| 124 | return selector; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public TrafficTreatment treatment() { |
| 129 | return treatment; |
| 130 | } |
| 131 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 132 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 133 | public long lifeMillis() { |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 134 | return life; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 138 | public long packets() { |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 139 | return packets; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public long bytes() { |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 144 | return bytes; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 148 | public FlowRuleState state() { |
| 149 | return this.state; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 154 | /* |
| 155 | * The priority and statistics can change on a given treatment and selector |
| 156 | * |
| 157 | * (non-Javadoc) |
| 158 | * @see java.lang.Object#equals(java.lang.Object) |
| 159 | */ |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 160 | public int hashCode() { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 161 | return Objects.hash(deviceId, selector, priority); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | public int hash() { |
| 165 | return Objects.hash(deviceId, selector, id); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
| 169 | /* |
| 170 | * The priority and statistics can change on a given treatment and selector |
| 171 | * |
| 172 | * (non-Javadoc) |
| 173 | * @see java.lang.Object#equals(java.lang.Object) |
| 174 | */ |
| 175 | public boolean equals(Object obj) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 176 | if (this == obj) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 177 | return true; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 178 | } |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 179 | if (obj instanceof DefaultFlowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 180 | DefaultFlowRule that = (DefaultFlowRule) obj; |
| 181 | return Objects.equals(deviceId, that.deviceId) && |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 182 | //Objects.equals(id, that.id) && |
| 183 | Objects.equals(priority, that.priority) && |
| 184 | Objects.equals(selector, that.selector); |
| 185 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 186 | } |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 187 | return false; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 190 | @Override |
| 191 | public String toString() { |
| 192 | return toStringHelper(this) |
| 193 | .add("id", id) |
| 194 | .add("deviceId", deviceId) |
| 195 | .add("priority", priority) |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 196 | .add("selector", selector.criteria()) |
| 197 | .add("treatment", treatment == null ? "N/A" : treatment.instructions()) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 198 | .add("created", created) |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 199 | .add("state", state) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 200 | .toString(); |
| 201 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 202 | |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 203 | @Override |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 204 | public int timeout() { |
| 205 | return timeout > MAX_TIMEOUT ? MAX_TIMEOUT : this.timeout; |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 206 | } |
| 207 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 208 | } |