alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 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 | */ |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 16 | package org.onosproject.net.meter; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 17 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 18 | import com.google.common.base.Objects; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | |
| 23 | import java.util.Collection; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 24 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * A default implementation of a meter. |
| 31 | */ |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 32 | public final class DefaultMeter implements Meter, MeterEntry { |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 33 | |
| 34 | |
| 35 | private final MeterId id; |
| 36 | private final ApplicationId appId; |
| 37 | private final Unit unit; |
| 38 | private final boolean burst; |
| 39 | private final Collection<Band> bands; |
| 40 | private final DeviceId deviceId; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 41 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 42 | private MeterState state; |
| 43 | private long life; |
| 44 | private long refCount; |
| 45 | private long packets; |
| 46 | private long bytes; |
| 47 | |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 48 | private DefaultMeter(DeviceId deviceId, MeterId id, ApplicationId appId, |
| 49 | Unit unit, boolean burst, |
alshabib | eadfc8e | 2015-08-18 15:40:46 -0700 | [diff] [blame] | 50 | Collection<Band> bands) { |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 51 | this.deviceId = deviceId; |
| 52 | this.id = id; |
| 53 | this.appId = appId; |
| 54 | this.unit = unit; |
| 55 | this.burst = burst; |
| 56 | this.bands = bands; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public DeviceId deviceId() { |
| 61 | return deviceId; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public MeterId id() { |
| 66 | return id; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public ApplicationId appId() { |
| 71 | return appId; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public Unit unit() { |
| 76 | return unit; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean isBurst() { |
| 81 | return burst; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Collection<Band> bands() { |
| 86 | return bands; |
| 87 | } |
| 88 | |
| 89 | @Override |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 90 | public MeterState state() { |
| 91 | return state; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public long life() { |
| 96 | return life; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public long referenceCount() { |
| 101 | return refCount; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public long packetsSeen() { |
| 106 | return packets; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public long bytesSeen() { |
| 111 | return bytes; |
| 112 | } |
| 113 | |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 114 | public static Builder builder() { |
| 115 | return new Builder(); |
| 116 | } |
| 117 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 118 | @Override |
| 119 | public void setState(MeterState state) { |
| 120 | this.state = state; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void setLife(long life) { |
| 125 | this.life = life; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void setReferenceCount(long count) { |
| 130 | this.refCount = count; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void setProcessedPackets(long packets) { |
| 135 | this.packets = packets; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void setProcessedBytes(long bytes) { |
| 140 | this.bytes = bytes; |
| 141 | } |
| 142 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 143 | @Override |
| 144 | public String toString() { |
| 145 | return toStringHelper(this) |
| 146 | .add("device", deviceId) |
| 147 | .add("id", id) |
| 148 | .add("appId", appId.name()) |
| 149 | .add("unit", unit) |
| 150 | .add("isBurst", burst) |
| 151 | .add("state", state) |
| 152 | .add("bands", bands).toString(); |
| 153 | } |
| 154 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public boolean equals(Object o) { |
| 157 | if (this == o) { |
| 158 | return true; |
| 159 | } |
| 160 | if (o == null || getClass() != o.getClass()) { |
| 161 | return false; |
| 162 | } |
| 163 | DefaultMeter that = (DefaultMeter) o; |
| 164 | return Objects.equal(id, that.id) && |
| 165 | Objects.equal(appId, that.appId) && |
| 166 | Objects.equal(unit, that.unit) && |
| 167 | Objects.equal(deviceId, that.deviceId); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public int hashCode() { |
| 172 | return Objects.hashCode(id, appId, unit, deviceId); |
| 173 | } |
| 174 | |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 175 | public static final class Builder implements Meter.Builder { |
| 176 | |
| 177 | private MeterId id; |
| 178 | private ApplicationId appId; |
| 179 | private Unit unit = Unit.KB_PER_SEC; |
| 180 | private boolean burst = false; |
| 181 | private Collection<Band> bands; |
| 182 | private DeviceId deviceId; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 183 | |
| 184 | |
| 185 | @Override |
| 186 | public Meter.Builder forDevice(DeviceId deviceId) { |
| 187 | this.deviceId = deviceId; |
| 188 | return this; |
| 189 | } |
| 190 | |
| 191 | @Override |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 192 | public Meter.Builder withId(MeterId id) { |
| 193 | this.id = id; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 194 | return this; |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public Meter.Builder fromApp(ApplicationId appId) { |
| 199 | this.appId = appId; |
| 200 | return this; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public Meter.Builder withUnit(Unit unit) { |
| 205 | this.unit = unit; |
| 206 | return this; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public Meter.Builder burst() { |
| 211 | this.burst = true; |
| 212 | return this; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public Meter.Builder withBands(Collection<Band> bands) { |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 217 | this.bands = ImmutableSet.copyOf(bands); |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 218 | return this; |
| 219 | } |
| 220 | |
| 221 | @Override |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 222 | public DefaultMeter build() { |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 223 | checkNotNull(deviceId, "Must specify a device"); |
| 224 | checkNotNull(bands, "Must have bands."); |
| 225 | checkArgument(bands.size() > 0, "Must have at least one band."); |
| 226 | checkNotNull(appId, "Must have an application id"); |
| 227 | checkNotNull(id, "Must specify a meter id"); |
alshabib | eadfc8e | 2015-08-18 15:40:46 -0700 | [diff] [blame] | 228 | return new DefaultMeter(deviceId, id, appId, unit, burst, bands); |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
| 232 | } |
| 233 | } |