blob: e4910d523ab53e5feb00064d57ca5b240e2ad955 [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 */
41public final class DefaultGroupBucket implements GroupBucket {
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;
47
48 /**
49 * Group bucket constructor with the parameters.
50 *
51 * @param type group bucket type
52 * @param treatment traffic treatment associated with group bucket
53 * @param weight optional weight associated with group bucket
54 * @param watchPort port that determines the liveness of group bucket
Thomas Vachuska0cd4de82015-02-03 09:17:08 -080055 * @param watchGroup group that determines the liveness of group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080056 */
57 private DefaultGroupBucket(GroupDescription.Type type,
58 TrafficTreatment treatment,
59 short weight,
60 PortNumber watchPort,
61 GroupId watchGroup) {
62 this.type = type;
63 this.treatment = checkNotNull(treatment);
64 this.weight = weight;
65 this.watchPort = watchPort;
66 this.watchGroup = watchGroup;
67 }
68
69 /**
70 * Creates indirect group bucket.
71 *
72 * @param treatment traffic treatment associated with group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080073 * @return indirect group bucket object
74 */
75 public static GroupBucket createIndirectGroupBucket(
76 TrafficTreatment treatment) {
77 return new DefaultGroupBucket(GroupDescription.Type.INDIRECT,
78 treatment,
79 (short) -1,
80 null,
81 null);
82 }
83
84 /**
85 * Creates select group bucket with weight as 1.
86 *
87 * @param treatment traffic treatment associated with group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080088 * @return select group bucket object
89 */
90 public static GroupBucket createSelectGroupBucket(
91 TrafficTreatment treatment) {
92 return new DefaultGroupBucket(GroupDescription.Type.SELECT,
93 treatment,
94 (short) 1,
95 null,
96 null);
97 }
98
99 /**
100 * Creates select group bucket with specified weight.
101 *
102 * @param treatment traffic treatment associated with group bucket
103 * @param weight weight associated with group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800104 * @return select group bucket object
105 */
106 public static GroupBucket createSelectGroupBucket(
107 TrafficTreatment treatment,
108 short weight) {
109 if (weight == 0) {
110 return null;
111 }
112
113 return new DefaultGroupBucket(GroupDescription.Type.SELECT,
114 treatment,
115 weight,
116 null,
117 null);
118 }
119
120 /**
121 * Creates failover group bucket with watchport or watchgroup.
122 *
123 * @param treatment traffic treatment associated with group bucket
124 * @param watchPort port that determines the liveness of group bucket
Thomas Vachuska0cd4de82015-02-03 09:17:08 -0800125 * @param watchGroup group that determines the liveness of group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800126 * @return failover group bucket object
127 */
128 public static GroupBucket createFailoverGroupBucket(
129 TrafficTreatment treatment,
130 PortNumber watchPort,
131 GroupId watchGroup) {
132 checkArgument(((watchPort != null) || (watchGroup != null)));
133 return new DefaultGroupBucket(GroupDescription.Type.FAILOVER,
134 treatment,
135 (short) -1,
136 watchPort,
137 watchGroup);
138 }
139
140 @Override
141 public GroupDescription.Type type() {
142 return this.type;
143 }
144
145 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800146 * Returns list of Traffic instructions that are part of the bucket.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800147 *
148 * @return TrafficTreatment Traffic instruction list
149 */
150 @Override
151 public TrafficTreatment treatment() {
152 return treatment;
153 }
154
155 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800156 * Returns weight of select group bucket.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800157 *
158 * @return short weight associated with a bucket
159 */
160 @Override
161 public short weight() {
162 return weight;
163 }
164
165 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800166 * Returns port number used for liveness detection for a
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800167 * failover bucket.
168 *
169 * @return PortNumber port number used for liveness detection
170 */
171 @Override
172 public PortNumber watchPort() {
173 return watchPort;
174 }
175
176 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800177 * Returns group identifier used for liveness detection for a
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800178 * failover bucket.
179 *
180 * @return GroupId group identifier to be used for liveness detection
181 */
182 @Override
183 public GroupId watchGroup() {
184 return watchGroup;
185 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800186
187 /*
188 * The type and treatment can change on a given bucket
189 *
190 * (non-Javadoc)
191 * @see java.lang.Object#equals(java.lang.Object)
192 */
193 @Override
194 public int hashCode() {
195 return Objects.hash(type, treatment);
196 }
197
198 /*
199 * The priority and statistics can change on a given treatment and selector
200 *
201 * (non-Javadoc)
202 * @see java.lang.Object#equals(java.lang.Object)
203 */
204 @Override
205 public boolean equals(Object obj) {
206 if (this == obj) {
207 return true;
208 }
209 if (obj instanceof DefaultGroupBucket) {
210 DefaultGroupBucket that = (DefaultGroupBucket) obj;
Ray Milkey42507352015-03-20 15:16:10 -0700211 List<Instruction> myInstructions = this.treatment.allInstructions();
212 List<Instruction> theirInstructions = that.treatment.allInstructions();
213
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800214 return Objects.equals(type, that.type) &&
Ray Milkey42507352015-03-20 15:16:10 -0700215 myInstructions.containsAll(theirInstructions) &&
216 theirInstructions.containsAll(myInstructions);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800217 }
218 return false;
219 }
220
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800221 @Override
222 public String toString() {
223 return toStringHelper(this)
224 .add("type", type)
225 .add("treatment", treatment)
226 .toString();
227 }
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800228}