blob: 8313f3f66b2812a5e2ee4a9a237a8df947a065a9 [file] [log] [blame]
Pier Luigi09220c22017-09-14 22:00:30 +02001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16
17package org.onosproject.net.behaviour.trafficcontrol;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.Objects;
Pier Luigi256d92b2017-11-15 13:22:15 +010021import org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Type;
Pier Luigi09220c22017-09-14 22:00:30 +020022
23import static com.google.common.base.MoreObjects.toStringHelper;
24import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
26import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_CLASS;
27import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_PRECEDENCE;
28
29/**
30 * Default implementation of the token bucket interface.
31 */
32@Beta
33public final class DefaultTokenBucket implements TokenBucket, TokenBucketEntry {
34
35 // Immutable parameters
36 private final long rate;
37 private final long burstSize;
38 private final Action action;
39 private final short dscp;
Pier Luigi256d92b2017-11-15 13:22:15 +010040 private final Type type;
Pier Luigi09220c22017-09-14 22:00:30 +020041
42 // Mutable parameters
43 private long processedPackets;
44 private long processedBytes;
45
Pier Luigi256d92b2017-11-15 13:22:15 +010046 private DefaultTokenBucket(long r, long bS, Action a, short d, Type t) {
Pier Luigi09220c22017-09-14 22:00:30 +020047 rate = r;
48 burstSize = bS;
49 action = a;
50 dscp = d;
Pier Luigi256d92b2017-11-15 13:22:15 +010051 type = t;
Pier Luigi09220c22017-09-14 22:00:30 +020052 }
53
54 @Override
55 public long rate() {
56 return rate;
57 }
58
59 @Override
60 public long burstSize() {
61 return burstSize;
62 }
63
64 @Override
65 public Action action() {
66 return action;
67 }
68
69 @Override
70 public short dscp() {
71 return dscp;
72 }
73
74 @Override
Pier Luigi256d92b2017-11-15 13:22:15 +010075 public Type type() {
76 return type;
77 }
78
79 @Override
Pier Luigi09220c22017-09-14 22:00:30 +020080 public long processedPackets() {
81 return processedPackets;
82 }
83
84 @Override
85 public void setProcessedPackets(long packets) {
86 processedPackets = packets;
87 }
88
89 @Override
90 public long processedBytes() {
91 return processedBytes;
92 }
93
94 @Override
95 public void setProcessedBytes(long bytes) {
96 processedBytes = bytes;
97 }
98
99 @Override
100 public String toString() {
101 return toStringHelper(this)
102 .add("rate", rate())
103 .add("burstSize", burstSize())
104 .add("action", action())
Pier Luigi256d92b2017-11-15 13:22:15 +0100105 .add("dscp", dscp())
106 .add("type", type()).toString();
Pier Luigi09220c22017-09-14 22:00:30 +0200107 }
108
109 @Override
110 public boolean equals(Object o) {
111 if (this == o) {
112 return true;
113 }
114 if (o == null || getClass() != o.getClass()) {
115 return false;
116 }
117 DefaultTokenBucket that = (DefaultTokenBucket) o;
118 return rate == that.rate &&
119 burstSize == that.burstSize &&
120 Objects.equal(action, that.action) &&
Pier Luigi256d92b2017-11-15 13:22:15 +0100121 dscp == that.dscp &&
122 Objects.equal(type, that.type);
Pier Luigi09220c22017-09-14 22:00:30 +0200123 }
124
125 @Override
126 public int hashCode() {
Pier Luigi256d92b2017-11-15 13:22:15 +0100127 return Objects.hashCode(rate, burstSize, action, dscp, type);
Pier Luigi09220c22017-09-14 22:00:30 +0200128 }
129
130 /**
131 * Returns a new builder reference.
132 *
133 * @return a new builder
134 */
135 public static Builder builder() {
136 return new Builder();
137 }
138
139 /**
140 * Implementation of the token bucket builder interface.
141 */
142 public static final class Builder implements TokenBucket.Builder {
143
144 private long rate;
145 // Default to 2 * MTU
146 private long burstSize = 2 * 1500;
147 private Action action;
148 private short dscp;
Pier Luigi256d92b2017-11-15 13:22:15 +0100149 private Type type;
Pier Luigi09220c22017-09-14 22:00:30 +0200150
151 @Override
152 public TokenBucket.Builder withRate(long r) {
153 rate = r;
154 return this;
155 }
156
157 @Override
158 public TokenBucket.Builder withBurstSize(long bS) {
159 burstSize = bS;
160 return this;
161 }
162
163 @Override
164 public TokenBucket.Builder withAction(Action a) {
165 action = a;
166 return this;
167 }
168
169 @Override
170 public TokenBucket.Builder withDscp(short d) {
171 dscp = d;
172 return this;
173 }
174
175 @Override
Pier Luigi256d92b2017-11-15 13:22:15 +0100176 public TokenBucket.Builder withType(Type t) {
177 type = t;
178 return this;
179 }
180
181 @Override
Pier Luigi09220c22017-09-14 22:00:30 +0200182 public DefaultTokenBucket build() {
Pier Luigi256d92b2017-11-15 13:22:15 +0100183 // Not null condition on the action and on the type
Pier Luigi09220c22017-09-14 22:00:30 +0200184 checkNotNull(action, "Must specify an action");
Pier Luigi256d92b2017-11-15 13:22:15 +0100185 checkNotNull(type, "Must specify a type");
Pier Luigi09220c22017-09-14 22:00:30 +0200186
187 // If action is based on DSCP modification
188 if (action == DSCP_CLASS || action == DSCP_PRECEDENCE) {
189 // dscp should be a value between 0 and 255
190 checkArgument(dscp >= MIN_DSCP && dscp <= MAX_DSCP, "Dscp is out of range");
191 }
192
193 // Finally we build the token bucket
Pier Luigi256d92b2017-11-15 13:22:15 +0100194 return new DefaultTokenBucket(rate, burstSize, action, dscp, type);
Pier Luigi09220c22017-09-14 22:00:30 +0200195 }
196 }
197}