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 | |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 156 | @Override |
| 157 | public GroupDescription.Type type() { |
| 158 | return this.type; |
| 159 | } |
| 160 | |
| 161 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 162 | * Returns list of Traffic instructions that are part of the bucket. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 163 | * |
| 164 | * @return TrafficTreatment Traffic instruction list |
| 165 | */ |
| 166 | @Override |
| 167 | public TrafficTreatment treatment() { |
| 168 | return treatment; |
| 169 | } |
| 170 | |
| 171 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 172 | * Returns weight of select group bucket. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 173 | * |
| 174 | * @return short weight associated with a bucket |
| 175 | */ |
| 176 | @Override |
| 177 | public short weight() { |
| 178 | return weight; |
| 179 | } |
| 180 | |
| 181 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 182 | * Returns port number used for liveness detection for a |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 183 | * failover bucket. |
| 184 | * |
| 185 | * @return PortNumber port number used for liveness detection |
| 186 | */ |
| 187 | @Override |
| 188 | public PortNumber watchPort() { |
| 189 | return watchPort; |
| 190 | } |
| 191 | |
| 192 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 193 | * Returns group identifier used for liveness detection for a |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 194 | * failover bucket. |
| 195 | * |
| 196 | * @return GroupId group identifier to be used for liveness detection |
| 197 | */ |
| 198 | @Override |
| 199 | public GroupId watchGroup() { |
| 200 | return watchGroup; |
| 201 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * The type and treatment can change on a given bucket |
| 205 | * |
| 206 | * (non-Javadoc) |
| 207 | * @see java.lang.Object#equals(java.lang.Object) |
| 208 | */ |
| 209 | @Override |
| 210 | public int hashCode() { |
| 211 | return Objects.hash(type, treatment); |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * The priority and statistics can change on a given treatment and selector |
| 216 | * |
| 217 | * (non-Javadoc) |
| 218 | * @see java.lang.Object#equals(java.lang.Object) |
| 219 | */ |
| 220 | @Override |
| 221 | public boolean equals(Object obj) { |
| 222 | if (this == obj) { |
| 223 | return true; |
| 224 | } |
| 225 | if (obj instanceof DefaultGroupBucket) { |
| 226 | DefaultGroupBucket that = (DefaultGroupBucket) obj; |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 227 | List<Instruction> myInstructions = this.treatment.allInstructions(); |
| 228 | List<Instruction> theirInstructions = that.treatment.allInstructions(); |
| 229 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 230 | return Objects.equals(type, that.type) && |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 231 | myInstructions.containsAll(theirInstructions) && |
| 232 | theirInstructions.containsAll(myInstructions); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 237 | @Override |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 238 | public boolean hasSameParameters(GroupBucket other) { |
| 239 | return weight == other.weight() && |
| 240 | Objects.equals(watchPort, other.watchPort()) && |
| 241 | Objects.equals(watchGroup, other.watchGroup()); |
| 242 | } |
| 243 | |
| 244 | @Override |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 245 | public String toString() { |
| 246 | return toStringHelper(this) |
| 247 | .add("type", type) |
| 248 | .add("treatment", treatment) |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 249 | .add("packets", packets) |
| 250 | .add("bytes", bytes) |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 251 | .toString(); |
| 252 | } |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 253 | |
| 254 | @Override |
| 255 | public long packets() { |
| 256 | return packets; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public long bytes() { |
| 261 | return bytes; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void setPackets(long packets) { |
| 266 | this.packets = packets; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void setBytes(long bytes) { |
| 271 | this.bytes = bytes; |
| 272 | } |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 273 | } |