blob: 6e59cf564854cf87ca328d817bdbded631c7bb80 [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;
alshabib6eb438a2014-10-01 16:39:37 -070043
alshabib1c319ff2014-10-04 20:29:09 -070044
alshabib97044902014-09-18 14:52:16 -070045 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
alshabib1c319ff2014-10-04 20:29:09 -070046 TrafficTreatment treatment, int priority, long flowId,
Jonathan Hartbc4a7932014-10-21 11:46:00 -070047 int timeout, boolean permanent) {
alshabib6b5cfec2014-09-18 17:42:18 -070048 this.deviceId = deviceId;
49 this.priority = priority;
50 this.selector = selector;
51 this.treatment = treatment;
alshabib1c319ff2014-10-04 20:29:09 -070052 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070053 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -070054 this.created = System.currentTimeMillis();
55
alshabib92c65ad2014-10-08 21:56:05 -070056 this.appId = (short) (flowId >>> 48);
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,
alshabibba5ac482014-10-02 17:15:20 -070061 TrafficTreatment treatement, int priority, ApplicationId appId,
Jonathan Hartbc4a7932014-10-21 11:46:00 -070062 int timeout, boolean permanent) {
alshabiba7f7ca82014-09-22 11:41:23 -070063
alshabib1c319ff2014-10-04 20:29:09 -070064 if (priority < FlowRule.MIN_PRIORITY) {
alshabiba0e04982014-10-03 13:03:19 -070065 throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY);
66 }
alshabib1c319ff2014-10-04 20:29:09 -070067
alshabib219ebaa2014-09-22 15:41:24 -070068 this.deviceId = deviceId;
69 this.priority = priority;
70 this.selector = selector;
alshabib1c319ff2014-10-04 20:29:09 -070071 this.treatment = treatement;
alshabib92c65ad2014-10-08 21:56:05 -070072 this.appId = appId.id();
alshabibba5ac482014-10-02 17:15:20 -070073 this.timeout = timeout;
Jonathan Hartbc4a7932014-10-21 11:46:00 -070074 this.permanent = permanent;
alshabib1c319ff2014-10-04 20:29:09 -070075 this.created = System.currentTimeMillis();
alshabibba5ac482014-10-02 17:15:20 -070076
alshabib92c65ad2014-10-08 21:56:05 -070077 this.id = FlowId.valueOf((((long) this.appId) << 48) | (this.hash() & 0x0000ffffffffL));
alshabib219ebaa2014-09-22 15:41:24 -070078 }
79
alshabib1c319ff2014-10-04 20:29:09 -070080 public DefaultFlowRule(FlowRule rule) {
81 this.deviceId = rule.deviceId();
82 this.priority = rule.priority();
83 this.selector = rule.selector();
84 this.treatment = rule.treatment();
85 this.appId = rule.appId();
86 this.id = rule.id();
87 this.timeout = rule.timeout();
Jonathan Hartbc4a7932014-10-21 11:46:00 -070088 this.permanent = rule.isPermanent();
alshabib219ebaa2014-09-22 15:41:24 -070089 this.created = System.currentTimeMillis();
alshabib1c319ff2014-10-04 20:29:09 -070090
alshabiba7f7ca82014-09-22 11:41:23 -070091 }
92
alshabib97044902014-09-18 14:52:16 -070093
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070094 @Override
95 public FlowId id() {
96 return id;
alshabib7b2748f2014-09-16 20:21:11 -070097 }
98
99 @Override
alshabib92c65ad2014-10-08 21:56:05 -0700100 public short appId() {
alshabiba68eb962014-09-24 20:34:13 -0700101 return appId;
102 }
103
104 @Override
alshabib7b2748f2014-09-16 20:21:11 -0700105 public int priority() {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700106 return priority;
alshabib7b2748f2014-09-16 20:21:11 -0700107 }
108
109 @Override
110 public DeviceId deviceId() {
111 return deviceId;
112 }
113
114 @Override
115 public TrafficSelector selector() {
116 return selector;
117 }
118
119 @Override
120 public TrafficTreatment treatment() {
121 return treatment;
122 }
123
alshabiba7f7ca82014-09-22 11:41:23 -0700124
125 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700126 /*
127 * The priority and statistics can change on a given treatment and selector
128 *
129 * (non-Javadoc)
130 * @see java.lang.Object#equals(java.lang.Object)
131 */
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700132 public int hashCode() {
alshabibba5ac482014-10-02 17:15:20 -0700133 return Objects.hash(deviceId, selector, priority);
alshabiba68eb962014-09-24 20:34:13 -0700134 }
135
136 public int hash() {
alshabib58747a62014-10-07 11:05:30 -0700137 return Objects.hash(deviceId, selector, treatment);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700138 }
139
140 @Override
141 /*
142 * The priority and statistics can change on a given treatment and selector
143 *
144 * (non-Javadoc)
145 * @see java.lang.Object#equals(java.lang.Object)
146 */
147 public boolean equals(Object obj) {
alshabiba7f7ca82014-09-22 11:41:23 -0700148 if (this == obj) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700149 return true;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700150 }
alshabib54ce5892014-09-23 17:50:51 -0700151 if (obj instanceof DefaultFlowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700152 DefaultFlowRule that = (DefaultFlowRule) obj;
153 return Objects.equals(deviceId, that.deviceId) &&
alshabib58747a62014-10-07 11:05:30 -0700154 Objects.equals(id, that.id) &&
alshabibba5ac482014-10-02 17:15:20 -0700155 Objects.equals(priority, that.priority) &&
156 Objects.equals(selector, that.selector);
157
alshabiba7f7ca82014-09-22 11:41:23 -0700158 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700159 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700160 }
161
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700162 @Override
163 public String toString() {
164 return toStringHelper(this)
alshabib8ca53902014-10-07 13:11:17 -0700165 .add("id", Long.toHexString(id.value()))
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700166 .add("deviceId", deviceId)
167 .add("priority", priority)
alshabibba5ac482014-10-02 17:15:20 -0700168 .add("selector", selector.criteria())
169 .add("treatment", treatment == null ? "N/A" : treatment.instructions())
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700170 .add("created", created)
171 .toString();
172 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700173
alshabib6eb438a2014-10-01 16:39:37 -0700174 @Override
alshabibba5ac482014-10-02 17:15:20 -0700175 public int timeout() {
alshabib58747a62014-10-07 11:05:30 -0700176 return timeout;
alshabib6eb438a2014-10-01 16:39:37 -0700177 }
178
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700179 @Override
180 public boolean isPermanent() {
181 return permanent;
182 }
183
alshabib7b2748f2014-09-16 20:21:11 -0700184}