blob: f705a945f0139ec261b1df7c226dab46fef4e30c [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
alshabib6eb438a2014-10-01 16:39:37 -070030 private boolean expired;
31
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,
alshabib6eb438a2014-10-01 16:39:37 -070034 long life, long packets, long bytes, long flowId, boolean expired) {
alshabib6b5cfec2014-09-18 17:42:18 -070035 this.deviceId = deviceId;
36 this.priority = priority;
37 this.selector = selector;
38 this.treatment = treatment;
alshabiba7f7ca82014-09-22 11:41:23 -070039 this.state = state;
alshabiba68eb962014-09-24 20:34:13 -070040 this.appId = ApplicationId.valueOf((int) (flowId >> 32));
alshabib6b5cfec2014-09-18 17:42:18 -070041 this.id = FlowId.valueOf(flowId);
alshabib6eb438a2014-10-01 16:39:37 -070042 this.expired = expired;
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();
alshabib97044902014-09-18 14:52:16 -070047 }
48
alshabiba7f7ca82014-09-22 11:41:23 -070049 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabiba68eb962014-09-24 20:34:13 -070050 TrafficTreatment treatement, int priority, ApplicationId appId) {
51 this(deviceId, selector, treatement, priority, FlowRuleState.CREATED, appId);
alshabiba7f7ca82014-09-22 11:41:23 -070052 }
53
54 public DefaultFlowRule(FlowRule rule, FlowRuleState state) {
55 this(rule.deviceId(), rule.selector(), rule.treatment(),
alshabiba68eb962014-09-24 20:34:13 -070056 rule.priority(), state, rule.id(), rule.appId());
alshabib219ebaa2014-09-22 15:41:24 -070057 }
58
59 private DefaultFlowRule(DeviceId deviceId,
60 TrafficSelector selector, TrafficTreatment treatment,
alshabiba68eb962014-09-24 20:34:13 -070061 int priority, FlowRuleState state, ApplicationId appId) {
alshabib219ebaa2014-09-22 15:41:24 -070062 this.deviceId = deviceId;
63 this.priority = priority;
64 this.selector = selector;
65 this.treatment = treatment;
66 this.state = state;
67 this.life = 0;
68 this.packets = 0;
69 this.bytes = 0;
alshabiba68eb962014-09-24 20:34:13 -070070 this.appId = appId;
71
72 this.id = FlowId.valueOf((((long) appId().id()) << 32) | (this.hash() & 0xffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -070073 this.created = System.currentTimeMillis();
74 }
75
76 private DefaultFlowRule(DeviceId deviceId,
77 TrafficSelector selector, TrafficTreatment treatment,
alshabiba68eb962014-09-24 20:34:13 -070078 int priority, FlowRuleState state, FlowId flowId, ApplicationId appId) {
alshabib219ebaa2014-09-22 15:41:24 -070079 this.deviceId = deviceId;
80 this.priority = priority;
81 this.selector = selector;
82 this.treatment = treatment;
83 this.state = state;
84 this.life = 0;
85 this.packets = 0;
86 this.bytes = 0;
alshabiba68eb962014-09-24 20:34:13 -070087 this.appId = appId;
alshabib219ebaa2014-09-22 15:41:24 -070088 this.id = flowId;
89 this.created = System.currentTimeMillis();
alshabiba7f7ca82014-09-22 11:41:23 -070090 }
91
alshabib97044902014-09-18 14:52:16 -070092
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070093 @Override
94 public FlowId id() {
95 return id;
alshabib7b2748f2014-09-16 20:21:11 -070096 }
97
98 @Override
alshabiba68eb962014-09-24 20:34:13 -070099 public ApplicationId appId() {
100 return appId;
101 }
102
103 @Override
alshabib7b2748f2014-09-16 20:21:11 -0700104 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700105 return priority;
alshabib7b2748f2014-09-16 20:21:11 -0700106 }
107
108 @Override
109 public DeviceId deviceId() {
110 return deviceId;
111 }
112
113 @Override
114 public TrafficSelector selector() {
115 return selector;
116 }
117
118 @Override
119 public TrafficTreatment treatment() {
120 return treatment;
121 }
122
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700123 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700124 public long lifeMillis() {
alshabib97044902014-09-18 14:52:16 -0700125 return life;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700126 }
127
128 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700129 public long packets() {
alshabib97044902014-09-18 14:52:16 -0700130 return packets;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700131 }
132
133 @Override
134 public long bytes() {
alshabib97044902014-09-18 14:52:16 -0700135 return bytes;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700136 }
137
138 @Override
alshabiba7f7ca82014-09-22 11:41:23 -0700139 public FlowRuleState state() {
140 return this.state;
141 }
142
143
144 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700145 /*
146 * The priority and statistics can change on a given treatment and selector
147 *
148 * (non-Javadoc)
149 * @see java.lang.Object#equals(java.lang.Object)
150 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700151 public int hashCode() {
alshabiba68eb962014-09-24 20:34:13 -0700152 return Objects.hash(deviceId, id);
153 }
154
155 public int hash() {
156 return Objects.hash(deviceId, selector, id);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700157 }
158
159 @Override
160 /*
161 * The priority and statistics can change on a given treatment and selector
162 *
163 * (non-Javadoc)
164 * @see java.lang.Object#equals(java.lang.Object)
165 */
166 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700167 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700168 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700169 }
alshabib54ce5892014-09-23 17:50:51 -0700170 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700171 DefaultFlowRule that = (DefaultFlowRule) obj;
172 return Objects.equals(deviceId, that.deviceId) &&
173 Objects.equals(id, that.id);
alshabiba7f7ca82014-09-22 11:41:23 -0700174 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700175 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700176 }
177
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700178 @Override
179 public String toString() {
180 return toStringHelper(this)
181 .add("id", id)
182 .add("deviceId", deviceId)
183 .add("priority", priority)
184 .add("selector", selector)
185 .add("treatment", treatment)
186 .add("created", created)
alshabibbb8b1282014-09-22 17:00:18 -0700187 .add("state", state)
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700188 .toString();
189 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700190
alshabib6eb438a2014-10-01 16:39:37 -0700191 @Override
192 public boolean expired() {
193 return expired;
194 }
195
alshabib7b2748f2014-09-16 20:21:11 -0700196}