blob: bb4805bc243de1d884e95b66675812d2dcc73b74 [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) {
53 this(deviceId, selector, treatement, priority,
54 FlowRuleState.CREATED, appId, timeout);
alshabiba7f7ca82014-09-22 11:41:23 -070055 }
56
57 public DefaultFlowRule(FlowRule rule, FlowRuleState state) {
58 this(rule.deviceId(), rule.selector(), rule.treatment(),
alshabibba5ac482014-10-02 17:15:20 -070059 rule.priority(), state, rule.id(), rule.appId(),
60 rule.timeout());
alshabib219ebaa2014-09-22 15:41:24 -070061 }
62
63 private DefaultFlowRule(DeviceId deviceId,
64 TrafficSelector selector, TrafficTreatment treatment,
alshabibba5ac482014-10-02 17:15:20 -070065 int priority, FlowRuleState state, ApplicationId appId,
66 int timeout) {
alshabib219ebaa2014-09-22 15:41:24 -070067 this.deviceId = deviceId;
68 this.priority = priority;
69 this.selector = selector;
70 this.treatment = treatment;
71 this.state = state;
72 this.life = 0;
73 this.packets = 0;
74 this.bytes = 0;
alshabiba68eb962014-09-24 20:34:13 -070075 this.appId = appId;
76
alshabibba5ac482014-10-02 17:15:20 -070077 this.timeout = timeout;
78
alshabiba68eb962014-09-24 20:34:13 -070079 this.id = FlowId.valueOf((((long) appId().id()) << 32) | (this.hash() & 0xffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -070080 this.created = System.currentTimeMillis();
81 }
82
83 private DefaultFlowRule(DeviceId deviceId,
84 TrafficSelector selector, TrafficTreatment treatment,
alshabibba5ac482014-10-02 17:15:20 -070085 int priority, FlowRuleState state, FlowId flowId, ApplicationId appId,
86 int timeout) {
alshabib219ebaa2014-09-22 15:41:24 -070087 this.deviceId = deviceId;
88 this.priority = priority;
89 this.selector = selector;
90 this.treatment = treatment;
91 this.state = state;
92 this.life = 0;
93 this.packets = 0;
94 this.bytes = 0;
alshabiba68eb962014-09-24 20:34:13 -070095 this.appId = appId;
alshabib219ebaa2014-09-22 15:41:24 -070096 this.id = flowId;
alshabibba5ac482014-10-02 17:15:20 -070097 this.timeout = timeout;
alshabib219ebaa2014-09-22 15:41:24 -070098 this.created = System.currentTimeMillis();
alshabiba7f7ca82014-09-22 11:41:23 -070099 }
100
alshabib97044902014-09-18 14:52:16 -0700101
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700102 @Override
103 public FlowId id() {
104 return id;
alshabib7b2748f2014-09-16 20:21:11 -0700105 }
106
107 @Override
alshabiba68eb962014-09-24 20:34:13 -0700108 public ApplicationId appId() {
109 return appId;
110 }
111
112 @Override
alshabib7b2748f2014-09-16 20:21:11 -0700113 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700114 return priority;
alshabib7b2748f2014-09-16 20:21:11 -0700115 }
116
117 @Override
118 public DeviceId deviceId() {
119 return deviceId;
120 }
121
122 @Override
123 public TrafficSelector selector() {
124 return selector;
125 }
126
127 @Override
128 public TrafficTreatment treatment() {
129 return treatment;
130 }
131
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700132 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700133 public long lifeMillis() {
alshabib97044902014-09-18 14:52:16 -0700134 return life;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700135 }
136
137 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700138 public long packets() {
alshabib97044902014-09-18 14:52:16 -0700139 return packets;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700140 }
141
142 @Override
143 public long bytes() {
alshabib97044902014-09-18 14:52:16 -0700144 return bytes;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700145 }
146
147 @Override
alshabiba7f7ca82014-09-22 11:41:23 -0700148 public FlowRuleState state() {
149 return this.state;
150 }
151
152
153 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700154 /*
155 * The priority and statistics can change on a given treatment and selector
156 *
157 * (non-Javadoc)
158 * @see java.lang.Object#equals(java.lang.Object)
159 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700160 public int hashCode() {
alshabibba5ac482014-10-02 17:15:20 -0700161 return Objects.hash(deviceId, selector, priority);
alshabiba68eb962014-09-24 20:34:13 -0700162 }
163
164 public int hash() {
165 return Objects.hash(deviceId, selector, id);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700166 }
167
168 @Override
169 /*
170 * The priority and statistics can change on a given treatment and selector
171 *
172 * (non-Javadoc)
173 * @see java.lang.Object#equals(java.lang.Object)
174 */
175 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700176 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700177 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700178 }
alshabib54ce5892014-09-23 17:50:51 -0700179 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700180 DefaultFlowRule that = (DefaultFlowRule) obj;
181 return Objects.equals(deviceId, that.deviceId) &&
alshabibba5ac482014-10-02 17:15:20 -0700182 //Objects.equals(id, that.id) &&
183 Objects.equals(priority, that.priority) &&
184 Objects.equals(selector, that.selector);
185
alshabiba7f7ca82014-09-22 11:41:23 -0700186 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700187 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700188 }
189
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700190 @Override
191 public String toString() {
192 return toStringHelper(this)
193 .add("id", id)
194 .add("deviceId", deviceId)
195 .add("priority", priority)
alshabibba5ac482014-10-02 17:15:20 -0700196 .add("selector", selector.criteria())
197 .add("treatment", treatment == null ? "N/A" : treatment.instructions())
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700198 .add("created", created)
alshabibbb8b1282014-09-22 17:00:18 -0700199 .add("state", state)
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700200 .toString();
201 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700202
alshabib6eb438a2014-10-01 16:39:37 -0700203 @Override
alshabibba5ac482014-10-02 17:15:20 -0700204 public int timeout() {
205 return timeout > MAX_TIMEOUT ? MAX_TIMEOUT : this.timeout;
alshabib6eb438a2014-10-01 16:39:37 -0700206 }
207
alshabib7b2748f2014-09-16 20:21:11 -0700208}