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