blob: 4bbcca1611b517c9c2c55e8f54fa675f38410b7c [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;
alshabib219ebaa2014-09-22 15:41:24 -070019import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070020
alshabib6b5cfec2014-09-18 17:42:18 -070021import java.util.Objects;
22
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070023import org.onlab.onos.core.ApplicationId;
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080024import org.onlab.onos.core.DefaultGroupId;
25import org.onlab.onos.core.GroupId;
alshabib7b2748f2014-09-16 20:21:11 -070026import org.onlab.onos.net.DeviceId;
alshabib219ebaa2014-09-22 15:41:24 -070027import org.slf4j.Logger;
alshabib7b2748f2014-09-16 20:21:11 -070028
29public class DefaultFlowRule implements FlowRule {
30
Yuta HIGUCHI3498aab2014-10-17 21:05:40 -070031 private static final Logger log = getLogger(DefaultFlowRule.class);
alshabib219ebaa2014-09-22 15:41:24 -070032
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070033 private final DeviceId deviceId;
34 private final int priority;
alshabib7b2748f2014-09-16 20:21:11 -070035 private final TrafficSelector selector;
36 private final TrafficTreatment treatment;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070037 private final long created;
38
alshabib219ebaa2014-09-22 15:41:24 -070039 private final FlowId id;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070040
alshabib92c65ad2014-10-08 21:56:05 -070041 private final short appId;
alshabiba68eb962014-09-24 20:34:13 -070042
alshabibba5ac482014-10-02 17:15:20 -070043 private final int timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070044 private final boolean permanent;
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080045 private final GroupId groupId;
alshabib6eb438a2014-10-01 16:39:37 -070046
alshabib1c319ff2014-10-04 20:29:09 -070047
alshabib97044902014-09-18 14:52:16 -070048 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabib1c319ff2014-10-04 20:29:09 -070049 TrafficTreatment treatment, int priority, long flowId,
Jonathan Hartbc4a7932014-10-21 11:46:00 -070050 int timeout, boolean permanent) {
alshabib6b5cfec2014-09-18 17:42:18 -070051 this.deviceId = deviceId;
52 this.priority = priority;
53 this.selector = selector;
54 this.treatment = treatment;
alshabib1c319ff2014-10-04 20:29:09 -070055 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070056 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -070057 this.created = System.currentTimeMillis();
58
alshabib92c65ad2014-10-08 21:56:05 -070059 this.appId = (short) (flowId >>> 48);
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080060 this.groupId = new DefaultGroupId((short) ((flowId >>> 32) & 0xFFFF));
alshabib6b5cfec2014-09-18 17:42:18 -070061 this.id = FlowId.valueOf(flowId);
alshabib97044902014-09-18 14:52:16 -070062 }
63
alshabiba7f7ca82014-09-22 11:41:23 -070064 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabib28204e52014-11-12 18:29:45 -080065 TrafficTreatment treatment, int priority, ApplicationId appId,
66 int timeout, boolean permanent) {
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080067 this(deviceId, selector, treatment, priority, appId, new DefaultGroupId(0), timeout, permanent);
68 }
69
70 @Deprecated
71 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
72 TrafficTreatment treatment, int priority, ApplicationId appId,
73 short groupId, int timeout, boolean permanent) {
74 this(deviceId, selector, treatment, priority, appId, new DefaultGroupId(groupId), timeout, permanent);
alshabib28204e52014-11-12 18:29:45 -080075 }
76
77 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
78 TrafficTreatment treatment, int priority, ApplicationId appId,
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080079 GroupId groupId, int timeout, boolean permanent) {
alshabiba7f7ca82014-09-22 11:41:23 -070080
alshabib1c319ff2014-10-04 20:29:09 -070081 if (priority < FlowRule.MIN_PRIORITY) {
alshabiba0e04982014-10-03 13:03:19 -070082 throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY);
83 }
alshabib1c319ff2014-10-04 20:29:09 -070084
alshabib219ebaa2014-09-22 15:41:24 -070085 this.deviceId = deviceId;
86 this.priority = priority;
87 this.selector = selector;
Ray Milkey1e207112014-11-11 10:38:00 -080088 this.treatment = treatment;
alshabib92c65ad2014-10-08 21:56:05 -070089 this.appId = appId.id();
alshabib28204e52014-11-12 18:29:45 -080090 this.groupId = groupId;
alshabibba5ac482014-10-02 17:15:20 -070091 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070092 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -070093 this.created = System.currentTimeMillis();
alshabibba5ac482014-10-02 17:15:20 -070094
alshabib28204e52014-11-12 18:29:45 -080095 /*
96 * id consists of the following.
97 * | appId (16 bits) | groupId (16 bits) | flowId (32 bits) |
98 */
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -080099 this.id = FlowId.valueOf((((long) this.appId) << 48) | (((long) this.groupId.id()) << 32)
100 | (this.hash() & 0xffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -0700101 }
102
alshabib1c319ff2014-10-04 20:29:09 -0700103 public DefaultFlowRule(FlowRule rule) {
104 this.deviceId = rule.deviceId();
105 this.priority = rule.priority();
106 this.selector = rule.selector();
107 this.treatment = rule.treatment();
108 this.appId = rule.appId();
alshabib28204e52014-11-12 18:29:45 -0800109 this.groupId = rule.groupId();
alshabib1c319ff2014-10-04 20:29:09 -0700110 this.id = rule.id();
111 this.timeout = rule.timeout();
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700112 this.permanent = rule.isPermanent();
alshabib219ebaa2014-09-22 15:41:24 -0700113 this.created = System.currentTimeMillis();
alshabib1c319ff2014-10-04 20:29:09 -0700114
alshabiba7f7ca82014-09-22 11:41:23 -0700115 }
116
alshabib97044902014-09-18 14:52:16 -0700117
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700118 @Override
119 public FlowId id() {
120 return id;
alshabib7b2748f2014-09-16 20:21:11 -0700121 }
122
123 @Override
alshabib92c65ad2014-10-08 21:56:05 -0700124 public short appId() {
alshabiba68eb962014-09-24 20:34:13 -0700125 return appId;
126 }
127
128 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800129 public GroupId groupId() {
alshabib28204e52014-11-12 18:29:45 -0800130 return groupId;
131 }
132
133 @Override
alshabib7b2748f2014-09-16 20:21:11 -0700134 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700135 return priority;
alshabib7b2748f2014-09-16 20:21:11 -0700136 }
137
138 @Override
139 public DeviceId deviceId() {
140 return deviceId;
141 }
142
143 @Override
144 public TrafficSelector selector() {
145 return selector;
146 }
147
148 @Override
149 public TrafficTreatment treatment() {
150 return treatment;
151 }
152
alshabiba7f7ca82014-09-22 11:41:23 -0700153
154 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700155 /*
156 * The priority and statistics can change on a given treatment and selector
157 *
158 * (non-Javadoc)
159 * @see java.lang.Object#equals(java.lang.Object)
160 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700161 public int hashCode() {
alshabibba5ac482014-10-02 17:15:20 -0700162 return Objects.hash(deviceId, selector, priority);
alshabiba68eb962014-09-24 20:34:13 -0700163 }
164
165 public int hash() {
alshabib58747a62014-10-07 11:05:30 -0700166 return Objects.hash(deviceId, selector, treatment);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700167 }
168
169 @Override
170 /*
171 * The priority and statistics can change on a given treatment and selector
172 *
173 * (non-Javadoc)
174 * @see java.lang.Object#equals(java.lang.Object)
175 */
176 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700177 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700178 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700179 }
alshabib54ce5892014-09-23 17:50:51 -0700180 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700181 DefaultFlowRule that = (DefaultFlowRule) obj;
182 return Objects.equals(deviceId, that.deviceId) &&
alshabib58747a62014-10-07 11:05:30 -0700183 Objects.equals(id, that.id) &&
alshabibba5ac482014-10-02 17:15:20 -0700184 Objects.equals(priority, that.priority) &&
185 Objects.equals(selector, that.selector);
186
alshabiba7f7ca82014-09-22 11:41:23 -0700187 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700188 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700189 }
190
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700191 @Override
192 public String toString() {
193 return toStringHelper(this)
alshabib8ca53902014-10-07 13:11:17 -0700194 .add("id", Long.toHexString(id.value()))
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700195 .add("deviceId", deviceId)
196 .add("priority", priority)
alshabibba5ac482014-10-02 17:15:20 -0700197 .add("selector", selector.criteria())
198 .add("treatment", treatment == null ? "N/A" : treatment.instructions())
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700199 .add("created", created)
200 .toString();
201 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700202
alshabib6eb438a2014-10-01 16:39:37 -0700203 @Override
alshabibba5ac482014-10-02 17:15:20 -0700204 public int timeout() {
alshabib58747a62014-10-07 11:05:30 -0700205 return timeout;
alshabib6eb438a2014-10-01 16:39:37 -0700206 }
207
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700208 @Override
209 public boolean isPermanent() {
210 return permanent;
211 }
212
alshabib7b2748f2014-09-16 20:21:11 -0700213}