Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.flow; |
| 17 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 19 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 20 | |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 23 | import org.onlab.onos.core.ApplicationId; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.DeviceId; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 25 | import org.slf4j.Logger; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 26 | |
| 27 | public class DefaultFlowRule implements FlowRule { |
| 28 | |
Yuta HIGUCHI | 3498aab | 2014-10-17 21:05:40 -0700 | [diff] [blame] | 29 | private static final Logger log = getLogger(DefaultFlowRule.class); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 30 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 31 | private final DeviceId deviceId; |
| 32 | private final int priority; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 33 | private final TrafficSelector selector; |
| 34 | private final TrafficTreatment treatment; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 35 | private final long created; |
| 36 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 37 | private final FlowId id; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 38 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 39 | private final short appId; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 40 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 41 | private final int timeout; |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 42 | private final boolean permanent; |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 43 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 44 | |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 45 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 46 | TrafficTreatment treatment, int priority, long flowId, |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 47 | int timeout, boolean permanent) { |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 48 | this.deviceId = deviceId; |
| 49 | this.priority = priority; |
| 50 | this.selector = selector; |
| 51 | this.treatment = treatment; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 52 | this.timeout = timeout; |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 53 | this.permanent = permanent; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 54 | this.created = System.currentTimeMillis(); |
| 55 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 56 | this.appId = (short) (flowId >>> 48); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 57 | this.id = FlowId.valueOf(flowId); |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 58 | } |
| 59 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 60 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 61 | TrafficTreatment treatement, int priority, ApplicationId appId, |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 62 | int timeout, boolean permanent) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 63 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 64 | if (priority < FlowRule.MIN_PRIORITY) { |
alshabib | a0e0498 | 2014-10-03 13:03:19 -0700 | [diff] [blame] | 65 | throw new IllegalArgumentException("Priority cannot be less than " + MIN_PRIORITY); |
| 66 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 67 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 68 | this.deviceId = deviceId; |
| 69 | this.priority = priority; |
| 70 | this.selector = selector; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 71 | this.treatment = treatement; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 72 | this.appId = appId.id(); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 73 | this.timeout = timeout; |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 74 | this.permanent = permanent; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 75 | this.created = System.currentTimeMillis(); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 76 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 77 | this.id = FlowId.valueOf((((long) this.appId) << 48) | (this.hash() & 0x0000ffffffffL)); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 78 | } |
| 79 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 80 | 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 Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 88 | this.permanent = rule.isPermanent(); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 89 | this.created = System.currentTimeMillis(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 90 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 91 | } |
| 92 | |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 93 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public FlowId id() { |
| 96 | return id; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 100 | public short appId() { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 101 | return appId; |
| 102 | } |
| 103 | |
| 104 | @Override |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 105 | public int priority() { |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 106 | return priority; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 107 | } |
| 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 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 124 | |
| 125 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 126 | /* |
| 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 Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 132 | public int hashCode() { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 133 | return Objects.hash(deviceId, selector, priority); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | public int hash() { |
alshabib | 58747a6 | 2014-10-07 11:05:30 -0700 | [diff] [blame] | 137 | return Objects.hash(deviceId, selector, treatment); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 138 | } |
| 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) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 148 | if (this == obj) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 149 | return true; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 150 | } |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 151 | if (obj instanceof DefaultFlowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 152 | DefaultFlowRule that = (DefaultFlowRule) obj; |
| 153 | return Objects.equals(deviceId, that.deviceId) && |
alshabib | 58747a6 | 2014-10-07 11:05:30 -0700 | [diff] [blame] | 154 | Objects.equals(id, that.id) && |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 155 | Objects.equals(priority, that.priority) && |
| 156 | Objects.equals(selector, that.selector); |
| 157 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 158 | } |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 159 | return false; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 162 | @Override |
| 163 | public String toString() { |
| 164 | return toStringHelper(this) |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 165 | .add("id", Long.toHexString(id.value())) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 166 | .add("deviceId", deviceId) |
| 167 | .add("priority", priority) |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 168 | .add("selector", selector.criteria()) |
| 169 | .add("treatment", treatment == null ? "N/A" : treatment.instructions()) |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 170 | .add("created", created) |
| 171 | .toString(); |
| 172 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 173 | |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 174 | @Override |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 175 | public int timeout() { |
alshabib | 58747a6 | 2014-10-07 11:05:30 -0700 | [diff] [blame] | 176 | return timeout; |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 179 | @Override |
| 180 | public boolean isPermanent() { |
| 181 | return permanent; |
| 182 | } |
| 183 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 184 | } |