blob: f3920b23bff03e18f04bfa1cd9e8a01d81285d76 [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;
21
22import static com.google.common.base.MoreObjects.toStringHelper;
23import static com.google.common.base.Preconditions.checkArgument;
24import static com.google.common.base.Preconditions.checkNotNull;
25import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_CLASS;
26import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_PRECEDENCE;
27
28/**
29 * Default implementation of the token bucket interface.
30 */
31@Beta
32public final class DefaultTokenBucket implements TokenBucket, TokenBucketEntry {
33
34 // Immutable parameters
35 private final long rate;
36 private final long burstSize;
37 private final Action action;
38 private final short dscp;
39
40 // Mutable parameters
41 private long processedPackets;
42 private long processedBytes;
43
44 private DefaultTokenBucket(long r, long bS, Action a, short d) {
45 rate = r;
46 burstSize = bS;
47 action = a;
48 dscp = d;
49 }
50
51 @Override
52 public long rate() {
53 return rate;
54 }
55
56 @Override
57 public long burstSize() {
58 return burstSize;
59 }
60
61 @Override
62 public Action action() {
63 return action;
64 }
65
66 @Override
67 public short dscp() {
68 return dscp;
69 }
70
71 @Override
72 public long processedPackets() {
73 return processedPackets;
74 }
75
76 @Override
77 public void setProcessedPackets(long packets) {
78 processedPackets = packets;
79 }
80
81 @Override
82 public long processedBytes() {
83 return processedBytes;
84 }
85
86 @Override
87 public void setProcessedBytes(long bytes) {
88 processedBytes = bytes;
89 }
90
91 @Override
92 public String toString() {
93 return toStringHelper(this)
94 .add("rate", rate())
95 .add("burstSize", burstSize())
96 .add("action", action())
97 .add("dscp", dscp()).toString();
98 }
99
100 @Override
101 public boolean equals(Object o) {
102 if (this == o) {
103 return true;
104 }
105 if (o == null || getClass() != o.getClass()) {
106 return false;
107 }
108 DefaultTokenBucket that = (DefaultTokenBucket) o;
109 return rate == that.rate &&
110 burstSize == that.burstSize &&
111 Objects.equal(action, that.action) &&
112 dscp == that.dscp;
113 }
114
115 @Override
116 public int hashCode() {
117 return Objects.hashCode(rate, burstSize, action, dscp);
118 }
119
120 /**
121 * Returns a new builder reference.
122 *
123 * @return a new builder
124 */
125 public static Builder builder() {
126 return new Builder();
127 }
128
129 /**
130 * Implementation of the token bucket builder interface.
131 */
132 public static final class Builder implements TokenBucket.Builder {
133
134 private long rate;
135 // Default to 2 * MTU
136 private long burstSize = 2 * 1500;
137 private Action action;
138 private short dscp;
139
140 @Override
141 public TokenBucket.Builder withRate(long r) {
142 rate = r;
143 return this;
144 }
145
146 @Override
147 public TokenBucket.Builder withBurstSize(long bS) {
148 burstSize = bS;
149 return this;
150 }
151
152 @Override
153 public TokenBucket.Builder withAction(Action a) {
154 action = a;
155 return this;
156 }
157
158 @Override
159 public TokenBucket.Builder withDscp(short d) {
160 dscp = d;
161 return this;
162 }
163
164 @Override
165 public DefaultTokenBucket build() {
166 // Not null condition on the action
167 checkNotNull(action, "Must specify an action");
168
169 // If action is based on DSCP modification
170 if (action == DSCP_CLASS || action == DSCP_PRECEDENCE) {
171 // dscp should be a value between 0 and 255
172 checkArgument(dscp >= MIN_DSCP && dscp <= MAX_DSCP, "Dscp is out of range");
173 }
174
175 // Finally we build the token bucket
176 return new DefaultTokenBucket(rate, burstSize, action, dscp);
177 }
178 }
179}