blob: deef14e73f5050249c6fb28e0cc5ce1ee7971e88 [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;
alshabib7b2748f2014-09-16 20:21:11 -070024import org.onlab.onos.net.DeviceId;
alshabib219ebaa2014-09-22 15:41:24 -070025import org.slf4j.Logger;
alshabib7b2748f2014-09-16 20:21:11 -070026
27public class DefaultFlowRule implements FlowRule {
28
Yuta HIGUCHI3498aab2014-10-17 21:05:40 -070029 private static final Logger log = getLogger(DefaultFlowRule.class);
alshabib219ebaa2014-09-22 15:41:24 -070030
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070031 private final DeviceId deviceId;
32 private final int priority;
alshabib7b2748f2014-09-16 20:21:11 -070033 private final TrafficSelector selector;
34 private final TrafficTreatment treatment;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070035 private final long created;
36
alshabib219ebaa2014-09-22 15:41:24 -070037 private final FlowId id;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070038
alshabib92c65ad2014-10-08 21:56:05 -070039 private final short appId;
alshabiba68eb962014-09-24 20:34:13 -070040
alshabibba5ac482014-10-02 17:15:20 -070041 private final int timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070042 private final boolean permanent;
alshabib28204e52014-11-12 18:29:45 -080043 private final short groupId;
alshabib6eb438a2014-10-01 16:39:37 -070044
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);
alshabib28204e52014-11-12 18:29:45 -080058 this.groupId = (short) ((flowId >>> 32) & 0xFFFF);
alshabib6b5cfec2014-09-18 17:42:18 -070059 this.id = FlowId.valueOf(flowId);
alshabib97044902014-09-18 14:52:16 -070060 }
61
alshabiba7f7ca82014-09-22 11:41:23 -070062 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabib28204e52014-11-12 18:29:45 -080063 TrafficTreatment treatment, int priority, ApplicationId appId,
64 int timeout, boolean permanent) {
65 this(deviceId, selector, treatment, priority, appId, (short) 0, timeout, permanent);
66 }
67
68 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
69 TrafficTreatment treatment, int priority, ApplicationId appId,
70 short groupId, int timeout, boolean permanent) {
alshabiba7f7ca82014-09-22 11:41:23 -070071
alshabib1c319ff2014-10-04 20:29:09 -070072 if (priority < FlowRule.MIN_PRIORITY) {
alshabiba0e04982014-10-03 13:03:19 -070073 throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY);
74 }
alshabib1c319ff2014-10-04 20:29:09 -070075
alshabib219ebaa2014-09-22 15:41:24 -070076 this.deviceId = deviceId;
77 this.priority = priority;
78 this.selector = selector;
Ray Milkey1e207112014-11-11 10:38:00 -080079 this.treatment = treatment;
alshabib92c65ad2014-10-08 21:56:05 -070080 this.appId = appId.id();
alshabib28204e52014-11-12 18:29:45 -080081 this.groupId = groupId;
alshabibba5ac482014-10-02 17:15:20 -070082 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070083 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -070084 this.created = System.currentTimeMillis();
alshabibba5ac482014-10-02 17:15:20 -070085
alshabib28204e52014-11-12 18:29:45 -080086 /*
87 * id consists of the following.
88 * | appId (16 bits) | groupId (16 bits) | flowId (32 bits) |
89 */
90 this.id = FlowId.valueOf((((long) this.appId) << 48) | (((long) this.groupId) << 32)
91 | (this.hash() & 0xffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -070092 }
93
alshabib1c319ff2014-10-04 20:29:09 -070094 public DefaultFlowRule(FlowRule rule) {
95 this.deviceId = rule.deviceId();
96 this.priority = rule.priority();
97 this.selector = rule.selector();
98 this.treatment = rule.treatment();
99 this.appId = rule.appId();
alshabib28204e52014-11-12 18:29:45 -0800100 this.groupId = rule.groupId();
alshabib1c319ff2014-10-04 20:29:09 -0700101 this.id = rule.id();
102 this.timeout = rule.timeout();
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700103 this.permanent = rule.isPermanent();
alshabib219ebaa2014-09-22 15:41:24 -0700104 this.created = System.currentTimeMillis();
alshabib1c319ff2014-10-04 20:29:09 -0700105
alshabiba7f7ca82014-09-22 11:41:23 -0700106 }
107
alshabib97044902014-09-18 14:52:16 -0700108
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700109 @Override
110 public FlowId id() {
111 return id;
alshabib7b2748f2014-09-16 20:21:11 -0700112 }
113
114 @Override
alshabib92c65ad2014-10-08 21:56:05 -0700115 public short appId() {
alshabiba68eb962014-09-24 20:34:13 -0700116 return appId;
117 }
118
119 @Override
alshabib28204e52014-11-12 18:29:45 -0800120 public short groupId() {
121 return groupId;
122 }
123
124 @Override
alshabib7b2748f2014-09-16 20:21:11 -0700125 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700126 return priority;
alshabib7b2748f2014-09-16 20:21:11 -0700127 }
128
129 @Override
130 public DeviceId deviceId() {
131 return deviceId;
132 }
133
134 @Override
135 public TrafficSelector selector() {
136 return selector;
137 }
138
139 @Override
140 public TrafficTreatment treatment() {
141 return treatment;
142 }
143
alshabiba7f7ca82014-09-22 11:41:23 -0700144
145 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700146 /*
147 * The priority and statistics can change on a given treatment and selector
148 *
149 * (non-Javadoc)
150 * @see java.lang.Object#equals(java.lang.Object)
151 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700152 public int hashCode() {
alshabibba5ac482014-10-02 17:15:20 -0700153 return Objects.hash(deviceId, selector, priority);
alshabiba68eb962014-09-24 20:34:13 -0700154 }
155
156 public int hash() {
alshabib58747a62014-10-07 11:05:30 -0700157 return Objects.hash(deviceId, selector, treatment);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700158 }
159
160 @Override
161 /*
162 * The priority and statistics can change on a given treatment and selector
163 *
164 * (non-Javadoc)
165 * @see java.lang.Object#equals(java.lang.Object)
166 */
167 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700168 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700169 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700170 }
alshabib54ce5892014-09-23 17:50:51 -0700171 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700172 DefaultFlowRule that = (DefaultFlowRule) obj;
173 return Objects.equals(deviceId, that.deviceId) &&
alshabib58747a62014-10-07 11:05:30 -0700174 Objects.equals(id, that.id) &&
alshabibba5ac482014-10-02 17:15:20 -0700175 Objects.equals(priority, that.priority) &&
176 Objects.equals(selector, that.selector);
177
alshabiba7f7ca82014-09-22 11:41:23 -0700178 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700179 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700180 }
181
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700182 @Override
183 public String toString() {
184 return toStringHelper(this)
alshabib8ca53902014-10-07 13:11:17 -0700185 .add("id", Long.toHexString(id.value()))
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700186 .add("deviceId", deviceId)
187 .add("priority", priority)
alshabibba5ac482014-10-02 17:15:20 -0700188 .add("selector", selector.criteria())
189 .add("treatment", treatment == null ? "N/A" : treatment.instructions())
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700190 .add("created", created)
191 .toString();
192 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700193
alshabib6eb438a2014-10-01 16:39:37 -0700194 @Override
alshabibba5ac482014-10-02 17:15:20 -0700195 public int timeout() {
alshabib58747a62014-10-07 11:05:30 -0700196 return timeout;
alshabib6eb438a2014-10-01 16:39:37 -0700197 }
198
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700199 @Override
200 public boolean isPermanent() {
201 return permanent;
202 }
203
alshabib7b2748f2014-09-16 20:21:11 -0700204}