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