blob: 8c6eb63f9a4fcd0b3d7d67023a88a3aa0eb375e2 [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
18import static com.google.common.base.Preconditions.checkNotNull;
19import static com.google.common.base.Preconditions.checkArgument;
20
21import org.onosproject.net.PortNumber;
22import org.onosproject.net.flow.TrafficTreatment;
23import org.onosproject.core.GroupId;
24
Thomas Vachuska0cd4de82015-02-03 09:17:08 -080025/**
26 * Group bucket implementation. A group bucket is collection of
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080027 * instructions that can be performed on a traffic flow. A select
28 * Group can have one or more Buckets where traffic will be
29 * processed by a single bucket in the group, based on device
30 * specific selection algorithm (e.g. hash on some fields of the
31 * incoming traffic flows or round robin) and hence can contains
32 * optional weight field to define the weights among the buckets
33 * in the group. A failover group bucket is associated with a
34 * specific port or group that controls its liveness.
35 */
36public final class DefaultGroupBucket implements GroupBucket {
37 private final GroupDescription.Type type;
38 private final TrafficTreatment treatment;
39 private final short weight;
40 private final PortNumber watchPort;
41 private final GroupId watchGroup;
42
43 /**
44 * Group bucket constructor with the parameters.
45 *
46 * @param type group bucket type
47 * @param treatment traffic treatment associated with group bucket
48 * @param weight optional weight associated with group bucket
49 * @param watchPort port that determines the liveness of group bucket
Thomas Vachuska0cd4de82015-02-03 09:17:08 -080050 * @param watchGroup group that determines the liveness of group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080051 */
52 private DefaultGroupBucket(GroupDescription.Type type,
53 TrafficTreatment treatment,
54 short weight,
55 PortNumber watchPort,
56 GroupId watchGroup) {
57 this.type = type;
58 this.treatment = checkNotNull(treatment);
59 this.weight = weight;
60 this.watchPort = watchPort;
61 this.watchGroup = watchGroup;
62 }
63
64 /**
65 * Creates indirect group bucket.
66 *
67 * @param treatment traffic treatment associated with group bucket
68 *
69 * @return indirect group bucket object
70 */
71 public static GroupBucket createIndirectGroupBucket(
72 TrafficTreatment treatment) {
73 return new DefaultGroupBucket(GroupDescription.Type.INDIRECT,
74 treatment,
75 (short) -1,
76 null,
77 null);
78 }
79
80 /**
81 * Creates select group bucket with weight as 1.
82 *
83 * @param treatment traffic treatment associated with group bucket
84 *
85 * @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
101 *
102 * @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 *
125 * @return failover group bucket object
126 */
127 public static GroupBucket createFailoverGroupBucket(
128 TrafficTreatment treatment,
129 PortNumber watchPort,
130 GroupId watchGroup) {
131 checkArgument(((watchPort != null) || (watchGroup != null)));
132 return new DefaultGroupBucket(GroupDescription.Type.FAILOVER,
133 treatment,
134 (short) -1,
135 watchPort,
136 watchGroup);
137 }
138
139 @Override
140 public GroupDescription.Type type() {
141 return this.type;
142 }
143
144 /**
145 * Return list of Traffic instructions that are part of the bucket.
146 *
147 * @return TrafficTreatment Traffic instruction list
148 */
149 @Override
150 public TrafficTreatment treatment() {
151 return treatment;
152 }
153
154 /**
155 * Return weight of select group bucket.
156 *
157 * @return short weight associated with a bucket
158 */
159 @Override
160 public short weight() {
161 return weight;
162 }
163
164 /**
165 * Return port number used for liveness detection for a
166 * failover bucket.
167 *
168 * @return PortNumber port number used for liveness detection
169 */
170 @Override
171 public PortNumber watchPort() {
172 return watchPort;
173 }
174
175 /**
176 * Return group identifier used for liveness detection for a
177 * failover bucket.
178 *
179 * @return GroupId group identifier to be used for liveness detection
180 */
181 @Override
182 public GroupId watchGroup() {
183 return watchGroup;
184 }
185}