blob: d489870046e1adeacbfac7e3e2924b04dd2a30b7 [file] [log] [blame]
alshabib7b2748f2014-09-16 20:21:11 -07001package org.onlab.onos.net.flow;
2
Ayaka Koshibed4e53e12014-09-18 14:24:55 -07003import static com.google.common.base.MoreObjects.toStringHelper;
alshabib219ebaa2014-09-22 15:41:24 -07004import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -07005
alshabib6b5cfec2014-09-18 17:42:18 -07006import java.util.Objects;
7
alshabiba68eb962014-09-24 20:34:13 -07008import org.onlab.onos.ApplicationId;
alshabib7b2748f2014-09-16 20:21:11 -07009import org.onlab.onos.net.DeviceId;
alshabib219ebaa2014-09-22 15:41:24 -070010import org.slf4j.Logger;
alshabib7b2748f2014-09-16 20:21:11 -070011
12public class DefaultFlowRule implements FlowRule {
13
alshabib219ebaa2014-09-22 15:41:24 -070014 private final Logger log = getLogger(getClass());
15
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070016 private final DeviceId deviceId;
17 private final int priority;
alshabib7b2748f2014-09-16 20:21:11 -070018 private final TrafficSelector selector;
19 private final TrafficTreatment treatment;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070020 private final long created;
alshabib6b5cfec2014-09-18 17:42:18 -070021 private final long life;
alshabib6b5cfec2014-09-18 17:42:18 -070022 private final long packets;
23 private final long bytes;
alshabiba7f7ca82014-09-22 11:41:23 -070024 private final FlowRuleState state;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070025
alshabib219ebaa2014-09-22 15:41:24 -070026 private final FlowId id;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070027
alshabiba68eb962014-09-24 20:34:13 -070028 private final ApplicationId appId;
29
alshabibba5ac482014-10-02 17:15:20 -070030 private final int timeout;
alshabib6eb438a2014-10-01 16:39:37 -070031
alshabib97044902014-09-18 14:52:16 -070032 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabiba7f7ca82014-09-22 11:41:23 -070033 TrafficTreatment treatment, int priority, FlowRuleState state,
alshabibba5ac482014-10-02 17:15:20 -070034 long life, long packets, long bytes, long flowId, boolean expired,
35 int timeout) {
alshabib6b5cfec2014-09-18 17:42:18 -070036 this.deviceId = deviceId;
37 this.priority = priority;
38 this.selector = selector;
39 this.treatment = treatment;
alshabiba7f7ca82014-09-22 11:41:23 -070040 this.state = state;
alshabiba68eb962014-09-24 20:34:13 -070041 this.appId = ApplicationId.valueOf((int) (flowId >> 32));
alshabib6b5cfec2014-09-18 17:42:18 -070042 this.id = FlowId.valueOf(flowId);
alshabib97044902014-09-18 14:52:16 -070043 this.life = life;
alshabib97044902014-09-18 14:52:16 -070044 this.packets = packets;
45 this.bytes = bytes;
alshabib6b5cfec2014-09-18 17:42:18 -070046 this.created = System.currentTimeMillis();
alshabibba5ac482014-10-02 17:15:20 -070047 this.timeout = timeout;
alshabib97044902014-09-18 14:52:16 -070048 }
49
alshabiba7f7ca82014-09-22 11:41:23 -070050 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabibba5ac482014-10-02 17:15:20 -070051 TrafficTreatment treatement, int priority, ApplicationId appId,
52 int timeout) {
alshabib5fe8f4c2014-10-03 11:55:55 -070053
54 this(deviceId, selector, treatement, priority == 0 ? 1 : priority,
alshabibba5ac482014-10-02 17:15:20 -070055 FlowRuleState.CREATED, appId, timeout);
alshabiba7f7ca82014-09-22 11:41:23 -070056 }
57
58 public DefaultFlowRule(FlowRule rule, FlowRuleState state) {
59 this(rule.deviceId(), rule.selector(), rule.treatment(),
alshabibba5ac482014-10-02 17:15:20 -070060 rule.priority(), state, rule.id(), rule.appId(),
61 rule.timeout());
alshabib219ebaa2014-09-22 15:41:24 -070062 }
63
64 private DefaultFlowRule(DeviceId deviceId,
65 TrafficSelector selector, TrafficTreatment treatment,
alshabibba5ac482014-10-02 17:15:20 -070066 int priority, FlowRuleState state, ApplicationId appId,
67 int timeout) {
alshabib219ebaa2014-09-22 15:41:24 -070068 this.deviceId = deviceId;
69 this.priority = priority;
70 this.selector = selector;
71 this.treatment = treatment;
72 this.state = state;
73 this.life = 0;
74 this.packets = 0;
75 this.bytes = 0;
alshabiba68eb962014-09-24 20:34:13 -070076 this.appId = appId;
77
alshabibba5ac482014-10-02 17:15:20 -070078 this.timeout = timeout;
79
alshabiba68eb962014-09-24 20:34:13 -070080 this.id = FlowId.valueOf((((long) appId().id()) << 32) | (this.hash() & 0xffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -070081 this.created = System.currentTimeMillis();
82 }
83
84 private DefaultFlowRule(DeviceId deviceId,
85 TrafficSelector selector, TrafficTreatment treatment,
alshabibba5ac482014-10-02 17:15:20 -070086 int priority, FlowRuleState state, FlowId flowId, ApplicationId appId,
87 int timeout) {
alshabib219ebaa2014-09-22 15:41:24 -070088 this.deviceId = deviceId;
89 this.priority = priority;
90 this.selector = selector;
91 this.treatment = treatment;
92 this.state = state;
93 this.life = 0;
94 this.packets = 0;
95 this.bytes = 0;
alshabiba68eb962014-09-24 20:34:13 -070096 this.appId = appId;
alshabib219ebaa2014-09-22 15:41:24 -070097 this.id = flowId;
alshabibba5ac482014-10-02 17:15:20 -070098 this.timeout = timeout;
alshabib219ebaa2014-09-22 15:41:24 -070099 this.created = System.currentTimeMillis();
alshabiba7f7ca82014-09-22 11:41:23 -0700100 }
101
alshabib97044902014-09-18 14:52:16 -0700102
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700103 @Override
104 public FlowId id() {
105 return id;
alshabib7b2748f2014-09-16 20:21:11 -0700106 }
107
108 @Override
alshabiba68eb962014-09-24 20:34:13 -0700109 public ApplicationId appId() {
110 return appId;
111 }
112
113 @Override
alshabib7b2748f2014-09-16 20:21:11 -0700114 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700115 return priority;
alshabib7b2748f2014-09-16 20:21:11 -0700116 }
117
118 @Override
119 public DeviceId deviceId() {
120 return deviceId;
121 }
122
123 @Override
124 public TrafficSelector selector() {
125 return selector;
126 }
127
128 @Override
129 public TrafficTreatment treatment() {
130 return treatment;
131 }
132
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700133 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700134 public long lifeMillis() {
alshabib97044902014-09-18 14:52:16 -0700135 return life;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700136 }
137
138 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700139 public long packets() {
alshabib97044902014-09-18 14:52:16 -0700140 return packets;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700141 }
142
143 @Override
144 public long bytes() {
alshabib97044902014-09-18 14:52:16 -0700145 return bytes;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700146 }
147
148 @Override
alshabiba7f7ca82014-09-22 11:41:23 -0700149 public FlowRuleState state() {
150 return this.state;
151 }
152
153
154 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700155 /*
156 * The priority and statistics can change on a given treatment and selector
157 *
158 * (non-Javadoc)
159 * @see java.lang.Object#equals(java.lang.Object)
160 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700161 public int hashCode() {
alshabibba5ac482014-10-02 17:15:20 -0700162 return Objects.hash(deviceId, selector, priority);
alshabiba68eb962014-09-24 20:34:13 -0700163 }
164
165 public int hash() {
166 return Objects.hash(deviceId, selector, id);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700167 }
168
169 @Override
170 /*
171 * The priority and statistics can change on a given treatment and selector
172 *
173 * (non-Javadoc)
174 * @see java.lang.Object#equals(java.lang.Object)
175 */
176 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700177 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700178 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700179 }
alshabib54ce5892014-09-23 17:50:51 -0700180 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700181 DefaultFlowRule that = (DefaultFlowRule) obj;
182 return Objects.equals(deviceId, that.deviceId) &&
alshabibba5ac482014-10-02 17:15:20 -0700183 //Objects.equals(id, that.id) &&
184 Objects.equals(priority, that.priority) &&
185 Objects.equals(selector, that.selector);
186
alshabiba7f7ca82014-09-22 11:41:23 -0700187 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700188 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700189 }
190
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700191 @Override
192 public String toString() {
193 return toStringHelper(this)
194 .add("id", id)
195 .add("deviceId", deviceId)
196 .add("priority", priority)
alshabibba5ac482014-10-02 17:15:20 -0700197 .add("selector", selector.criteria())
198 .add("treatment", treatment == null ? "N/A" : treatment.instructions())
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700199 .add("created", created)
alshabibbb8b1282014-09-22 17:00:18 -0700200 .add("state", state)
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700201 .toString();
202 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700203
alshabib6eb438a2014-10-01 16:39:37 -0700204 @Override
alshabibba5ac482014-10-02 17:15:20 -0700205 public int timeout() {
206 return timeout > MAX_TIMEOUT ? MAX_TIMEOUT : this.timeout;
alshabib6eb438a2014-10-01 16:39:37 -0700207 }
208
alshabib7b2748f2014-09-16 20:21:11 -0700209}