blob: deb654ed6767c4730aee8fd59b42263384456938 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
alshabib7b2748f2014-09-16 20:21:11 -070019package org.onlab.onos.net.flow;
20
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070021import static com.google.common.base.MoreObjects.toStringHelper;
alshabib219ebaa2014-09-22 15:41:24 -070022import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070023
alshabib6b5cfec2014-09-18 17:42:18 -070024import java.util.Objects;
25
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070026import org.onlab.onos.core.ApplicationId;
alshabib7b2748f2014-09-16 20:21:11 -070027import org.onlab.onos.net.DeviceId;
alshabib219ebaa2014-09-22 15:41:24 -070028import org.slf4j.Logger;
alshabib7b2748f2014-09-16 20:21:11 -070029
30public class DefaultFlowRule implements FlowRule {
31
Yuta HIGUCHI3498aab2014-10-17 21:05:40 -070032 private static final Logger log = getLogger(DefaultFlowRule.class);
alshabib219ebaa2014-09-22 15:41:24 -070033
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070034 private final DeviceId deviceId;
35 private final int priority;
alshabib7b2748f2014-09-16 20:21:11 -070036 private final TrafficSelector selector;
37 private final TrafficTreatment treatment;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070038 private final long created;
39
alshabib219ebaa2014-09-22 15:41:24 -070040 private final FlowId id;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070041
alshabib92c65ad2014-10-08 21:56:05 -070042 private final short appId;
alshabiba68eb962014-09-24 20:34:13 -070043
alshabibba5ac482014-10-02 17:15:20 -070044 private final int timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070045 private final boolean permanent;
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);
alshabib6b5cfec2014-09-18 17:42:18 -070060 this.id = FlowId.valueOf(flowId);
alshabib97044902014-09-18 14:52:16 -070061 }
62
alshabiba7f7ca82014-09-22 11:41:23 -070063 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabibba5ac482014-10-02 17:15:20 -070064 TrafficTreatment treatement, int priority, ApplicationId appId,
Jonathan Hartbc4a7932014-10-21 11:46:00 -070065 int timeout, boolean permanent) {
alshabiba7f7ca82014-09-22 11:41:23 -070066
alshabib1c319ff2014-10-04 20:29:09 -070067 if (priority < FlowRule.MIN_PRIORITY) {
alshabiba0e04982014-10-03 13:03:19 -070068 throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY);
69 }
alshabib1c319ff2014-10-04 20:29:09 -070070
alshabib219ebaa2014-09-22 15:41:24 -070071 this.deviceId = deviceId;
72 this.priority = priority;
73 this.selector = selector;
alshabib1c319ff2014-10-04 20:29:09 -070074 this.treatment = treatement;
alshabib92c65ad2014-10-08 21:56:05 -070075 this.appId = appId.id();
alshabibba5ac482014-10-02 17:15:20 -070076 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070077 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -070078 this.created = System.currentTimeMillis();
alshabibba5ac482014-10-02 17:15:20 -070079
alshabib92c65ad2014-10-08 21:56:05 -070080 this.id = FlowId.valueOf((((long) this.appId) << 48) | (this.hash() & 0x0000ffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -070081 }
82
alshabib1c319ff2014-10-04 20:29:09 -070083 public DefaultFlowRule(FlowRule rule) {
84 this.deviceId = rule.deviceId();
85 this.priority = rule.priority();
86 this.selector = rule.selector();
87 this.treatment = rule.treatment();
88 this.appId = rule.appId();
89 this.id = rule.id();
90 this.timeout = rule.timeout();
Jonathan Hartbc4a7932014-10-21 11:46:00 -070091 this.permanent = rule.isPermanent();
alshabib219ebaa2014-09-22 15:41:24 -070092 this.created = System.currentTimeMillis();
alshabib1c319ff2014-10-04 20:29:09 -070093
alshabiba7f7ca82014-09-22 11:41:23 -070094 }
95
alshabib97044902014-09-18 14:52:16 -070096
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070097 @Override
98 public FlowId id() {
99 return id;
alshabib7b2748f2014-09-16 20:21:11 -0700100 }
101
102 @Override
alshabib92c65ad2014-10-08 21:56:05 -0700103 public short appId() {
alshabiba68eb962014-09-24 20:34:13 -0700104 return appId;
105 }
106
107 @Override
alshabib7b2748f2014-09-16 20:21:11 -0700108 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700109 return priority;
alshabib7b2748f2014-09-16 20:21:11 -0700110 }
111
112 @Override
113 public DeviceId deviceId() {
114 return deviceId;
115 }
116
117 @Override
118 public TrafficSelector selector() {
119 return selector;
120 }
121
122 @Override
123 public TrafficTreatment treatment() {
124 return treatment;
125 }
126
alshabiba7f7ca82014-09-22 11:41:23 -0700127
128 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700129 /*
130 * The priority and statistics can change on a given treatment and selector
131 *
132 * (non-Javadoc)
133 * @see java.lang.Object#equals(java.lang.Object)
134 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700135 public int hashCode() {
alshabibba5ac482014-10-02 17:15:20 -0700136 return Objects.hash(deviceId, selector, priority);
alshabiba68eb962014-09-24 20:34:13 -0700137 }
138
139 public int hash() {
alshabib58747a62014-10-07 11:05:30 -0700140 return Objects.hash(deviceId, selector, treatment);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700141 }
142
143 @Override
144 /*
145 * The priority and statistics can change on a given treatment and selector
146 *
147 * (non-Javadoc)
148 * @see java.lang.Object#equals(java.lang.Object)
149 */
150 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700151 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700152 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700153 }
alshabib54ce5892014-09-23 17:50:51 -0700154 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700155 DefaultFlowRule that = (DefaultFlowRule) obj;
156 return Objects.equals(deviceId, that.deviceId) &&
alshabib58747a62014-10-07 11:05:30 -0700157 Objects.equals(id, that.id) &&
alshabibba5ac482014-10-02 17:15:20 -0700158 Objects.equals(priority, that.priority) &&
159 Objects.equals(selector, that.selector);
160
alshabiba7f7ca82014-09-22 11:41:23 -0700161 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700162 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700163 }
164
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700165 @Override
166 public String toString() {
167 return toStringHelper(this)
alshabib8ca53902014-10-07 13:11:17 -0700168 .add("id", Long.toHexString(id.value()))
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700169 .add("deviceId", deviceId)
170 .add("priority", priority)
alshabibba5ac482014-10-02 17:15:20 -0700171 .add("selector", selector.criteria())
172 .add("treatment", treatment == null ? "N/A" : treatment.instructions())
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700173 .add("created", created)
174 .toString();
175 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700176
alshabib6eb438a2014-10-01 16:39:37 -0700177 @Override
alshabibba5ac482014-10-02 17:15:20 -0700178 public int timeout() {
alshabib58747a62014-10-07 11:05:30 -0700179 return timeout;
alshabib6eb438a2014-10-01 16:39:37 -0700180 }
181
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700182 @Override
183 public boolean isPermanent() {
184 return permanent;
185 }
186
alshabib7b2748f2014-09-16 20:21:11 -0700187}