blob: ff7a317dfc19032dca61e4b25ce888b884433bc1 [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;
21
alshabib219ebaa2014-09-22 15:41:24 -070022 private final FlowId id;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070023
alshabiba68eb962014-09-24 20:34:13 -070024 private final ApplicationId appId;
25
alshabibba5ac482014-10-02 17:15:20 -070026 private final int timeout;
alshabib6eb438a2014-10-01 16:39:37 -070027
alshabib1c319ff2014-10-04 20:29:09 -070028
alshabib97044902014-09-18 14:52:16 -070029 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabib1c319ff2014-10-04 20:29:09 -070030 TrafficTreatment treatment, int priority, long flowId,
alshabibba5ac482014-10-02 17:15:20 -070031 int timeout) {
alshabib6b5cfec2014-09-18 17:42:18 -070032 this.deviceId = deviceId;
33 this.priority = priority;
34 this.selector = selector;
35 this.treatment = treatment;
alshabib1c319ff2014-10-04 20:29:09 -070036 this.timeout = timeout;
37 this.created = System.currentTimeMillis();
38
alshabiba68eb962014-09-24 20:34:13 -070039 this.appId = ApplicationId.valueOf((int) (flowId >> 32));
alshabib6b5cfec2014-09-18 17:42:18 -070040 this.id = FlowId.valueOf(flowId);
alshabib97044902014-09-18 14:52:16 -070041 }
42
alshabiba7f7ca82014-09-22 11:41:23 -070043 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabibba5ac482014-10-02 17:15:20 -070044 TrafficTreatment treatement, int priority, ApplicationId appId,
45 int timeout) {
alshabiba7f7ca82014-09-22 11:41:23 -070046
alshabib1c319ff2014-10-04 20:29:09 -070047 if (priority < FlowRule.MIN_PRIORITY) {
alshabiba0e04982014-10-03 13:03:19 -070048 throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY);
49 }
alshabib1c319ff2014-10-04 20:29:09 -070050
alshabib219ebaa2014-09-22 15:41:24 -070051 this.deviceId = deviceId;
52 this.priority = priority;
53 this.selector = selector;
alshabib1c319ff2014-10-04 20:29:09 -070054 this.treatment = treatement;
alshabiba68eb962014-09-24 20:34:13 -070055 this.appId = appId;
alshabibba5ac482014-10-02 17:15:20 -070056 this.timeout = timeout;
alshabib1c319ff2014-10-04 20:29:09 -070057 this.created = System.currentTimeMillis();
alshabibba5ac482014-10-02 17:15:20 -070058
alshabiba68eb962014-09-24 20:34:13 -070059 this.id = FlowId.valueOf((((long) appId().id()) << 32) | (this.hash() & 0xffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -070060 }
61
alshabib1c319ff2014-10-04 20:29:09 -070062 public DefaultFlowRule(FlowRule rule) {
63 this.deviceId = rule.deviceId();
64 this.priority = rule.priority();
65 this.selector = rule.selector();
66 this.treatment = rule.treatment();
67 this.appId = rule.appId();
68 this.id = rule.id();
69 this.timeout = rule.timeout();
alshabib219ebaa2014-09-22 15:41:24 -070070 this.created = System.currentTimeMillis();
alshabib1c319ff2014-10-04 20:29:09 -070071
alshabiba7f7ca82014-09-22 11:41:23 -070072 }
73
alshabib97044902014-09-18 14:52:16 -070074
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070075 @Override
76 public FlowId id() {
77 return id;
alshabib7b2748f2014-09-16 20:21:11 -070078 }
79
80 @Override
alshabiba68eb962014-09-24 20:34:13 -070081 public ApplicationId appId() {
82 return appId;
83 }
84
85 @Override
alshabib7b2748f2014-09-16 20:21:11 -070086 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070087 return priority;
alshabib7b2748f2014-09-16 20:21:11 -070088 }
89
90 @Override
91 public DeviceId deviceId() {
92 return deviceId;
93 }
94
95 @Override
96 public TrafficSelector selector() {
97 return selector;
98 }
99
100 @Override
101 public TrafficTreatment treatment() {
102 return treatment;
103 }
104
alshabiba7f7ca82014-09-22 11:41:23 -0700105
106 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700107 /*
108 * The priority and statistics can change on a given treatment and selector
109 *
110 * (non-Javadoc)
111 * @see java.lang.Object#equals(java.lang.Object)
112 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700113 public int hashCode() {
alshabibba5ac482014-10-02 17:15:20 -0700114 return Objects.hash(deviceId, selector, priority);
alshabiba68eb962014-09-24 20:34:13 -0700115 }
116
117 public int hash() {
alshabib58747a62014-10-07 11:05:30 -0700118 return Objects.hash(deviceId, selector, treatment);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700119 }
120
121 @Override
122 /*
123 * The priority and statistics can change on a given treatment and selector
124 *
125 * (non-Javadoc)
126 * @see java.lang.Object#equals(java.lang.Object)
127 */
128 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700129 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700130 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700131 }
alshabib54ce5892014-09-23 17:50:51 -0700132 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700133 DefaultFlowRule that = (DefaultFlowRule) obj;
134 return Objects.equals(deviceId, that.deviceId) &&
alshabib58747a62014-10-07 11:05:30 -0700135 Objects.equals(id, that.id) &&
alshabibba5ac482014-10-02 17:15:20 -0700136 Objects.equals(priority, that.priority) &&
137 Objects.equals(selector, that.selector);
138
alshabiba7f7ca82014-09-22 11:41:23 -0700139 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700140 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700141 }
142
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700143 @Override
144 public String toString() {
145 return toStringHelper(this)
146 .add("id", id)
147 .add("deviceId", deviceId)
148 .add("priority", priority)
alshabibba5ac482014-10-02 17:15:20 -0700149 .add("selector", selector.criteria())
150 .add("treatment", treatment == null ? "N/A" : treatment.instructions())
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700151 .add("created", created)
152 .toString();
153 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700154
alshabib6eb438a2014-10-01 16:39:37 -0700155 @Override
alshabibba5ac482014-10-02 17:15:20 -0700156 public int timeout() {
alshabib58747a62014-10-07 11:05:30 -0700157 return timeout;
alshabib6eb438a2014-10-01 16:39:37 -0700158 }
159
alshabib7b2748f2014-09-16 20:21:11 -0700160}