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