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