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