blob: 3fab3870337fd7d847e6135e2379aca233ab353b [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 Vavilapalli0599d512015-01-30 12:57:56 -080021import org.onosproject.core.GroupId;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080022import org.onosproject.net.PortNumber;
23import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080024
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
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080068 * @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
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080083 * @return select group bucket object
84 */
85 public static GroupBucket createSelectGroupBucket(
86 TrafficTreatment treatment) {
87 return new DefaultGroupBucket(GroupDescription.Type.SELECT,
88 treatment,
89 (short) 1,
90 null,
91 null);
92 }
93
94 /**
95 * Creates select group bucket with specified weight.
96 *
97 * @param treatment traffic treatment associated with group bucket
98 * @param weight weight associated with group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080099 * @return select group bucket object
100 */
101 public static GroupBucket createSelectGroupBucket(
102 TrafficTreatment treatment,
103 short weight) {
104 if (weight == 0) {
105 return null;
106 }
107
108 return new DefaultGroupBucket(GroupDescription.Type.SELECT,
109 treatment,
110 weight,
111 null,
112 null);
113 }
114
115 /**
116 * Creates failover group bucket with watchport or watchgroup.
117 *
118 * @param treatment traffic treatment associated with group bucket
119 * @param watchPort port that determines the liveness of group bucket
Thomas Vachuska0cd4de82015-02-03 09:17:08 -0800120 * @param watchGroup group that determines the liveness of group bucket
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800121 * @return failover group bucket object
122 */
123 public static GroupBucket createFailoverGroupBucket(
124 TrafficTreatment treatment,
125 PortNumber watchPort,
126 GroupId watchGroup) {
127 checkArgument(((watchPort != null) || (watchGroup != null)));
128 return new DefaultGroupBucket(GroupDescription.Type.FAILOVER,
129 treatment,
130 (short) -1,
131 watchPort,
132 watchGroup);
133 }
134
135 @Override
136 public GroupDescription.Type type() {
137 return this.type;
138 }
139
140 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800141 * Returns list of Traffic instructions that are part of the bucket.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800142 *
143 * @return TrafficTreatment Traffic instruction list
144 */
145 @Override
146 public TrafficTreatment treatment() {
147 return treatment;
148 }
149
150 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800151 * Returns weight of select group bucket.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800152 *
153 * @return short weight associated with a bucket
154 */
155 @Override
156 public short weight() {
157 return weight;
158 }
159
160 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800161 * Returns port number used for liveness detection for a
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800162 * failover bucket.
163 *
164 * @return PortNumber port number used for liveness detection
165 */
166 @Override
167 public PortNumber watchPort() {
168 return watchPort;
169 }
170
171 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800172 * Returns group identifier used for liveness detection for a
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800173 * failover bucket.
174 *
175 * @return GroupId group identifier to be used for liveness detection
176 */
177 @Override
178 public GroupId watchGroup() {
179 return watchGroup;
180 }
181}