Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.net.behaviour.trafficcontrol; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.Objects; |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 21 | import org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Type; |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 22 | |
| 23 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_CLASS; |
| 27 | import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_PRECEDENCE; |
| 28 | |
| 29 | /** |
| 30 | * Default implementation of the token bucket interface. |
| 31 | */ |
| 32 | @Beta |
| 33 | public 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 Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 40 | private final Type type; |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 41 | |
| 42 | // Mutable parameters |
| 43 | private long processedPackets; |
| 44 | private long processedBytes; |
| 45 | |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 46 | private DefaultTokenBucket(long r, long bS, Action a, short d, Type t) { |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 47 | rate = r; |
| 48 | burstSize = bS; |
| 49 | action = a; |
| 50 | dscp = d; |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 51 | type = t; |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 52 | } |
| 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 Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 75 | public Type type() { |
| 76 | return type; |
| 77 | } |
| 78 | |
| 79 | @Override |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 80 | 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 Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 105 | .add("dscp", dscp()) |
| 106 | .add("type", type()).toString(); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 107 | } |
| 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 Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 121 | dscp == that.dscp && |
| 122 | Objects.equal(type, that.type); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public int hashCode() { |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 127 | return Objects.hashCode(rate, burstSize, action, dscp, type); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 128 | } |
| 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 |
Ray Milkey | 3717e60 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 146 | private long burstSize = 2L * 1500L; |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 147 | private Action action; |
| 148 | private short dscp; |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 149 | private Type type; |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 150 | |
| 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 Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 176 | public TokenBucket.Builder withType(Type t) { |
| 177 | type = t; |
| 178 | return this; |
| 179 | } |
| 180 | |
| 181 | @Override |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 182 | public DefaultTokenBucket build() { |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 183 | // Not null condition on the action and on the type |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 184 | checkNotNull(action, "Must specify an action"); |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 185 | checkNotNull(type, "Must specify a type"); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 186 | |
| 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 Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 194 | return new DefaultTokenBucket(rate, burstSize, action, dscp, type); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | } |