blob: e555794b304b16cc9fb5148df1a8216009ab90a5 [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,
alshabib28204e52014-11-12 18:29:45 -080064 TrafficTreatment treatment, int priority, ApplicationId appId,
65 int timeout, boolean permanent) {
sangho11c30ac2015-01-22 14:30:55 -080066 this(deviceId, selector, treatment, priority, appId, new DefaultGroupId(0),
67 timeout, permanent);
68 }
69
70 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
71 TrafficTreatment treatment, int priority, ApplicationId appId,
72 int timeout, boolean permanent, Type type) {
73
74 if (priority < FlowRule.MIN_PRIORITY) {
75 throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY);
76 }
77
78 this.deviceId = deviceId;
79 this.priority = priority;
80 this.selector = selector;
81 this.treatment = treatment;
82 this.appId = appId.id();
83 this.groupId = new DefaultGroupId(0);
84 this.timeout = timeout;
85 this.permanent = permanent;
86 this.created = System.currentTimeMillis();
87 this.type = type;
88
89 /*
90 * id consists of the following.
91 * | appId (16 bits) | groupId (16 bits) | flowId (32 bits) |
92 */
93 this.id = FlowId.valueOf((((long) this.appId) << 48) | (((long) this.groupId.id()) << 32)
94 | (this.hash() & 0xffffffffL));
95
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080096 }
97
alshabib28204e52014-11-12 18:29:45 -080098 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
99 TrafficTreatment treatment, int priority, ApplicationId appId,
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800100 GroupId groupId, int timeout, boolean permanent) {
alshabiba7f7ca82014-09-22 11:41:23 -0700101
alshabib1c319ff2014-10-04 20:29:09 -0700102 if (priority < FlowRule.MIN_PRIORITY) {
alshabiba0e04982014-10-03 13:03:19 -0700103 throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY);
104 }
alshabib1c319ff2014-10-04 20:29:09 -0700105
alshabib219ebaa2014-09-22 15:41:24 -0700106 this.deviceId = deviceId;
107 this.priority = priority;
108 this.selector = selector;
Ray Milkey1e207112014-11-11 10:38:00 -0800109 this.treatment = treatment;
alshabib92c65ad2014-10-08 21:56:05 -0700110 this.appId = appId.id();
alshabib28204e52014-11-12 18:29:45 -0800111 this.groupId = groupId;
alshabibba5ac482014-10-02 17:15:20 -0700112 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700113 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -0700114 this.created = System.currentTimeMillis();
sangho11c30ac2015-01-22 14:30:55 -0800115 this.type = Type.DEFAULT;
alshabibba5ac482014-10-02 17:15:20 -0700116
alshabib28204e52014-11-12 18:29:45 -0800117 /*
118 * id consists of the following.
119 * | appId (16 bits) | groupId (16 bits) | flowId (32 bits) |
120 */
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800121 this.id = FlowId.valueOf((((long) this.appId) << 48) | (((long) this.groupId.id()) << 32)
122 | (this.hash() & 0xffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -0700123 }
124
alshabib1c319ff2014-10-04 20:29:09 -0700125 public DefaultFlowRule(FlowRule rule) {
126 this.deviceId = rule.deviceId();
127 this.priority = rule.priority();
128 this.selector = rule.selector();
129 this.treatment = rule.treatment();
130 this.appId = rule.appId();
alshabib28204e52014-11-12 18:29:45 -0800131 this.groupId = rule.groupId();
alshabib1c319ff2014-10-04 20:29:09 -0700132 this.id = rule.id();
133 this.timeout = rule.timeout();
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700134 this.permanent = rule.isPermanent();
alshabib219ebaa2014-09-22 15:41:24 -0700135 this.created = System.currentTimeMillis();
sangho11c30ac2015-01-22 14:30:55 -0800136 this.type = rule.type();
alshabib1c319ff2014-10-04 20:29:09 -0700137
alshabiba7f7ca82014-09-22 11:41:23 -0700138 }
139
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700140 @Override
141 public FlowId id() {
142 return id;
alshabib7b2748f2014-09-16 20:21:11 -0700143 }
144
145 @Override
alshabib92c65ad2014-10-08 21:56:05 -0700146 public short appId() {
alshabiba68eb962014-09-24 20:34:13 -0700147 return appId;
148 }
149
150 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800151 public GroupId groupId() {
alshabib28204e52014-11-12 18:29:45 -0800152 return groupId;
153 }
154
155 @Override
alshabib7b2748f2014-09-16 20:21:11 -0700156 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700157 return priority;
alshabib7b2748f2014-09-16 20:21:11 -0700158 }
159
160 @Override
161 public DeviceId deviceId() {
162 return deviceId;
163 }
164
165 @Override
166 public TrafficSelector selector() {
167 return selector;
168 }
169
170 @Override
171 public TrafficTreatment treatment() {
172 return treatment;
173 }
174
alshabiba7f7ca82014-09-22 11:41:23 -0700175
176 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700177 /*
178 * The priority and statistics can change on a given treatment and selector
179 *
180 * (non-Javadoc)
181 * @see java.lang.Object#equals(java.lang.Object)
182 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700183 public int hashCode() {
alshabibba5ac482014-10-02 17:15:20 -0700184 return Objects.hash(deviceId, selector, priority);
alshabiba68eb962014-09-24 20:34:13 -0700185 }
186
187 public int hash() {
alshabib58747a62014-10-07 11:05:30 -0700188 return Objects.hash(deviceId, selector, treatment);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700189 }
190
191 @Override
192 /*
193 * The priority and statistics can change on a given treatment and selector
194 *
195 * (non-Javadoc)
196 * @see java.lang.Object#equals(java.lang.Object)
197 */
198 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700199 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700200 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700201 }
alshabib54ce5892014-09-23 17:50:51 -0700202 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700203 DefaultFlowRule that = (DefaultFlowRule) obj;
204 return Objects.equals(deviceId, that.deviceId) &&
alshabibba5ac482014-10-02 17:15:20 -0700205 Objects.equals(priority, that.priority) &&
206 Objects.equals(selector, that.selector);
207
alshabiba7f7ca82014-09-22 11:41:23 -0700208 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700209 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700210 }
211
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700212 @Override
213 public String toString() {
214 return toStringHelper(this)
alshabib8ca53902014-10-07 13:11:17 -0700215 .add("id", Long.toHexString(id.value()))
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700216 .add("deviceId", deviceId)
217 .add("priority", priority)
alshabibba5ac482014-10-02 17:15:20 -0700218 .add("selector", selector.criteria())
219 .add("treatment", treatment == null ? "N/A" : treatment.instructions())
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700220 .add("created", created)
221 .toString();
222 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700223
alshabib6eb438a2014-10-01 16:39:37 -0700224 @Override
alshabibba5ac482014-10-02 17:15:20 -0700225 public int timeout() {
alshabib58747a62014-10-07 11:05:30 -0700226 return timeout;
alshabib6eb438a2014-10-01 16:39:37 -0700227 }
228
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700229 @Override
230 public boolean isPermanent() {
231 return permanent;
232 }
233
sangho11c30ac2015-01-22 14:30:55 -0800234 @Override
235 public Type type() {
236 return type;
237 }
238
alshabib7b2748f2014-09-16 20:21:11 -0700239}