blob: 65c4a167168558dc430df5e2f06d54c4865d1a14 [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
alshabib97044902014-09-18 14:52:16 -070030 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabiba7f7ca82014-09-22 11:41:23 -070031 TrafficTreatment treatment, int priority, FlowRuleState state,
alshabib9290eea2014-09-22 11:58:17 -070032 long life, long packets, long bytes, long flowId) {
alshabib6b5cfec2014-09-18 17:42:18 -070033 this.deviceId = deviceId;
34 this.priority = priority;
35 this.selector = selector;
36 this.treatment = treatment;
alshabiba7f7ca82014-09-22 11:41:23 -070037 this.state = state;
alshabiba68eb962014-09-24 20:34:13 -070038 this.appId = ApplicationId.valueOf((int) (flowId >> 32));
alshabib6b5cfec2014-09-18 17:42:18 -070039 this.id = FlowId.valueOf(flowId);
40
alshabib97044902014-09-18 14:52:16 -070041 this.life = life;
alshabib97044902014-09-18 14:52:16 -070042 this.packets = packets;
43 this.bytes = bytes;
alshabib6b5cfec2014-09-18 17:42:18 -070044 this.created = System.currentTimeMillis();
alshabib97044902014-09-18 14:52:16 -070045 }
46
alshabiba7f7ca82014-09-22 11:41:23 -070047 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabiba68eb962014-09-24 20:34:13 -070048 TrafficTreatment treatement, int priority, ApplicationId appId) {
49 this(deviceId, selector, treatement, priority, FlowRuleState.CREATED, appId);
alshabiba7f7ca82014-09-22 11:41:23 -070050 }
51
52 public DefaultFlowRule(FlowRule rule, FlowRuleState state) {
53 this(rule.deviceId(), rule.selector(), rule.treatment(),
alshabiba68eb962014-09-24 20:34:13 -070054 rule.priority(), state, rule.id(), rule.appId());
alshabib219ebaa2014-09-22 15:41:24 -070055 }
56
57 private DefaultFlowRule(DeviceId deviceId,
58 TrafficSelector selector, TrafficTreatment treatment,
alshabiba68eb962014-09-24 20:34:13 -070059 int priority, FlowRuleState state, ApplicationId appId) {
alshabib219ebaa2014-09-22 15:41:24 -070060 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;
alshabiba68eb962014-09-24 20:34:13 -070068 this.appId = appId;
69
70 this.id = FlowId.valueOf((((long) appId().id()) << 32) | (this.hash() & 0xffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -070071 this.created = System.currentTimeMillis();
72 }
73
74 private DefaultFlowRule(DeviceId deviceId,
75 TrafficSelector selector, TrafficTreatment treatment,
alshabiba68eb962014-09-24 20:34:13 -070076 int priority, FlowRuleState state, FlowId flowId, ApplicationId appId) {
alshabib219ebaa2014-09-22 15:41:24 -070077 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;
alshabiba68eb962014-09-24 20:34:13 -070085 this.appId = appId;
alshabib219ebaa2014-09-22 15:41:24 -070086 this.id = flowId;
87 this.created = System.currentTimeMillis();
alshabiba7f7ca82014-09-22 11:41:23 -070088 }
89
alshabib97044902014-09-18 14:52:16 -070090
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070091 @Override
92 public FlowId id() {
93 return id;
alshabib7b2748f2014-09-16 20:21:11 -070094 }
95
96 @Override
alshabiba68eb962014-09-24 20:34:13 -070097 public ApplicationId appId() {
98 return appId;
99 }
100
101 @Override
alshabib7b2748f2014-09-16 20:21:11 -0700102 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700103 return priority;
alshabib7b2748f2014-09-16 20:21:11 -0700104 }
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 Koshibe08eabaa2014-09-17 14:59:25 -0700121 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700122 public long lifeMillis() {
alshabib97044902014-09-18 14:52:16 -0700123 return life;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700124 }
125
126 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700127 public long packets() {
alshabib97044902014-09-18 14:52:16 -0700128 return packets;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700129 }
130
131 @Override
132 public long bytes() {
alshabib97044902014-09-18 14:52:16 -0700133 return bytes;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700134 }
135
136 @Override
alshabiba7f7ca82014-09-22 11:41:23 -0700137 public FlowRuleState state() {
138 return this.state;
139 }
140
141
142 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700143 /*
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 Koshibe08eabaa2014-09-17 14:59:25 -0700149 public int hashCode() {
alshabiba68eb962014-09-24 20:34:13 -0700150 return Objects.hash(deviceId, id);
151 }
152
153 public int hash() {
154 return Objects.hash(deviceId, selector, id);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700155 }
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) {
alshabiba7f7ca82014-09-22 11:41:23 -0700165 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700166 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700167 }
alshabib54ce5892014-09-23 17:50:51 -0700168 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700169 DefaultFlowRule that = (DefaultFlowRule) obj;
170 return Objects.equals(deviceId, that.deviceId) &&
171 Objects.equals(id, that.id);
alshabiba7f7ca82014-09-22 11:41:23 -0700172 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700173 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700174 }
175
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700176 @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)
alshabibbb8b1282014-09-22 17:00:18 -0700185 .add("state", state)
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700186 .toString();
187 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700188
alshabib7b2748f2014-09-16 20:21:11 -0700189}