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; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | |
| 25 | import java.util.Collection; |
| 26 | |
| 27 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 28 | import static com.google.common.base.Preconditions.checkArgument; |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * Default implementation of the policer interface. |
| 33 | */ |
| 34 | @Beta |
| 35 | public final class DefaultPolicer implements Policer, PolicerEntry { |
| 36 | |
| 37 | // Immutable parameters |
| 38 | private final DeviceId deviceId; |
| 39 | private final ApplicationId applicationId; |
| 40 | private final PolicerId policerId; |
| 41 | private final boolean colorAware; |
| 42 | private final Unit unit; |
| 43 | private final Collection<TokenBucket> tokenBuckets; |
| 44 | private final String description; |
| 45 | |
| 46 | // Mutable parameters |
| 47 | private long referenceCount; |
| 48 | private long processedPackets; |
| 49 | private long processedBytes; |
| 50 | private long life; |
| 51 | |
| 52 | private DefaultPolicer(DeviceId dId, ApplicationId aId, PolicerId pId, |
| 53 | boolean cA, Unit u, Collection<TokenBucket> tB, |
| 54 | String d) { |
| 55 | deviceId = dId; |
| 56 | applicationId = aId; |
| 57 | policerId = pId; |
| 58 | colorAware = cA; |
| 59 | unit = u; |
| 60 | tokenBuckets = tB; |
| 61 | description = d; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public DeviceId deviceId() { |
| 66 | return deviceId; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public ApplicationId applicationId() { |
| 71 | return applicationId; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public PolicerId policerId() { |
| 76 | return policerId; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean isColorAware() { |
| 81 | return colorAware; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Unit unit() { |
| 86 | return unit; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Collection<TokenBucket> tokenBuckets() { |
| 91 | return tokenBuckets; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public String description() { |
| 96 | return description; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public long referenceCount() { |
| 101 | return referenceCount; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void setReferenceCount(long count) { |
| 106 | referenceCount = count; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public long processedPackets() { |
| 111 | return processedPackets; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void setProcessedPackets(long packets) { |
| 116 | processedPackets = packets; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public long processedBytes() { |
| 121 | return processedBytes; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void setProcessedBytes(long bytes) { |
| 126 | processedBytes = bytes; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public long life() { |
| 131 | return life; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void setLife(long l) { |
| 136 | life = l; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public String toString() { |
| 141 | return toStringHelper(this) |
| 142 | .add("appId", applicationId()) |
| 143 | .add("id", policerId()) |
| 144 | .add("isColorAware", isColorAware()) |
| 145 | .add("unit", unit()) |
| 146 | .add("tokenBuckets", tokenBuckets()) |
| 147 | .add("description", description()) |
| 148 | .toString(); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public boolean equals(Object o) { |
| 153 | if (this == o) { |
| 154 | return true; |
| 155 | } |
| 156 | if (o == null || getClass() != o.getClass()) { |
| 157 | return false; |
| 158 | } |
| 159 | DefaultPolicer that = (DefaultPolicer) o; |
| 160 | return Objects.equal(policerId, that.policerId); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public int hashCode() { |
| 165 | return policerId.hashCode(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns a new builder reference. |
| 170 | * |
| 171 | * @return a new builder |
| 172 | */ |
| 173 | public static Builder builder() { |
| 174 | return new Builder(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Implementation of the policer builder interface. |
| 179 | */ |
| 180 | public static final class Builder implements Policer.Builder { |
| 181 | |
| 182 | private DeviceId deviceId; |
| 183 | private ApplicationId applicationId; |
| 184 | private PolicerId policerId; |
| 185 | // Default to unaware |
| 186 | private boolean colorAware = false; |
| 187 | // Default to MBps |
| 188 | private Unit unit = Unit.MB_PER_SEC; |
| 189 | private Collection<TokenBucket> tokenBuckets; |
| 190 | private String description = ""; |
| 191 | |
| 192 | @Override |
| 193 | public Policer.Builder forDeviceId(DeviceId dId) { |
| 194 | deviceId = dId; |
| 195 | return this; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public Policer.Builder fromApp(ApplicationId appId) { |
| 200 | applicationId = appId; |
| 201 | return this; |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public Policer.Builder withId(PolicerId id) { |
| 206 | policerId = id; |
| 207 | return this; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public Policer.Builder colorAware(boolean isColorAware) { |
| 212 | colorAware = isColorAware; |
| 213 | return this; |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public Policer.Builder withUnit(Unit u) { |
| 218 | unit = u; |
| 219 | return this; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public Policer.Builder withPolicingResource(PolicingResource policingResource) { |
| 224 | policerId = policingResource.policerId(); |
| 225 | deviceId = policingResource.connectPoint().deviceId(); |
| 226 | return this; |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public Policer.Builder withTokenBuckets(Collection<TokenBucket> tB) { |
| 231 | tokenBuckets = ImmutableSet.copyOf(tB); |
| 232 | return this; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public Policer.Builder withDescription(String d) { |
| 237 | description = d; |
| 238 | return this; |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public DefaultPolicer build() { |
| 243 | // Not null condition on some mandatory parameters |
| 244 | checkNotNull(deviceId, "Must specify a deviceId"); |
| 245 | checkNotNull(applicationId, "Must specify an application id"); |
| 246 | checkNotNull(policerId, "Must specify a policer id"); |
| 247 | checkNotNull(unit, "Must specify a unit for the policer"); |
| 248 | checkNotNull(tokenBuckets, "Must have token buckets"); |
| 249 | checkNotNull(description, "Must have a description"); |
| 250 | |
| 251 | // Verify argument conditions |
| 252 | checkArgument(!tokenBuckets.isEmpty(), "Must have at least one token bucket"); |
| 253 | |
| 254 | // Finally we build the policer |
| 255 | return new DefaultPolicer(deviceId, applicationId, policerId, |
| 256 | colorAware, unit, tokenBuckets, |
| 257 | description); |
| 258 | } |
| 259 | } |
| 260 | } |