blob: 6efd3e79aab93377d90380704cc311b8b2663509 [file] [log] [blame]
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
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
142 @Override
143 public GroupDescription.Type type() {
144 return this.type;
145 }
146
147 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800148 * Returns list of Traffic instructions that are part of the bucket.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800149 *
150 * @return TrafficTreatment Traffic instruction list
151 */
152 @Override
153 public TrafficTreatment treatment() {
154 return treatment;
155 }
156
157 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800158 * Returns weight of select group bucket.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800159 *
160 * @return short weight associated with a bucket
161 */
162 @Override
163 public short weight() {
164 return weight;
165 }
166
167 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800168 * Returns port number used for liveness detection for a
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800169 * failover bucket.
170 *
171 * @return PortNumber port number used for liveness detection
172 */
173 @Override
174 public PortNumber watchPort() {
175 return watchPort;
176 }
177
178 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800179 * Returns group identifier used for liveness detection for a
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800180 * failover bucket.
181 *
182 * @return GroupId group identifier to be used for liveness detection
183 */
184 @Override
185 public GroupId watchGroup() {
186 return watchGroup;
187 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800188
189 /*
190 * The type and treatment can change on a given bucket
191 *
192 * (non-Javadoc)
193 * @see java.lang.Object#equals(java.lang.Object)
194 */
195 @Override
196 public int hashCode() {
197 return Objects.hash(type, treatment);
198 }
199
200 /*
201 * The priority and statistics can change on a given treatment and selector
202 *
203 * (non-Javadoc)
204 * @see java.lang.Object#equals(java.lang.Object)
205 */
206 @Override
207 public boolean equals(Object obj) {
208 if (this == obj) {
209 return true;
210 }
211 if (obj instanceof DefaultGroupBucket) {
212 DefaultGroupBucket that = (DefaultGroupBucket) obj;
Ray Milkey42507352015-03-20 15:16:10 -0700213 List<Instruction> myInstructions = this.treatment.allInstructions();
214 List<Instruction> theirInstructions = that.treatment.allInstructions();
215
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800216 return Objects.equals(type, that.type) &&
Ray Milkey42507352015-03-20 15:16:10 -0700217 myInstructions.containsAll(theirInstructions) &&
218 theirInstructions.containsAll(myInstructions);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800219 }
220 return false;
221 }
222
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800223 @Override
224 public String toString() {
225 return toStringHelper(this)
226 .add("type", type)
227 .add("treatment", treatment)
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700228 .add("packets", packets)
229 .add("bytes", bytes)
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800230 .toString();
231 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700232
233 @Override
234 public long packets() {
235 return packets;
236 }
237
238 @Override
239 public long bytes() {
240 return bytes;
241 }
242
243 @Override
244 public void setPackets(long packets) {
245 this.packets = packets;
246 }
247
248 @Override
249 public void setBytes(long bytes) {
250 this.bytes = bytes;
251 }
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800252}