blob: ff1271ed9809e137d57f4071075739b066fc5e5f [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 Vavilapallied12ae52015-02-09 14:43:19 -080018import static com.google.common.base.MoreObjects.toStringHelper;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080019import static com.google.common.base.Preconditions.checkArgument;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080020import static com.google.common.base.Preconditions.checkNotNull;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080021
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080022import java.util.Objects;
23
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080024import org.onosproject.core.GroupId;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080025import org.onosproject.net.PortNumber;
26import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080027
Thomas Vachuska0cd4de82015-02-03 09:17:08 -080028/**
29 * Group bucket implementation. A group bucket is collection of
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080030 * instructions that can be performed on a traffic flow. A select
31 * Group can have one or more Buckets where traffic will be
32 * processed by a single bucket in the group, based on device
33 * specific selection algorithm (e.g. hash on some fields of the
34 * incoming traffic flows or round robin) and hence can contains
35 * optional weight field to define the weights among the buckets
36 * in the group. A failover group bucket is associated with a
37 * specific port or group that controls its liveness.
38 */
39public final class DefaultGroupBucket implements GroupBucket {
40 private final GroupDescription.Type type;
41 private final TrafficTreatment treatment;
42 private final short weight;
43 private final PortNumber watchPort;
44 private final GroupId watchGroup;
45
46 /**
47 * Group bucket constructor with the parameters.
48 *
49 * @param type group bucket type
50 * @param treatment traffic treatment associated with group bucket
51 * @param weight optional weight associated with group bucket
52 * @param watchPort port that determines the liveness of group bucket
Thomas Vachuska0cd4de82015-02-03 09:17:08 -080053 * @param watchGroup group that determines the liveness of group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080054 */
55 private DefaultGroupBucket(GroupDescription.Type type,
56 TrafficTreatment treatment,
57 short weight,
58 PortNumber watchPort,
59 GroupId watchGroup) {
60 this.type = type;
61 this.treatment = checkNotNull(treatment);
62 this.weight = weight;
63 this.watchPort = watchPort;
64 this.watchGroup = watchGroup;
65 }
66
67 /**
68 * Creates indirect group bucket.
69 *
70 * @param treatment traffic treatment associated with group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080071 * @return indirect group bucket object
72 */
73 public static GroupBucket createIndirectGroupBucket(
74 TrafficTreatment treatment) {
75 return new DefaultGroupBucket(GroupDescription.Type.INDIRECT,
76 treatment,
77 (short) -1,
78 null,
79 null);
80 }
81
82 /**
83 * Creates select group bucket with weight as 1.
84 *
85 * @param treatment traffic treatment associated with group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080086 * @return select group bucket object
87 */
88 public static GroupBucket createSelectGroupBucket(
89 TrafficTreatment treatment) {
90 return new DefaultGroupBucket(GroupDescription.Type.SELECT,
91 treatment,
92 (short) 1,
93 null,
94 null);
95 }
96
97 /**
98 * Creates select group bucket with specified weight.
99 *
100 * @param treatment traffic treatment associated with group bucket
101 * @param weight weight associated with group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800102 * @return select group bucket object
103 */
104 public static GroupBucket createSelectGroupBucket(
105 TrafficTreatment treatment,
106 short weight) {
107 if (weight == 0) {
108 return null;
109 }
110
111 return new DefaultGroupBucket(GroupDescription.Type.SELECT,
112 treatment,
113 weight,
114 null,
115 null);
116 }
117
118 /**
119 * Creates failover group bucket with watchport or watchgroup.
120 *
121 * @param treatment traffic treatment associated with group bucket
122 * @param watchPort port that determines the liveness of group bucket
Thomas Vachuska0cd4de82015-02-03 09:17:08 -0800123 * @param watchGroup group that determines the liveness of group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800124 * @return failover group bucket object
125 */
126 public static GroupBucket createFailoverGroupBucket(
127 TrafficTreatment treatment,
128 PortNumber watchPort,
129 GroupId watchGroup) {
130 checkArgument(((watchPort != null) || (watchGroup != null)));
131 return new DefaultGroupBucket(GroupDescription.Type.FAILOVER,
132 treatment,
133 (short) -1,
134 watchPort,
135 watchGroup);
136 }
137
138 @Override
139 public GroupDescription.Type type() {
140 return this.type;
141 }
142
143 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800144 * Returns list of Traffic instructions that are part of the bucket.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800145 *
146 * @return TrafficTreatment Traffic instruction list
147 */
148 @Override
149 public TrafficTreatment treatment() {
150 return treatment;
151 }
152
153 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800154 * Returns weight of select group bucket.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800155 *
156 * @return short weight associated with a bucket
157 */
158 @Override
159 public short weight() {
160 return weight;
161 }
162
163 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800164 * Returns port number used for liveness detection for a
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800165 * failover bucket.
166 *
167 * @return PortNumber port number used for liveness detection
168 */
169 @Override
170 public PortNumber watchPort() {
171 return watchPort;
172 }
173
174 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800175 * Returns group identifier used for liveness detection for a
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800176 * failover bucket.
177 *
178 * @return GroupId group identifier to be used for liveness detection
179 */
180 @Override
181 public GroupId watchGroup() {
182 return watchGroup;
183 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800184
185 /*
186 * The type and treatment can change on a given bucket
187 *
188 * (non-Javadoc)
189 * @see java.lang.Object#equals(java.lang.Object)
190 */
191 @Override
192 public int hashCode() {
193 return Objects.hash(type, treatment);
194 }
195
196 /*
197 * The priority and statistics can change on a given treatment and selector
198 *
199 * (non-Javadoc)
200 * @see java.lang.Object#equals(java.lang.Object)
201 */
202 @Override
203 public boolean equals(Object obj) {
204 if (this == obj) {
205 return true;
206 }
207 if (obj instanceof DefaultGroupBucket) {
208 DefaultGroupBucket that = (DefaultGroupBucket) obj;
209 return Objects.equals(type, that.type) &&
210 this.treatment.instructions().containsAll(that.treatment.instructions()) &&
211 that.treatment.instructions().containsAll(this.treatment.instructions());
212 }
213 return false;
214 }
215
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800216 @Override
217 public String toString() {
218 return toStringHelper(this)
219 .add("type", type)
220 .add("treatment", treatment)
221 .toString();
222 }
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800223}