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