blob: 4b3d64fe965dec5b9af2495842fed156e0616aa4 [file] [log] [blame]
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -08003 *
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 */
16package org.onosproject.net.group;
17
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080018import org.onosproject.core.GroupId;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080019import org.onosproject.net.PortNumber;
20import org.onosproject.net.flow.TrafficTreatment;
Ray Milkey42507352015-03-20 15:16:10 -070021import org.onosproject.net.flow.instructions.Instruction;
22
23import java.util.List;
24import java.util.Objects;
25
26import static com.google.common.base.MoreObjects.toStringHelper;
27import static com.google.common.base.Preconditions.checkArgument;
28import static com.google.common.base.Preconditions.checkNotNull;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080029
Thomas Vachuska0cd4de82015-02-03 09:17:08 -080030/**
31 * Group bucket implementation. A group bucket is collection of
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080032 * 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 Vavilapalli10e75cd2015-04-13 16:21:24 -070041public final class DefaultGroupBucket implements GroupBucket, StoredGroupBucketEntry {
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080042 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 Vavilapalli10e75cd2015-04-13 16:21:24 -070047 private long packets;
48 private long bytes;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080049
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 Vachuska0cd4de82015-02-03 09:17:08 -080057 * @param watchGroup group that determines the liveness of group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080058 */
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 Vavilapalli56db94f2015-01-22 22:30:17 -080075 * @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 Vavilapalli56db94f2015-01-22 22:30:17 -080090 * @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 Vavilapalli56db94f2015-01-22 22:30:17 -0800106 * @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 Vachuska0cd4de82015-02-03 09:17:08 -0800127 * @param watchGroup group that determines the liveness of group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800128 * @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 Chanc42e84e2015-10-20 16:24:19 -0700142 /**
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 Cascone5079a7f2019-04-16 17:33:31 -0700156 /**
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 Vavilapalli56db94f2015-01-22 22:30:17 -0800170 @Override
171 public GroupDescription.Type type() {
172 return this.type;
173 }
174
175 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800176 * Returns list of Traffic instructions that are part of the bucket.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800177 *
178 * @return TrafficTreatment Traffic instruction list
179 */
180 @Override
181 public TrafficTreatment treatment() {
182 return treatment;
183 }
184
185 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800186 * Returns weight of select group bucket.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800187 *
188 * @return short weight associated with a bucket
189 */
190 @Override
191 public short weight() {
192 return weight;
193 }
194
195 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800196 * Returns port number used for liveness detection for a
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800197 * 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 Vavilapalli0599d512015-01-30 12:57:56 -0800207 * Returns group identifier used for liveness detection for a
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800208 * 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 Vavilapalli45c27c82015-01-30 12:57:56 -0800216
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 Milkey42507352015-03-20 15:16:10 -0700241 List<Instruction> myInstructions = this.treatment.allInstructions();
242 List<Instruction> theirInstructions = that.treatment.allInstructions();
243
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800244 return Objects.equals(type, that.type) &&
Ray Milkey42507352015-03-20 15:16:10 -0700245 myInstructions.containsAll(theirInstructions) &&
246 theirInstructions.containsAll(myInstructions);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800247 }
248 return false;
249 }
250
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800251 @Override
Victor Silvadf1eeae2016-08-12 15:28:57 -0300252 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 Vavilapallied12ae52015-02-09 14:43:19 -0800259 public String toString() {
260 return toStringHelper(this)
261 .add("type", type)
262 .add("treatment", treatment)
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700263 .add("packets", packets)
264 .add("bytes", bytes)
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800265 .toString();
266 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700267
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 Vavilapalli56db94f2015-01-22 22:30:17 -0800287}