blob: 62e331f6616827f57c18e4b877d70e851d7322b5 [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;
4
alshabib6b5cfec2014-09-18 17:42:18 -07005import java.util.Objects;
6
alshabib7b2748f2014-09-16 20:21:11 -07007import org.onlab.onos.net.DeviceId;
8
9public class DefaultFlowRule implements FlowRule {
10
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070011 private final DeviceId deviceId;
12 private final int priority;
alshabib7b2748f2014-09-16 20:21:11 -070013 private final TrafficSelector selector;
14 private final TrafficTreatment treatment;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070015 private final FlowId id;
16 private final long created;
alshabib6b5cfec2014-09-18 17:42:18 -070017 private final long life;
alshabib6b5cfec2014-09-18 17:42:18 -070018 private final long packets;
19 private final long bytes;
alshabiba7f7ca82014-09-22 11:41:23 -070020 private final FlowRuleState state;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070021
alshabib7b2748f2014-09-16 20:21:11 -070022
23 public DefaultFlowRule(DeviceId deviceId,
alshabiba7f7ca82014-09-22 11:41:23 -070024 TrafficSelector selector, TrafficTreatment treatment,
25 int priority, FlowRuleState state) {
alshabib7b2748f2014-09-16 20:21:11 -070026 this.deviceId = deviceId;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070027 this.priority = priority;
28 this.selector = selector;
29 this.treatment = treatment;
alshabiba7f7ca82014-09-22 11:41:23 -070030 this.state = state;
alshabib97044902014-09-18 14:52:16 -070031 this.life = 0;
alshabib97044902014-09-18 14:52:16 -070032 this.packets = 0;
33 this.bytes = 0;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070034 this.id = FlowId.valueOf(this.hashCode());
35 this.created = System.currentTimeMillis();
36 }
37
alshabib97044902014-09-18 14:52:16 -070038 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabiba7f7ca82014-09-22 11:41:23 -070039 TrafficTreatment treatment, int priority, FlowRuleState state,
40 long life, long packets, long bytes, Integer flowId) {
alshabib6b5cfec2014-09-18 17:42:18 -070041 this.deviceId = deviceId;
42 this.priority = priority;
43 this.selector = selector;
44 this.treatment = treatment;
alshabiba7f7ca82014-09-22 11:41:23 -070045 this.state = state;
alshabib6b5cfec2014-09-18 17:42:18 -070046
47 this.id = FlowId.valueOf(flowId);
48
alshabib97044902014-09-18 14:52:16 -070049 this.life = life;
alshabib97044902014-09-18 14:52:16 -070050 this.packets = packets;
51 this.bytes = bytes;
alshabib6b5cfec2014-09-18 17:42:18 -070052 this.created = System.currentTimeMillis();
alshabib97044902014-09-18 14:52:16 -070053 }
54
alshabiba7f7ca82014-09-22 11:41:23 -070055 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
56 TrafficTreatment treatement, int priority) {
57 this(deviceId, selector, treatement, priority, FlowRuleState.CREATED);
58 }
59
60 public DefaultFlowRule(FlowRule rule, FlowRuleState state) {
61 this(rule.deviceId(), rule.selector(), rule.treatment(),
62 rule.priority(), state);
63 }
64
alshabib97044902014-09-18 14:52:16 -070065
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070066 @Override
67 public FlowId id() {
68 return id;
alshabib7b2748f2014-09-16 20:21:11 -070069 }
70
71 @Override
72 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070073 return priority;
alshabib7b2748f2014-09-16 20:21:11 -070074 }
75
76 @Override
77 public DeviceId deviceId() {
78 return deviceId;
79 }
80
81 @Override
82 public TrafficSelector selector() {
83 return selector;
84 }
85
86 @Override
87 public TrafficTreatment treatment() {
88 return treatment;
89 }
90
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070091 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070092 public long lifeMillis() {
alshabib97044902014-09-18 14:52:16 -070093 return life;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070094 }
95
96 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070097 public long packets() {
alshabib97044902014-09-18 14:52:16 -070098 return packets;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070099 }
100
101 @Override
102 public long bytes() {
alshabib97044902014-09-18 14:52:16 -0700103 return bytes;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700104 }
105
106 @Override
alshabiba7f7ca82014-09-22 11:41:23 -0700107 public FlowRuleState state() {
108 return this.state;
109 }
110
111
112 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700113 /*
114 * The priority and statistics can change on a given treatment and selector
115 *
116 * (non-Javadoc)
117 * @see java.lang.Object#equals(java.lang.Object)
118 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700119 public int hashCode() {
alshabib6b5cfec2014-09-18 17:42:18 -0700120 return Objects.hash(deviceId, selector, treatment);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700121 }
122
123 @Override
124 /*
125 * The priority and statistics can change on a given treatment and selector
126 *
127 * (non-Javadoc)
128 * @see java.lang.Object#equals(java.lang.Object)
129 */
130 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700131 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700132 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700133 }
alshabiba7f7ca82014-09-22 11:41:23 -0700134 if (obj instanceof FlowRule) {
135 FlowRule that = (FlowRule) obj;
136 return Objects.equals(deviceId, that.deviceId()) &&
137 Objects.equals(id, that.id());
138 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700139 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700140 }
141
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700142 @Override
143 public String toString() {
144 return toStringHelper(this)
145 .add("id", id)
146 .add("deviceId", deviceId)
147 .add("priority", priority)
148 .add("selector", selector)
149 .add("treatment", treatment)
150 .add("created", created)
151 .toString();
152 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700153
alshabib7b2748f2014-09-16 20:21:11 -0700154}