Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.net.group; |
| 17 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 18 | import org.onosproject.core.GroupId; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 19 | import org.onosproject.net.PortNumber; |
| 20 | import org.onosproject.net.flow.TrafficTreatment; |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 21 | import org.onosproject.net.flow.instructions.Instruction; |
| 22 | |
| 23 | import java.util.List; |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 29 | |
Thomas Vachuska | 0cd4de8 | 2015-02-03 09:17:08 -0800 | [diff] [blame] | 30 | /** |
| 31 | * Group bucket implementation. A group bucket is collection of |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 32 | * instructions that can be performed on a traffic flow. A select |
| 33 | * Group can have one or more Buckets where traffic will be |
| 34 | * processed by a single bucket in the group, based on device |
| 35 | * specific selection algorithm (e.g. hash on some fields of the |
| 36 | * incoming traffic flows or round robin) and hence can contains |
| 37 | * optional weight field to define the weights among the buckets |
| 38 | * in the group. A failover group bucket is associated with a |
| 39 | * specific port or group that controls its liveness. |
| 40 | */ |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 41 | public final class DefaultGroupBucket implements GroupBucket, StoredGroupBucketEntry { |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 42 | private final GroupDescription.Type type; |
| 43 | private final TrafficTreatment treatment; |
| 44 | private final short weight; |
| 45 | private final PortNumber watchPort; |
| 46 | private final GroupId watchGroup; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 47 | private long packets; |
| 48 | private long bytes; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Group bucket constructor with the parameters. |
| 52 | * |
| 53 | * @param type group bucket type |
| 54 | * @param treatment traffic treatment associated with group bucket |
| 55 | * @param weight optional weight associated with group bucket |
| 56 | * @param watchPort port that determines the liveness of group bucket |
Thomas Vachuska | 0cd4de8 | 2015-02-03 09:17:08 -0800 | [diff] [blame] | 57 | * @param watchGroup group that determines the liveness of group bucket |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 58 | */ |
| 59 | private DefaultGroupBucket(GroupDescription.Type type, |
| 60 | TrafficTreatment treatment, |
| 61 | short weight, |
| 62 | PortNumber watchPort, |
| 63 | GroupId watchGroup) { |
| 64 | this.type = type; |
| 65 | this.treatment = checkNotNull(treatment); |
| 66 | this.weight = weight; |
| 67 | this.watchPort = watchPort; |
| 68 | this.watchGroup = watchGroup; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Creates indirect group bucket. |
| 73 | * |
| 74 | * @param treatment traffic treatment associated with group bucket |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 75 | * @return indirect group bucket object |
| 76 | */ |
| 77 | public static GroupBucket createIndirectGroupBucket( |
| 78 | TrafficTreatment treatment) { |
| 79 | return new DefaultGroupBucket(GroupDescription.Type.INDIRECT, |
| 80 | treatment, |
| 81 | (short) -1, |
| 82 | null, |
| 83 | null); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Creates select group bucket with weight as 1. |
| 88 | * |
| 89 | * @param treatment traffic treatment associated with group bucket |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 90 | * @return select group bucket object |
| 91 | */ |
| 92 | public static GroupBucket createSelectGroupBucket( |
| 93 | TrafficTreatment treatment) { |
| 94 | return new DefaultGroupBucket(GroupDescription.Type.SELECT, |
| 95 | treatment, |
| 96 | (short) 1, |
| 97 | null, |
| 98 | null); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Creates select group bucket with specified weight. |
| 103 | * |
| 104 | * @param treatment traffic treatment associated with group bucket |
| 105 | * @param weight weight associated with group bucket |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 106 | * @return select group bucket object |
| 107 | */ |
| 108 | public static GroupBucket createSelectGroupBucket( |
| 109 | TrafficTreatment treatment, |
| 110 | short weight) { |
| 111 | if (weight == 0) { |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | return new DefaultGroupBucket(GroupDescription.Type.SELECT, |
| 116 | treatment, |
| 117 | weight, |
| 118 | null, |
| 119 | null); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Creates failover group bucket with watchport or watchgroup. |
| 124 | * |
| 125 | * @param treatment traffic treatment associated with group bucket |
| 126 | * @param watchPort port that determines the liveness of group bucket |
Thomas Vachuska | 0cd4de8 | 2015-02-03 09:17:08 -0800 | [diff] [blame] | 127 | * @param watchGroup group that determines the liveness of group bucket |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 128 | * @return failover group bucket object |
| 129 | */ |
| 130 | public static GroupBucket createFailoverGroupBucket( |
| 131 | TrafficTreatment treatment, |
| 132 | PortNumber watchPort, |
| 133 | GroupId watchGroup) { |
| 134 | checkArgument(((watchPort != null) || (watchGroup != null))); |
| 135 | return new DefaultGroupBucket(GroupDescription.Type.FAILOVER, |
| 136 | treatment, |
| 137 | (short) -1, |
| 138 | watchPort, |
| 139 | watchGroup); |
| 140 | } |
| 141 | |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Creates all group bucket. |
| 144 | * |
| 145 | * @param treatment traffic treatment associated with group bucket |
| 146 | * @return all group bucket object |
| 147 | */ |
| 148 | public static GroupBucket createAllGroupBucket(TrafficTreatment treatment) { |
| 149 | return new DefaultGroupBucket(GroupDescription.Type.ALL, |
| 150 | treatment, |
| 151 | (short) -1, |
| 152 | null, |
| 153 | null); |
| 154 | } |
| 155 | |
Carmelo Cascone | 5079a7f | 2019-04-16 17:33:31 -0700 | [diff] [blame] | 156 | /** |
| 157 | * Creates clone group bucket. |
| 158 | * |
| 159 | * @param treatment traffic treatment associated with group bucket |
| 160 | * @return clone group bucket object |
| 161 | */ |
| 162 | public static GroupBucket createCloneGroupBucket(TrafficTreatment treatment) { |
| 163 | return new DefaultGroupBucket(GroupDescription.Type.CLONE, |
| 164 | treatment, |
| 165 | (short) -1, |
| 166 | null, |
| 167 | null); |
| 168 | } |
| 169 | |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 170 | @Override |
| 171 | public GroupDescription.Type type() { |
| 172 | return this.type; |
| 173 | } |
| 174 | |
| 175 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 176 | * Returns list of Traffic instructions that are part of the bucket. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 177 | * |
| 178 | * @return TrafficTreatment Traffic instruction list |
| 179 | */ |
| 180 | @Override |
| 181 | public TrafficTreatment treatment() { |
| 182 | return treatment; |
| 183 | } |
| 184 | |
| 185 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 186 | * Returns weight of select group bucket. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 187 | * |
| 188 | * @return short weight associated with a bucket |
| 189 | */ |
| 190 | @Override |
| 191 | public short weight() { |
| 192 | return weight; |
| 193 | } |
| 194 | |
| 195 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 196 | * Returns port number used for liveness detection for a |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 197 | * failover bucket. |
| 198 | * |
| 199 | * @return PortNumber port number used for liveness detection |
| 200 | */ |
| 201 | @Override |
| 202 | public PortNumber watchPort() { |
| 203 | return watchPort; |
| 204 | } |
| 205 | |
| 206 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 207 | * Returns group identifier used for liveness detection for a |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 208 | * failover bucket. |
| 209 | * |
| 210 | * @return GroupId group identifier to be used for liveness detection |
| 211 | */ |
| 212 | @Override |
| 213 | public GroupId watchGroup() { |
| 214 | return watchGroup; |
| 215 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * The type and treatment can change on a given bucket |
| 219 | * |
| 220 | * (non-Javadoc) |
| 221 | * @see java.lang.Object#equals(java.lang.Object) |
| 222 | */ |
| 223 | @Override |
| 224 | public int hashCode() { |
| 225 | return Objects.hash(type, treatment); |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * The priority and statistics can change on a given treatment and selector |
| 230 | * |
| 231 | * (non-Javadoc) |
| 232 | * @see java.lang.Object#equals(java.lang.Object) |
| 233 | */ |
| 234 | @Override |
| 235 | public boolean equals(Object obj) { |
| 236 | if (this == obj) { |
| 237 | return true; |
| 238 | } |
| 239 | if (obj instanceof DefaultGroupBucket) { |
| 240 | DefaultGroupBucket that = (DefaultGroupBucket) obj; |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 241 | List<Instruction> myInstructions = this.treatment.allInstructions(); |
| 242 | List<Instruction> theirInstructions = that.treatment.allInstructions(); |
| 243 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 244 | return Objects.equals(type, that.type) && |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 245 | myInstructions.containsAll(theirInstructions) && |
| 246 | theirInstructions.containsAll(myInstructions); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 251 | @Override |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 252 | public boolean hasSameParameters(GroupBucket other) { |
| 253 | return weight == other.weight() && |
| 254 | Objects.equals(watchPort, other.watchPort()) && |
| 255 | Objects.equals(watchGroup, other.watchGroup()); |
| 256 | } |
| 257 | |
| 258 | @Override |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 259 | public String toString() { |
| 260 | return toStringHelper(this) |
| 261 | .add("type", type) |
| 262 | .add("treatment", treatment) |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 263 | .add("packets", packets) |
| 264 | .add("bytes", bytes) |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 265 | .toString(); |
| 266 | } |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 267 | |
| 268 | @Override |
| 269 | public long packets() { |
| 270 | return packets; |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public long bytes() { |
| 275 | return bytes; |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public void setPackets(long packets) { |
| 280 | this.packets = packets; |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void setBytes(long bytes) { |
| 285 | this.bytes = bytes; |
| 286 | } |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 287 | } |