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; |
| 4 | |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 5 | import java.util.Objects; |
| 6 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 7 | import org.onlab.onos.net.DeviceId; |
| 8 | |
| 9 | public class DefaultFlowRule implements FlowRule { |
| 10 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 11 | private final DeviceId deviceId; |
| 12 | private final int priority; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 13 | private final TrafficSelector selector; |
| 14 | private final TrafficTreatment treatment; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 15 | private final FlowId id; |
| 16 | private final long created; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 17 | private final long life; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 18 | private final long packets; |
| 19 | private final long bytes; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame^] | 20 | private final FlowRuleState state; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 21 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 22 | |
| 23 | public DefaultFlowRule(DeviceId deviceId, |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame^] | 24 | TrafficSelector selector, TrafficTreatment treatment, |
| 25 | int priority, FlowRuleState state) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 26 | this.deviceId = deviceId; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 27 | this.priority = priority; |
| 28 | this.selector = selector; |
| 29 | this.treatment = treatment; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame^] | 30 | this.state = state; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 31 | this.life = 0; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 32 | this.packets = 0; |
| 33 | this.bytes = 0; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 34 | this.id = FlowId.valueOf(this.hashCode()); |
| 35 | this.created = System.currentTimeMillis(); |
| 36 | } |
| 37 | |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 38 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame^] | 39 | TrafficTreatment treatment, int priority, FlowRuleState state, |
| 40 | long life, long packets, long bytes, Integer flowId) { |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 41 | this.deviceId = deviceId; |
| 42 | this.priority = priority; |
| 43 | this.selector = selector; |
| 44 | this.treatment = treatment; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame^] | 45 | this.state = state; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 46 | |
| 47 | this.id = FlowId.valueOf(flowId); |
| 48 | |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 49 | this.life = life; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 50 | this.packets = packets; |
| 51 | this.bytes = bytes; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 52 | this.created = System.currentTimeMillis(); |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 53 | } |
| 54 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame^] | 55 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
| 56 | TrafficTreatment treatement, int priority) { |
| 57 | this(deviceId, selector, treatement, priority, FlowRuleState.CREATED); |
| 58 | } |
| 59 | |
| 60 | public DefaultFlowRule(FlowRule rule, FlowRuleState state) { |
| 61 | this(rule.deviceId(), rule.selector(), rule.treatment(), |
| 62 | rule.priority(), state); |
| 63 | } |
| 64 | |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 65 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 66 | @Override |
| 67 | public FlowId id() { |
| 68 | return id; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public int priority() { |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 73 | return priority; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public DeviceId deviceId() { |
| 78 | return deviceId; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public TrafficSelector selector() { |
| 83 | return selector; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public TrafficTreatment treatment() { |
| 88 | return treatment; |
| 89 | } |
| 90 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 91 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 92 | public long lifeMillis() { |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 93 | return life; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 97 | public long packets() { |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 98 | return packets; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public long bytes() { |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 103 | return bytes; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame^] | 107 | public FlowRuleState state() { |
| 108 | return this.state; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 113 | /* |
| 114 | * The priority and statistics can change on a given treatment and selector |
| 115 | * |
| 116 | * (non-Javadoc) |
| 117 | * @see java.lang.Object#equals(java.lang.Object) |
| 118 | */ |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 119 | public int hashCode() { |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 120 | return Objects.hash(deviceId, selector, treatment); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | /* |
| 125 | * The priority and statistics can change on a given treatment and selector |
| 126 | * |
| 127 | * (non-Javadoc) |
| 128 | * @see java.lang.Object#equals(java.lang.Object) |
| 129 | */ |
| 130 | public boolean equals(Object obj) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame^] | 131 | if (this == obj) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 132 | return true; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 133 | } |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame^] | 134 | if (obj instanceof FlowRule) { |
| 135 | FlowRule that = (FlowRule) obj; |
| 136 | return Objects.equals(deviceId, that.deviceId()) && |
| 137 | Objects.equals(id, that.id()); |
| 138 | } |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 139 | return false; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 142 | @Override |
| 143 | public String toString() { |
| 144 | return toStringHelper(this) |
| 145 | .add("id", id) |
| 146 | .add("deviceId", deviceId) |
| 147 | .add("priority", priority) |
| 148 | .add("selector", selector) |
| 149 | .add("treatment", treatment) |
| 150 | .add("created", created) |
| 151 | .toString(); |
| 152 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 153 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 154 | } |