blob: f083d183fef37cf91c404004e7f17b86d3a48c00 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow;
alshabib7b2748f2014-09-16 20:21:11 -070017
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070018import static com.google.common.base.MoreObjects.toStringHelper;
19
alshabib6b5cfec2014-09-18 17:42:18 -070020import java.util.Objects;
21
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.core.ApplicationId;
23import org.onosproject.core.DefaultGroupId;
24import org.onosproject.core.GroupId;
25import org.onosproject.net.DeviceId;
alshabib7b2748f2014-09-16 20:21:11 -070026
27public class DefaultFlowRule implements FlowRule {
28
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070029 private final DeviceId deviceId;
30 private final int priority;
alshabib7b2748f2014-09-16 20:21:11 -070031 private final TrafficSelector selector;
32 private final TrafficTreatment treatment;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070033 private final long created;
34
alshabib219ebaa2014-09-22 15:41:24 -070035 private final FlowId id;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070036
alshabib92c65ad2014-10-08 21:56:05 -070037 private final short appId;
alshabiba68eb962014-09-24 20:34:13 -070038
alshabibba5ac482014-10-02 17:15:20 -070039 private final int timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070040 private final boolean permanent;
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080041 private final GroupId groupId;
alshabib6eb438a2014-10-01 16:39:37 -070042
sangho11c30ac2015-01-22 14:30:55 -080043 private final Type type;
44
alshabib1c319ff2014-10-04 20:29:09 -070045
alshabib97044902014-09-18 14:52:16 -070046 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabib1c319ff2014-10-04 20:29:09 -070047 TrafficTreatment treatment, int priority, long flowId,
Jonathan Hartbc4a7932014-10-21 11:46:00 -070048 int timeout, boolean permanent) {
alshabib6b5cfec2014-09-18 17:42:18 -070049 this.deviceId = deviceId;
50 this.priority = priority;
51 this.selector = selector;
52 this.treatment = treatment;
alshabib1c319ff2014-10-04 20:29:09 -070053 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070054 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -070055 this.created = System.currentTimeMillis();
56
alshabib92c65ad2014-10-08 21:56:05 -070057 this.appId = (short) (flowId >>> 48);
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080058 this.groupId = new DefaultGroupId((short) ((flowId >>> 32) & 0xFFFF));
alshabib6b5cfec2014-09-18 17:42:18 -070059 this.id = FlowId.valueOf(flowId);
sangho11c30ac2015-01-22 14:30:55 -080060 this.type = Type.DEFAULT;
alshabib97044902014-09-18 14:52:16 -070061 }
62
alshabiba7f7ca82014-09-22 11:41:23 -070063 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
Saurav Dasfa2fa932015-03-03 11:29:48 -080064 TrafficTreatment treatment, int priority, long flowId,
65 int timeout, boolean permanent, Type tableType) {
66 this.deviceId = deviceId;
67 this.priority = priority;
68 this.selector = selector;
69 this.treatment = treatment;
70 this.timeout = timeout;
71 this.permanent = permanent;
72 this.created = System.currentTimeMillis();
73
74 this.appId = (short) (flowId >>> 48);
75 this.groupId = new DefaultGroupId((short) ((flowId >>> 32) & 0xFFFF));
76 this.id = FlowId.valueOf(flowId);
77 this.type = tableType;
78 }
79
80 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabib28204e52014-11-12 18:29:45 -080081 TrafficTreatment treatment, int priority, ApplicationId appId,
82 int timeout, boolean permanent) {
sangho11c30ac2015-01-22 14:30:55 -080083 this(deviceId, selector, treatment, priority, appId, new DefaultGroupId(0),
84 timeout, permanent);
85 }
86
87 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
88 TrafficTreatment treatment, int priority, ApplicationId appId,
89 int timeout, boolean permanent, Type type) {
90
91 if (priority < FlowRule.MIN_PRIORITY) {
92 throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY);
93 }
94
95 this.deviceId = deviceId;
96 this.priority = priority;
97 this.selector = selector;
98 this.treatment = treatment;
99 this.appId = appId.id();
100 this.groupId = new DefaultGroupId(0);
101 this.timeout = timeout;
102 this.permanent = permanent;
103 this.created = System.currentTimeMillis();
104 this.type = type;
105
106 /*
107 * id consists of the following.
108 * | appId (16 bits) | groupId (16 bits) | flowId (32 bits) |
109 */
110 this.id = FlowId.valueOf((((long) this.appId) << 48) | (((long) this.groupId.id()) << 32)
111 | (this.hash() & 0xffffffffL));
112
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800113 }
114
alshabib28204e52014-11-12 18:29:45 -0800115 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
116 TrafficTreatment treatment, int priority, ApplicationId appId,
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800117 GroupId groupId, int timeout, boolean permanent) {
alshabiba7f7ca82014-09-22 11:41:23 -0700118
alshabib1c319ff2014-10-04 20:29:09 -0700119 if (priority < FlowRule.MIN_PRIORITY) {
alshabiba0e04982014-10-03 13:03:19 -0700120 throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY);
121 }
alshabib1c319ff2014-10-04 20:29:09 -0700122
alshabib219ebaa2014-09-22 15:41:24 -0700123 this.deviceId = deviceId;
124 this.priority = priority;
125 this.selector = selector;
Ray Milkey1e207112014-11-11 10:38:00 -0800126 this.treatment = treatment;
alshabib92c65ad2014-10-08 21:56:05 -0700127 this.appId = appId.id();
alshabib28204e52014-11-12 18:29:45 -0800128 this.groupId = groupId;
alshabibba5ac482014-10-02 17:15:20 -0700129 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700130 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -0700131 this.created = System.currentTimeMillis();
sangho11c30ac2015-01-22 14:30:55 -0800132 this.type = Type.DEFAULT;
alshabibba5ac482014-10-02 17:15:20 -0700133
alshabib28204e52014-11-12 18:29:45 -0800134 /*
135 * id consists of the following.
136 * | appId (16 bits) | groupId (16 bits) | flowId (32 bits) |
137 */
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800138 this.id = FlowId.valueOf((((long) this.appId) << 48) | (((long) this.groupId.id()) << 32)
139 | (this.hash() & 0xffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -0700140 }
141
alshabib1c319ff2014-10-04 20:29:09 -0700142 public DefaultFlowRule(FlowRule rule) {
143 this.deviceId = rule.deviceId();
144 this.priority = rule.priority();
145 this.selector = rule.selector();
146 this.treatment = rule.treatment();
147 this.appId = rule.appId();
alshabib28204e52014-11-12 18:29:45 -0800148 this.groupId = rule.groupId();
alshabib1c319ff2014-10-04 20:29:09 -0700149 this.id = rule.id();
150 this.timeout = rule.timeout();
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700151 this.permanent = rule.isPermanent();
alshabib219ebaa2014-09-22 15:41:24 -0700152 this.created = System.currentTimeMillis();
sangho11c30ac2015-01-22 14:30:55 -0800153 this.type = rule.type();
alshabib1c319ff2014-10-04 20:29:09 -0700154
alshabiba7f7ca82014-09-22 11:41:23 -0700155 }
156
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700157 @Override
158 public FlowId id() {
159 return id;
alshabib7b2748f2014-09-16 20:21:11 -0700160 }
161
162 @Override
alshabib92c65ad2014-10-08 21:56:05 -0700163 public short appId() {
alshabiba68eb962014-09-24 20:34:13 -0700164 return appId;
165 }
166
167 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800168 public GroupId groupId() {
alshabib28204e52014-11-12 18:29:45 -0800169 return groupId;
170 }
171
172 @Override
alshabib7b2748f2014-09-16 20:21:11 -0700173 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700174 return priority;
alshabib7b2748f2014-09-16 20:21:11 -0700175 }
176
177 @Override
178 public DeviceId deviceId() {
179 return deviceId;
180 }
181
182 @Override
183 public TrafficSelector selector() {
184 return selector;
185 }
186
187 @Override
188 public TrafficTreatment treatment() {
189 return treatment;
190 }
191
alshabiba7f7ca82014-09-22 11:41:23 -0700192 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700193 /*
194 * The priority and statistics can change on a given treatment and selector
195 *
196 * (non-Javadoc)
197 * @see java.lang.Object#equals(java.lang.Object)
198 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700199 public int hashCode() {
Saurav Dascbe6de32015-03-01 18:30:46 -0800200 return Objects.hash(deviceId, selector, priority, type);
alshabiba68eb962014-09-24 20:34:13 -0700201 }
202
203 public int hash() {
Saurav Dascbe6de32015-03-01 18:30:46 -0800204 return Objects.hash(deviceId, selector, treatment, type);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700205 }
206
207 @Override
208 /*
209 * The priority and statistics can change on a given treatment and selector
210 *
211 * (non-Javadoc)
212 * @see java.lang.Object#equals(java.lang.Object)
213 */
214 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700215 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700216 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700217 }
alshabib54ce5892014-09-23 17:50:51 -0700218 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700219 DefaultFlowRule that = (DefaultFlowRule) obj;
220 return Objects.equals(deviceId, that.deviceId) &&
alshabibba5ac482014-10-02 17:15:20 -0700221 Objects.equals(priority, that.priority) &&
Saurav Dascbe6de32015-03-01 18:30:46 -0800222 Objects.equals(selector, that.selector) &&
223 Objects.equals(type, that.type);
alshabibba5ac482014-10-02 17:15:20 -0700224
alshabiba7f7ca82014-09-22 11:41:23 -0700225 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700226 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700227 }
228
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700229 @Override
230 public String toString() {
231 return toStringHelper(this)
alshabib8ca53902014-10-07 13:11:17 -0700232 .add("id", Long.toHexString(id.value()))
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700233 .add("deviceId", deviceId)
234 .add("priority", priority)
alshabibba5ac482014-10-02 17:15:20 -0700235 .add("selector", selector.criteria())
236 .add("treatment", treatment == null ? "N/A" : treatment.instructions())
Saurav Dascbe6de32015-03-01 18:30:46 -0800237 .add("table type", type)
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700238 .add("created", created)
239 .toString();
240 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700241
alshabib6eb438a2014-10-01 16:39:37 -0700242 @Override
alshabibba5ac482014-10-02 17:15:20 -0700243 public int timeout() {
alshabib58747a62014-10-07 11:05:30 -0700244 return timeout;
alshabib6eb438a2014-10-01 16:39:37 -0700245 }
246
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700247 @Override
248 public boolean isPermanent() {
249 return permanent;
250 }
251
sangho11c30ac2015-01-22 14:30:55 -0800252 @Override
253 public Type type() {
254 return type;
255 }
256
alshabib7b2748f2014-09-16 20:21:11 -0700257}