blob: 693f957e5d3f4fd5e3b8c9c74ae622fd50c86184 [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
25/* Group bucket implementation. A group bucket is collection of
26 * instructions that can be performed on a traffic flow. A select
27 * Group can have one or more Buckets where traffic will be
28 * processed by a single bucket in the group, based on device
29 * specific selection algorithm (e.g. hash on some fields of the
30 * incoming traffic flows or round robin) and hence can contains
31 * optional weight field to define the weights among the buckets
32 * in the group. A failover group bucket is associated with a
33 * specific port or group that controls its liveness.
34 */
35public final class DefaultGroupBucket implements GroupBucket {
36 private final GroupDescription.Type type;
37 private final TrafficTreatment treatment;
38 private final short weight;
39 private final PortNumber watchPort;
40 private final GroupId watchGroup;
41
42 /**
43 * Group bucket constructor with the parameters.
44 *
45 * @param type group bucket type
46 * @param treatment traffic treatment associated with group bucket
47 * @param weight optional weight associated with group bucket
48 * @param watchPort port that determines the liveness of group bucket
49 * @param watchPort group that determines the liveness of group bucket
50 */
51 private DefaultGroupBucket(GroupDescription.Type type,
52 TrafficTreatment treatment,
53 short weight,
54 PortNumber watchPort,
55 GroupId watchGroup) {
56 this.type = type;
57 this.treatment = checkNotNull(treatment);
58 this.weight = weight;
59 this.watchPort = watchPort;
60 this.watchGroup = watchGroup;
61 }
62
63 /**
64 * Creates indirect group bucket.
65 *
66 * @param treatment traffic treatment associated with group bucket
67 *
68 * @return indirect group bucket object
69 */
70 public static GroupBucket createIndirectGroupBucket(
71 TrafficTreatment treatment) {
72 return new DefaultGroupBucket(GroupDescription.Type.INDIRECT,
73 treatment,
74 (short) -1,
75 null,
76 null);
77 }
78
79 /**
80 * Creates select group bucket with weight as 1.
81 *
82 * @param treatment traffic treatment associated with group bucket
83 *
84 * @return select group bucket object
85 */
86 public static GroupBucket createSelectGroupBucket(
87 TrafficTreatment treatment) {
88 return new DefaultGroupBucket(GroupDescription.Type.SELECT,
89 treatment,
90 (short) 1,
91 null,
92 null);
93 }
94
95 /**
96 * Creates select group bucket with specified weight.
97 *
98 * @param treatment traffic treatment associated with group bucket
99 * @param weight weight associated with group bucket
100 *
101 * @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
122 * @param watchPort group that determines the liveness of group bucket
123 *
124 * @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 /**
144 * Return list of Traffic instructions that are part of the bucket.
145 *
146 * @return TrafficTreatment Traffic instruction list
147 */
148 @Override
149 public TrafficTreatment treatment() {
150 return treatment;
151 }
152
153 /**
154 * Return weight of select group bucket.
155 *
156 * @return short weight associated with a bucket
157 */
158 @Override
159 public short weight() {
160 return weight;
161 }
162
163 /**
164 * Return port number used for liveness detection for a
165 * 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 /**
175 * Return group identifier used for liveness detection for a
176 * 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 }
184}