blob: 0cdc290266d71e152f95935683a5f5ec0e06cc79 [file] [log] [blame]
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800156 @Override
157 public GroupDescription.Type type() {
158 return this.type;
159 }
160
161 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800162 * Returns list of Traffic instructions that are part of the bucket.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800163 *
164 * @return TrafficTreatment Traffic instruction list
165 */
166 @Override
167 public TrafficTreatment treatment() {
168 return treatment;
169 }
170
171 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800172 * Returns weight of select group bucket.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800173 *
174 * @return short weight associated with a bucket
175 */
176 @Override
177 public short weight() {
178 return weight;
179 }
180
181 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800182 * Returns port number used for liveness detection for a
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800183 * 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 Vavilapalli0599d512015-01-30 12:57:56 -0800193 * Returns group identifier used for liveness detection for a
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800194 * 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 Vavilapalli45c27c82015-01-30 12:57:56 -0800202
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 Milkey42507352015-03-20 15:16:10 -0700227 List<Instruction> myInstructions = this.treatment.allInstructions();
228 List<Instruction> theirInstructions = that.treatment.allInstructions();
229
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800230 return Objects.equals(type, that.type) &&
Ray Milkey42507352015-03-20 15:16:10 -0700231 myInstructions.containsAll(theirInstructions) &&
232 theirInstructions.containsAll(myInstructions);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800233 }
234 return false;
235 }
236
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800237 @Override
238 public String toString() {
239 return toStringHelper(this)
240 .add("type", type)
241 .add("treatment", treatment)
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700242 .add("packets", packets)
243 .add("bytes", bytes)
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800244 .toString();
245 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700246
247 @Override
248 public long packets() {
249 return packets;
250 }
251
252 @Override
253 public long bytes() {
254 return bytes;
255 }
256
257 @Override
258 public void setPackets(long packets) {
259 this.packets = packets;
260 }
261
262 @Override
263 public void setBytes(long bytes) {
264 this.bytes = bytes;
265 }
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800266}