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