blob: a6593a8718a4d46412801fd269d791c000ded2f8 [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
Yuta HIGUCHI3498aab2014-10-17 21:05:40 -070014 private static final Logger log = getLogger(DefaultFlowRule.class);
alshabib219ebaa2014-09-22 15:41:24 -070015
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
alshabib92c65ad2014-10-08 21:56:05 -070024 private final short appId;
alshabiba68eb962014-09-24 20:34:13 -070025
alshabibba5ac482014-10-02 17:15:20 -070026 private final int timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070027 private final boolean permanent;
alshabib6eb438a2014-10-01 16:39:37 -070028
alshabib1c319ff2014-10-04 20:29:09 -070029
alshabib97044902014-09-18 14:52:16 -070030 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabib1c319ff2014-10-04 20:29:09 -070031 TrafficTreatment treatment, int priority, long flowId,
Jonathan Hartbc4a7932014-10-21 11:46:00 -070032 int timeout, boolean permanent) {
alshabib6b5cfec2014-09-18 17:42:18 -070033 this.deviceId = deviceId;
34 this.priority = priority;
35 this.selector = selector;
36 this.treatment = treatment;
alshabib1c319ff2014-10-04 20:29:09 -070037 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070038 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -070039 this.created = System.currentTimeMillis();
40
alshabib92c65ad2014-10-08 21:56:05 -070041 this.appId = (short) (flowId >>> 48);
alshabib6b5cfec2014-09-18 17:42:18 -070042 this.id = FlowId.valueOf(flowId);
alshabib97044902014-09-18 14:52:16 -070043 }
44
alshabiba7f7ca82014-09-22 11:41:23 -070045 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabibba5ac482014-10-02 17:15:20 -070046 TrafficTreatment treatement, int priority, ApplicationId appId,
Jonathan Hartbc4a7932014-10-21 11:46:00 -070047 int timeout, boolean permanent) {
alshabiba7f7ca82014-09-22 11:41:23 -070048
alshabib1c319ff2014-10-04 20:29:09 -070049 if (priority < FlowRule.MIN_PRIORITY) {
alshabiba0e04982014-10-03 13:03:19 -070050 throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY);
51 }
alshabib1c319ff2014-10-04 20:29:09 -070052
alshabib219ebaa2014-09-22 15:41:24 -070053 this.deviceId = deviceId;
54 this.priority = priority;
55 this.selector = selector;
alshabib1c319ff2014-10-04 20:29:09 -070056 this.treatment = treatement;
alshabib92c65ad2014-10-08 21:56:05 -070057 this.appId = appId.id();
alshabibba5ac482014-10-02 17:15:20 -070058 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070059 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -070060 this.created = System.currentTimeMillis();
alshabibba5ac482014-10-02 17:15:20 -070061
alshabib92c65ad2014-10-08 21:56:05 -070062 this.id = FlowId.valueOf((((long) this.appId) << 48) | (this.hash() & 0x0000ffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -070063 }
64
alshabib1c319ff2014-10-04 20:29:09 -070065 public DefaultFlowRule(FlowRule rule) {
66 this.deviceId = rule.deviceId();
67 this.priority = rule.priority();
68 this.selector = rule.selector();
69 this.treatment = rule.treatment();
70 this.appId = rule.appId();
71 this.id = rule.id();
72 this.timeout = rule.timeout();
Jonathan Hartbc4a7932014-10-21 11:46:00 -070073 this.permanent = rule.isPermanent();
alshabib219ebaa2014-09-22 15:41:24 -070074 this.created = System.currentTimeMillis();
alshabib1c319ff2014-10-04 20:29:09 -070075
alshabiba7f7ca82014-09-22 11:41:23 -070076 }
77
alshabib97044902014-09-18 14:52:16 -070078
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070079 @Override
80 public FlowId id() {
81 return id;
alshabib7b2748f2014-09-16 20:21:11 -070082 }
83
84 @Override
alshabib92c65ad2014-10-08 21:56:05 -070085 public short appId() {
alshabiba68eb962014-09-24 20:34:13 -070086 return appId;
87 }
88
89 @Override
alshabib7b2748f2014-09-16 20:21:11 -070090 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070091 return priority;
alshabib7b2748f2014-09-16 20:21:11 -070092 }
93
94 @Override
95 public DeviceId deviceId() {
96 return deviceId;
97 }
98
99 @Override
100 public TrafficSelector selector() {
101 return selector;
102 }
103
104 @Override
105 public TrafficTreatment treatment() {
106 return treatment;
107 }
108
alshabiba7f7ca82014-09-22 11:41:23 -0700109
110 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700111 /*
112 * The priority and statistics can change on a given treatment and selector
113 *
114 * (non-Javadoc)
115 * @see java.lang.Object#equals(java.lang.Object)
116 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700117 public int hashCode() {
alshabibba5ac482014-10-02 17:15:20 -0700118 return Objects.hash(deviceId, selector, priority);
alshabiba68eb962014-09-24 20:34:13 -0700119 }
120
121 public int hash() {
alshabib58747a62014-10-07 11:05:30 -0700122 return Objects.hash(deviceId, selector, treatment);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700123 }
124
125 @Override
126 /*
127 * The priority and statistics can change on a given treatment and selector
128 *
129 * (non-Javadoc)
130 * @see java.lang.Object#equals(java.lang.Object)
131 */
132 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700133 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700134 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700135 }
alshabib54ce5892014-09-23 17:50:51 -0700136 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700137 DefaultFlowRule that = (DefaultFlowRule) obj;
138 return Objects.equals(deviceId, that.deviceId) &&
alshabib58747a62014-10-07 11:05:30 -0700139 Objects.equals(id, that.id) &&
alshabibba5ac482014-10-02 17:15:20 -0700140 Objects.equals(priority, that.priority) &&
141 Objects.equals(selector, that.selector);
142
alshabiba7f7ca82014-09-22 11:41:23 -0700143 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700144 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700145 }
146
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700147 @Override
148 public String toString() {
149 return toStringHelper(this)
alshabib8ca53902014-10-07 13:11:17 -0700150 .add("id", Long.toHexString(id.value()))
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700151 .add("deviceId", deviceId)
152 .add("priority", priority)
alshabibba5ac482014-10-02 17:15:20 -0700153 .add("selector", selector.criteria())
154 .add("treatment", treatment == null ? "N/A" : treatment.instructions())
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700155 .add("created", created)
156 .toString();
157 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700158
alshabib6eb438a2014-10-01 16:39:37 -0700159 @Override
alshabibba5ac482014-10-02 17:15:20 -0700160 public int timeout() {
alshabib58747a62014-10-07 11:05:30 -0700161 return timeout;
alshabib6eb438a2014-10-01 16:39:37 -0700162 }
163
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700164 @Override
165 public boolean isPermanent() {
166 return permanent;
167 }
168
alshabib7b2748f2014-09-16 20:21:11 -0700169}