blob: 168f0440434b62cf3caacc77b4278db584363628 [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 */
alshabib7b2748f2014-09-16 20:21:11 -070016package org.onlab.onos.net.flow;
17
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
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070022import org.onlab.onos.core.ApplicationId;
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080023import org.onlab.onos.core.DefaultGroupId;
24import org.onlab.onos.core.GroupId;
alshabib7b2748f2014-09-16 20:21:11 -070025import org.onlab.onos.net.DeviceId;
26
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
alshabib1c319ff2014-10-04 20:29:09 -070043
alshabib97044902014-09-18 14:52:16 -070044 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabib1c319ff2014-10-04 20:29:09 -070045 TrafficTreatment treatment, int priority, long flowId,
Jonathan Hartbc4a7932014-10-21 11:46:00 -070046 int timeout, boolean permanent) {
alshabib6b5cfec2014-09-18 17:42:18 -070047 this.deviceId = deviceId;
48 this.priority = priority;
49 this.selector = selector;
50 this.treatment = treatment;
alshabib1c319ff2014-10-04 20:29:09 -070051 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070052 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -070053 this.created = System.currentTimeMillis();
54
alshabib92c65ad2014-10-08 21:56:05 -070055 this.appId = (short) (flowId >>> 48);
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080056 this.groupId = new DefaultGroupId((short) ((flowId >>> 32) & 0xFFFF));
alshabib6b5cfec2014-09-18 17:42:18 -070057 this.id = FlowId.valueOf(flowId);
alshabib97044902014-09-18 14:52:16 -070058 }
59
alshabiba7f7ca82014-09-22 11:41:23 -070060 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabib28204e52014-11-12 18:29:45 -080061 TrafficTreatment treatment, int priority, ApplicationId appId,
62 int timeout, boolean permanent) {
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080063 this(deviceId, selector, treatment, priority, appId, new DefaultGroupId(0), timeout, permanent);
64 }
65
66 @Deprecated
67 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
68 TrafficTreatment treatment, int priority, ApplicationId appId,
69 short groupId, int timeout, boolean permanent) {
70 this(deviceId, selector, treatment, priority, appId, new DefaultGroupId(groupId), timeout, permanent);
alshabib28204e52014-11-12 18:29:45 -080071 }
72
73 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
74 TrafficTreatment treatment, int priority, ApplicationId appId,
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080075 GroupId groupId, int timeout, boolean permanent) {
alshabiba7f7ca82014-09-22 11:41:23 -070076
alshabib1c319ff2014-10-04 20:29:09 -070077 if (priority < FlowRule.MIN_PRIORITY) {
alshabiba0e04982014-10-03 13:03:19 -070078 throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY);
79 }
alshabib1c319ff2014-10-04 20:29:09 -070080
alshabib219ebaa2014-09-22 15:41:24 -070081 this.deviceId = deviceId;
82 this.priority = priority;
83 this.selector = selector;
Ray Milkey1e207112014-11-11 10:38:00 -080084 this.treatment = treatment;
alshabib92c65ad2014-10-08 21:56:05 -070085 this.appId = appId.id();
alshabib28204e52014-11-12 18:29:45 -080086 this.groupId = groupId;
alshabibba5ac482014-10-02 17:15:20 -070087 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070088 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -070089 this.created = System.currentTimeMillis();
alshabibba5ac482014-10-02 17:15:20 -070090
alshabib28204e52014-11-12 18:29:45 -080091 /*
92 * id consists of the following.
93 * | appId (16 bits) | groupId (16 bits) | flowId (32 bits) |
94 */
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080095 this.id = FlowId.valueOf((((long) this.appId) << 48) | (((long) this.groupId.id()) << 32)
96 | (this.hash() & 0xffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -070097 }
98
alshabib1c319ff2014-10-04 20:29:09 -070099 public DefaultFlowRule(FlowRule rule) {
100 this.deviceId = rule.deviceId();
101 this.priority = rule.priority();
102 this.selector = rule.selector();
103 this.treatment = rule.treatment();
104 this.appId = rule.appId();
alshabib28204e52014-11-12 18:29:45 -0800105 this.groupId = rule.groupId();
alshabib1c319ff2014-10-04 20:29:09 -0700106 this.id = rule.id();
107 this.timeout = rule.timeout();
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700108 this.permanent = rule.isPermanent();
alshabib219ebaa2014-09-22 15:41:24 -0700109 this.created = System.currentTimeMillis();
alshabib1c319ff2014-10-04 20:29:09 -0700110
alshabiba7f7ca82014-09-22 11:41:23 -0700111 }
112
alshabib97044902014-09-18 14:52:16 -0700113
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700114 @Override
115 public FlowId id() {
116 return id;
alshabib7b2748f2014-09-16 20:21:11 -0700117 }
118
119 @Override
alshabib92c65ad2014-10-08 21:56:05 -0700120 public short appId() {
alshabiba68eb962014-09-24 20:34:13 -0700121 return appId;
122 }
123
124 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800125 public GroupId groupId() {
alshabib28204e52014-11-12 18:29:45 -0800126 return groupId;
127 }
128
129 @Override
alshabib7b2748f2014-09-16 20:21:11 -0700130 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700131 return priority;
alshabib7b2748f2014-09-16 20:21:11 -0700132 }
133
134 @Override
135 public DeviceId deviceId() {
136 return deviceId;
137 }
138
139 @Override
140 public TrafficSelector selector() {
141 return selector;
142 }
143
144 @Override
145 public TrafficTreatment treatment() {
146 return treatment;
147 }
148
alshabiba7f7ca82014-09-22 11:41:23 -0700149
150 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700151 /*
152 * The priority and statistics can change on a given treatment and selector
153 *
154 * (non-Javadoc)
155 * @see java.lang.Object#equals(java.lang.Object)
156 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700157 public int hashCode() {
alshabibba5ac482014-10-02 17:15:20 -0700158 return Objects.hash(deviceId, selector, priority);
alshabiba68eb962014-09-24 20:34:13 -0700159 }
160
161 public int hash() {
alshabib58747a62014-10-07 11:05:30 -0700162 return Objects.hash(deviceId, selector, treatment);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700163 }
164
165 @Override
166 /*
167 * The priority and statistics can change on a given treatment and selector
168 *
169 * (non-Javadoc)
170 * @see java.lang.Object#equals(java.lang.Object)
171 */
172 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700173 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700174 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700175 }
alshabib54ce5892014-09-23 17:50:51 -0700176 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700177 DefaultFlowRule that = (DefaultFlowRule) obj;
178 return Objects.equals(deviceId, that.deviceId) &&
alshabibba5ac482014-10-02 17:15:20 -0700179 Objects.equals(priority, that.priority) &&
180 Objects.equals(selector, that.selector);
181
alshabiba7f7ca82014-09-22 11:41:23 -0700182 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700183 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700184 }
185
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700186 @Override
187 public String toString() {
188 return toStringHelper(this)
alshabib8ca53902014-10-07 13:11:17 -0700189 .add("id", Long.toHexString(id.value()))
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700190 .add("deviceId", deviceId)
191 .add("priority", priority)
alshabibba5ac482014-10-02 17:15:20 -0700192 .add("selector", selector.criteria())
193 .add("treatment", treatment == null ? "N/A" : treatment.instructions())
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700194 .add("created", created)
195 .toString();
196 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700197
alshabib6eb438a2014-10-01 16:39:37 -0700198 @Override
alshabibba5ac482014-10-02 17:15:20 -0700199 public int timeout() {
alshabib58747a62014-10-07 11:05:30 -0700200 return timeout;
alshabib6eb438a2014-10-01 16:39:37 -0700201 }
202
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700203 @Override
204 public boolean isPermanent() {
205 return permanent;
206 }
207
alshabib7b2748f2014-09-16 20:21:11 -0700208}