blob: 9b44fc2aada4603d6024073f88a66623147c3b88 [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
alshabiba0e04982014-10-03 13:03:19 -070032 /**
33 * Creates a flow rule given the following paremeters.
34 * @param deviceId the device where the rule should be installed
35 * @param selector the traffic selection
36 * @param treatment how the seleted traffic should be handled
37 * @param priority the rule priority cannot be less than FlowRule.MIN_PRIORITY
38 * @param state the state in which the rule is
39 * @param life how long it has existed for (ms)
40 * @param packets number of packets it has seen
41 * @param bytes number of bytes it has seen
42 * @param flowId the identifier
43 * @param timeout the rule's timeout (idle) not to exceed
44 * FlowRule.MAX_TIMEOUT of idle time
45 */
alshabib97044902014-09-18 14:52:16 -070046 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabiba7f7ca82014-09-22 11:41:23 -070047 TrafficTreatment treatment, int priority, FlowRuleState state,
alshabiba0e04982014-10-03 13:03:19 -070048 long life, long packets, long bytes, long flowId,
alshabibba5ac482014-10-02 17:15:20 -070049 int timeout) {
alshabib6b5cfec2014-09-18 17:42:18 -070050 this.deviceId = deviceId;
51 this.priority = priority;
52 this.selector = selector;
53 this.treatment = treatment;
alshabiba7f7ca82014-09-22 11:41:23 -070054 this.state = state;
alshabiba68eb962014-09-24 20:34:13 -070055 this.appId = ApplicationId.valueOf((int) (flowId >> 32));
alshabib6b5cfec2014-09-18 17:42:18 -070056 this.id = FlowId.valueOf(flowId);
alshabib97044902014-09-18 14:52:16 -070057 this.life = life;
alshabib97044902014-09-18 14:52:16 -070058 this.packets = packets;
59 this.bytes = bytes;
alshabib6b5cfec2014-09-18 17:42:18 -070060 this.created = System.currentTimeMillis();
alshabibba5ac482014-10-02 17:15:20 -070061 this.timeout = timeout;
alshabib97044902014-09-18 14:52:16 -070062 }
63
alshabiba7f7ca82014-09-22 11:41:23 -070064 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabibba5ac482014-10-02 17:15:20 -070065 TrafficTreatment treatement, int priority, ApplicationId appId,
66 int timeout) {
alshabiba0e04982014-10-03 13:03:19 -070067 this(deviceId, selector, treatement, priority,
alshabibba5ac482014-10-02 17:15:20 -070068 FlowRuleState.CREATED, appId, timeout);
alshabiba7f7ca82014-09-22 11:41:23 -070069 }
70
71 public DefaultFlowRule(FlowRule rule, FlowRuleState state) {
72 this(rule.deviceId(), rule.selector(), rule.treatment(),
alshabibba5ac482014-10-02 17:15:20 -070073 rule.priority(), state, rule.id(), rule.appId(),
74 rule.timeout());
alshabib219ebaa2014-09-22 15:41:24 -070075 }
76
77 private DefaultFlowRule(DeviceId deviceId,
78 TrafficSelector selector, TrafficTreatment treatment,
alshabibba5ac482014-10-02 17:15:20 -070079 int priority, FlowRuleState state, ApplicationId appId,
80 int timeout) {
alshabiba0e04982014-10-03 13:03:19 -070081 if (priority < MIN_PRIORITY) {
82 throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY);
83 }
alshabib219ebaa2014-09-22 15:41:24 -070084 this.deviceId = deviceId;
85 this.priority = priority;
86 this.selector = selector;
87 this.treatment = treatment;
88 this.state = state;
89 this.life = 0;
90 this.packets = 0;
91 this.bytes = 0;
alshabiba68eb962014-09-24 20:34:13 -070092 this.appId = appId;
93
alshabibba5ac482014-10-02 17:15:20 -070094 this.timeout = timeout;
95
alshabiba68eb962014-09-24 20:34:13 -070096 this.id = FlowId.valueOf((((long) appId().id()) << 32) | (this.hash() & 0xffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -070097 this.created = System.currentTimeMillis();
98 }
99
100 private DefaultFlowRule(DeviceId deviceId,
101 TrafficSelector selector, TrafficTreatment treatment,
alshabibba5ac482014-10-02 17:15:20 -0700102 int priority, FlowRuleState state, FlowId flowId, ApplicationId appId,
103 int timeout) {
alshabib219ebaa2014-09-22 15:41:24 -0700104 this.deviceId = deviceId;
105 this.priority = priority;
106 this.selector = selector;
107 this.treatment = treatment;
108 this.state = state;
109 this.life = 0;
110 this.packets = 0;
111 this.bytes = 0;
alshabiba68eb962014-09-24 20:34:13 -0700112 this.appId = appId;
alshabib219ebaa2014-09-22 15:41:24 -0700113 this.id = flowId;
alshabibba5ac482014-10-02 17:15:20 -0700114 this.timeout = timeout;
alshabib219ebaa2014-09-22 15:41:24 -0700115 this.created = System.currentTimeMillis();
alshabiba7f7ca82014-09-22 11:41:23 -0700116 }
117
alshabib97044902014-09-18 14:52:16 -0700118
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700119 @Override
120 public FlowId id() {
121 return id;
alshabib7b2748f2014-09-16 20:21:11 -0700122 }
123
124 @Override
alshabiba68eb962014-09-24 20:34:13 -0700125 public ApplicationId appId() {
126 return appId;
127 }
128
129 @Override
alshabib7b2748f2014-09-16 20:21:11 -0700130 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700131 return priority;
alshabib7b2748f2014-09-16 20:21:11 -0700132 }
133
134 @Override
135 public DeviceId deviceId() {
136 return deviceId;
137 }
138
139 @Override
140 public TrafficSelector selector() {
141 return selector;
142 }
143
144 @Override
145 public TrafficTreatment treatment() {
146 return treatment;
147 }
148
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700149 @Override
alshabiba0e04982014-10-03 13:03:19 -0700150 public long life() {
alshabib97044902014-09-18 14:52:16 -0700151 return life;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700152 }
153
154 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700155 public long packets() {
alshabib97044902014-09-18 14:52:16 -0700156 return packets;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700157 }
158
159 @Override
160 public long bytes() {
alshabib97044902014-09-18 14:52:16 -0700161 return bytes;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700162 }
163
164 @Override
alshabiba7f7ca82014-09-22 11:41:23 -0700165 public FlowRuleState state() {
166 return this.state;
167 }
168
169
170 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700171 /*
172 * The priority and statistics can change on a given treatment and selector
173 *
174 * (non-Javadoc)
175 * @see java.lang.Object#equals(java.lang.Object)
176 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700177 public int hashCode() {
alshabibba5ac482014-10-02 17:15:20 -0700178 return Objects.hash(deviceId, selector, priority);
alshabiba68eb962014-09-24 20:34:13 -0700179 }
180
181 public int hash() {
182 return Objects.hash(deviceId, selector, id);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700183 }
184
185 @Override
186 /*
187 * The priority and statistics can change on a given treatment and selector
188 *
189 * (non-Javadoc)
190 * @see java.lang.Object#equals(java.lang.Object)
191 */
192 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700193 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700194 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700195 }
alshabib54ce5892014-09-23 17:50:51 -0700196 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700197 DefaultFlowRule that = (DefaultFlowRule) obj;
198 return Objects.equals(deviceId, that.deviceId) &&
alshabibba5ac482014-10-02 17:15:20 -0700199 //Objects.equals(id, that.id) &&
200 Objects.equals(priority, that.priority) &&
201 Objects.equals(selector, that.selector);
202
alshabiba7f7ca82014-09-22 11:41:23 -0700203 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700204 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700205 }
206
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700207 @Override
208 public String toString() {
209 return toStringHelper(this)
210 .add("id", id)
211 .add("deviceId", deviceId)
212 .add("priority", priority)
alshabibba5ac482014-10-02 17:15:20 -0700213 .add("selector", selector.criteria())
214 .add("treatment", treatment == null ? "N/A" : treatment.instructions())
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700215 .add("created", created)
alshabibbb8b1282014-09-22 17:00:18 -0700216 .add("state", state)
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700217 .toString();
218 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700219
alshabib6eb438a2014-10-01 16:39:37 -0700220 @Override
alshabibba5ac482014-10-02 17:15:20 -0700221 public int timeout() {
222 return timeout > MAX_TIMEOUT ? MAX_TIMEOUT : this.timeout;
alshabib6eb438a2014-10-01 16:39:37 -0700223 }
224
alshabib7b2748f2014-09-16 20:21:11 -0700225}