blob: ad22160a950494b30ec6aaf2af34227cfc3ccfcc [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
alshabib7b2748f2014-09-16 20:21:11 -07008import org.onlab.onos.net.DeviceId;
alshabib219ebaa2014-09-22 15:41:24 -07009import org.slf4j.Logger;
alshabib7b2748f2014-09-16 20:21:11 -070010
11public class DefaultFlowRule implements FlowRule {
12
alshabib219ebaa2014-09-22 15:41:24 -070013 private final Logger log = getLogger(getClass());
14
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070015 private final DeviceId deviceId;
16 private final int priority;
alshabib7b2748f2014-09-16 20:21:11 -070017 private final TrafficSelector selector;
18 private final TrafficTreatment treatment;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070019 private final long created;
alshabib6b5cfec2014-09-18 17:42:18 -070020 private final long life;
alshabib6b5cfec2014-09-18 17:42:18 -070021 private final long packets;
22 private final long bytes;
alshabiba7f7ca82014-09-22 11:41:23 -070023 private final FlowRuleState state;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070024
alshabib219ebaa2014-09-22 15:41:24 -070025 private final FlowId id;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070026
alshabib97044902014-09-18 14:52:16 -070027 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabiba7f7ca82014-09-22 11:41:23 -070028 TrafficTreatment treatment, int priority, FlowRuleState state,
alshabib9290eea2014-09-22 11:58:17 -070029 long life, long packets, long bytes, long flowId) {
alshabib6b5cfec2014-09-18 17:42:18 -070030 this.deviceId = deviceId;
31 this.priority = priority;
32 this.selector = selector;
33 this.treatment = treatment;
alshabiba7f7ca82014-09-22 11:41:23 -070034 this.state = state;
alshabib6b5cfec2014-09-18 17:42:18 -070035
36 this.id = FlowId.valueOf(flowId);
37
alshabib97044902014-09-18 14:52:16 -070038 this.life = life;
alshabib97044902014-09-18 14:52:16 -070039 this.packets = packets;
40 this.bytes = bytes;
alshabib6b5cfec2014-09-18 17:42:18 -070041 this.created = System.currentTimeMillis();
alshabib97044902014-09-18 14:52:16 -070042 }
43
alshabiba7f7ca82014-09-22 11:41:23 -070044 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
45 TrafficTreatment treatement, int priority) {
46 this(deviceId, selector, treatement, priority, FlowRuleState.CREATED);
47 }
48
49 public DefaultFlowRule(FlowRule rule, FlowRuleState state) {
50 this(rule.deviceId(), rule.selector(), rule.treatment(),
alshabib219ebaa2014-09-22 15:41:24 -070051 rule.priority(), state, rule.id());
52 }
53
54 private DefaultFlowRule(DeviceId deviceId,
55 TrafficSelector selector, TrafficTreatment treatment,
56 int priority, FlowRuleState state) {
57 this.deviceId = deviceId;
58 this.priority = priority;
59 this.selector = selector;
60 this.treatment = treatment;
61 this.state = state;
62 this.life = 0;
63 this.packets = 0;
64 this.bytes = 0;
65 this.id = FlowId.valueOf(this.hashCode());
66 this.created = System.currentTimeMillis();
67 }
68
69 private DefaultFlowRule(DeviceId deviceId,
70 TrafficSelector selector, TrafficTreatment treatment,
71 int priority, FlowRuleState state, FlowId flowId) {
72 this.deviceId = deviceId;
73 this.priority = priority;
74 this.selector = selector;
75 this.treatment = treatment;
76 this.state = state;
77 this.life = 0;
78 this.packets = 0;
79 this.bytes = 0;
80 this.id = flowId;
81 this.created = System.currentTimeMillis();
alshabiba7f7ca82014-09-22 11:41:23 -070082 }
83
alshabib97044902014-09-18 14:52:16 -070084
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070085 @Override
86 public FlowId id() {
87 return id;
alshabib7b2748f2014-09-16 20:21:11 -070088 }
89
90 @Override
91 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070092 return priority;
alshabib7b2748f2014-09-16 20:21:11 -070093 }
94
95 @Override
96 public DeviceId deviceId() {
97 return deviceId;
98 }
99
100 @Override
101 public TrafficSelector selector() {
102 return selector;
103 }
104
105 @Override
106 public TrafficTreatment treatment() {
107 return treatment;
108 }
109
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700110 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700111 public long lifeMillis() {
alshabib97044902014-09-18 14:52:16 -0700112 return life;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700113 }
114
115 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700116 public long packets() {
alshabib97044902014-09-18 14:52:16 -0700117 return packets;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700118 }
119
120 @Override
121 public long bytes() {
alshabib97044902014-09-18 14:52:16 -0700122 return bytes;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700123 }
124
125 @Override
alshabiba7f7ca82014-09-22 11:41:23 -0700126 public FlowRuleState state() {
127 return this.state;
128 }
129
130
131 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700132 /*
133 * The priority and statistics can change on a given treatment and selector
134 *
135 * (non-Javadoc)
136 * @see java.lang.Object#equals(java.lang.Object)
137 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700138 public int hashCode() {
alshabib6b5cfec2014-09-18 17:42:18 -0700139 return Objects.hash(deviceId, selector, treatment);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700140 }
141
142 @Override
143 /*
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 */
149 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700150 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700151 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700152 }
alshabib54ce5892014-09-23 17:50:51 -0700153 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700154 DefaultFlowRule that = (DefaultFlowRule) obj;
155 return Objects.equals(deviceId, that.deviceId) &&
156 Objects.equals(id, that.id);
alshabiba7f7ca82014-09-22 11:41:23 -0700157 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700158 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700159 }
160
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700161 @Override
162 public String toString() {
163 return toStringHelper(this)
164 .add("id", id)
165 .add("deviceId", deviceId)
166 .add("priority", priority)
167 .add("selector", selector)
168 .add("treatment", treatment)
169 .add("created", created)
alshabibbb8b1282014-09-22 17:00:18 -0700170 .add("state", state)
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700171 .toString();
172 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700173
alshabib7b2748f2014-09-16 20:21:11 -0700174}