blob: ef8b3df73d64f0a7027b8a910e730e40a19f2f2b [file] [log] [blame]
alshabib1d2bc402015-07-31 17:04:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
alshabib1d2bc402015-07-31 17:04:11 -07003 *
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 */
alshabib10c810b2015-08-18 16:59:04 -070016package org.onosproject.net.meter;
alshabib1d2bc402015-07-31 17:04:11 -070017
alshabibe1248b62015-08-20 17:21:55 -070018import com.google.common.base.Objects;
alshabib58fe6dc2015-08-19 17:16:13 -070019import com.google.common.collect.ImmutableSet;
alshabib1d2bc402015-07-31 17:04:11 -070020import org.onosproject.core.ApplicationId;
Andrea Campanella5bdbe432021-05-03 15:59:19 +020021import org.onosproject.net.AbstractAnnotated;
22import org.onosproject.net.Annotations;
alshabib1d2bc402015-07-31 17:04:11 -070023import org.onosproject.net.DeviceId;
24
25import java.util.Collection;
Wailok Shum79919522021-08-22 19:35:34 +080026import java.util.Optional;
alshabib1d2bc402015-07-31 17:04:11 -070027
alshabib58fe6dc2015-08-19 17:16:13 -070028import static com.google.common.base.MoreObjects.toStringHelper;
alshabib1d2bc402015-07-31 17:04:11 -070029import static com.google.common.base.Preconditions.checkArgument;
30import static com.google.common.base.Preconditions.checkNotNull;
Frank Wangd7e3b4b2017-09-24 13:37:54 +090031import static org.onosproject.net.meter.MeterCellId.MeterCellType.INDEX;
alshabib1d2bc402015-07-31 17:04:11 -070032
33/**
34 * A default implementation of a meter.
35 */
Andrea Campanella5bdbe432021-05-03 15:59:19 +020036public final class DefaultMeter extends AbstractAnnotated implements Meter, MeterEntry {
alshabib1d2bc402015-07-31 17:04:11 -070037
Frank Wangd7e3b4b2017-09-24 13:37:54 +090038 private final MeterCellId cellId;
Wailok Shum79919522021-08-22 19:35:34 +080039 private final Optional<ApplicationId> appId;
alshabib1d2bc402015-07-31 17:04:11 -070040 private final Unit unit;
41 private final boolean burst;
42 private final Collection<Band> bands;
43 private final DeviceId deviceId;
alshabib1d2bc402015-07-31 17:04:11 -070044
alshabib7bb05012015-08-05 10:15:09 -070045 private MeterState state;
46 private long life;
47 private long refCount;
48 private long packets;
49 private long bytes;
50
Wailok Shum79919522021-08-22 19:35:34 +080051 private DefaultMeter(DeviceId deviceId, MeterCellId cellId,
52 Optional<ApplicationId> appId, Unit unit,
53 boolean burst, Collection<Band> bands,
Andrea Campanella5bdbe432021-05-03 15:59:19 +020054 Annotations... annotations) {
55 super(annotations);
alshabib1d2bc402015-07-31 17:04:11 -070056 this.deviceId = deviceId;
Frank Wangd7e3b4b2017-09-24 13:37:54 +090057 this.cellId = cellId;
alshabib1d2bc402015-07-31 17:04:11 -070058 this.appId = appId;
59 this.unit = unit;
60 this.burst = burst;
61 this.bands = bands;
alshabib1d2bc402015-07-31 17:04:11 -070062 }
63
64 @Override
65 public DeviceId deviceId() {
66 return deviceId;
67 }
68
69 @Override
70 public MeterId id() {
Frank Wangd7e3b4b2017-09-24 13:37:54 +090071 // 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;
alshabib1d2bc402015-07-31 17:04:11 -070081 }
82
83 @Override
84 public ApplicationId appId() {
Wailok Shum79919522021-08-22 19:35:34 +080085 return appId.orElse(null);
86 // TODO: Deprecate this API because AppId becomes optional in Meter
alshabib1d2bc402015-07-31 17:04:11 -070087 }
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
alshabib7bb05012015-08-05 10:15:09 -0700105 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
alshabib1d2bc402015-07-31 17:04:11 -0700129 public static Builder builder() {
130 return new Builder();
131 }
132
alshabib7bb05012015-08-05 10:15:09 -0700133 @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
alshabib58fe6dc2015-08-19 17:16:13 -0700158 @Override
159 public String toString() {
160 return toStringHelper(this)
161 .add("device", deviceId)
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900162 .add("cellId", cellId)
Wailok Shum79919522021-08-22 19:35:34 +0800163 .add("appId", appId.orElse(null))
alshabib58fe6dc2015-08-19 17:16:13 -0700164 .add("unit", unit)
165 .add("isBurst", burst)
166 .add("state", state)
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200167 .add("bands", bands)
168 .add("annotations", annotations())
169 .toString();
alshabib58fe6dc2015-08-19 17:16:13 -0700170 }
171
alshabibe1248b62015-08-20 17:21:55 -0700172 @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 Wangd7e3b4b2017-09-24 13:37:54 +0900181 return Objects.equal(cellId, that.cellId) &&
alshabibe1248b62015-08-20 17:21:55 -0700182 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 Wangd7e3b4b2017-09-24 13:37:54 +0900189 return Objects.hashCode(cellId, appId, unit, deviceId);
alshabibe1248b62015-08-20 17:21:55 -0700190 }
191
alshabib1d2bc402015-07-31 17:04:11 -0700192 public static final class Builder implements Meter.Builder {
193
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900194 private MeterCellId cellId;
Wailok Shum79919522021-08-22 19:35:34 +0800195 private Optional<ApplicationId> appId = Optional.empty();
alshabib1d2bc402015-07-31 17:04:11 -0700196 private Unit unit = Unit.KB_PER_SEC;
197 private boolean burst = false;
198 private Collection<Band> bands;
199 private DeviceId deviceId;
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200200 private Annotations annotations;
alshabib1d2bc402015-07-31 17:04:11 -0700201
alshabib1d2bc402015-07-31 17:04:11 -0700202 @Override
203 public Meter.Builder forDevice(DeviceId deviceId) {
204 this.deviceId = deviceId;
205 return this;
206 }
207
208 @Override
alshabib58fe6dc2015-08-19 17:16:13 -0700209 public Meter.Builder withId(MeterId id) {
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900210 this.withCellId(id);
211 return this;
212 }
213
214 @Override
215 public Meter.Builder withCellId(MeterCellId cellId) {
216 this.cellId = cellId;
alshabib1d2bc402015-07-31 17:04:11 -0700217 return this;
218 }
219
220 @Override
221 public Meter.Builder fromApp(ApplicationId appId) {
Wailok Shum79919522021-08-22 19:35:34 +0800222 this.appId = Optional.ofNullable(appId);
alshabib1d2bc402015-07-31 17:04:11 -0700223 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) {
alshabib58fe6dc2015-08-19 17:16:13 -0700240 this.bands = ImmutableSet.copyOf(bands);
alshabib1d2bc402015-07-31 17:04:11 -0700241 return this;
242 }
243
244 @Override
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200245 public Builder withAnnotations(Annotations anns) {
246 this.annotations = anns;
247 return this;
248 }
249
250 @Override
alshabib7bb05012015-08-05 10:15:09 -0700251 public DefaultMeter build() {
alshabib1d2bc402015-07-31 17:04:11 -0700252 checkNotNull(deviceId, "Must specify a device");
253 checkNotNull(bands, "Must have bands.");
Jon Hallcbd1b392017-01-18 20:15:44 -0800254 checkArgument(!bands.isEmpty(), "Must have at least one band.");
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900255 checkArgument(cellId != null, "Must specify a cell id.");
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200256 return new DefaultMeter(deviceId, cellId, appId, unit, burst, bands,
257 annotations);
alshabib1d2bc402015-07-31 17:04:11 -0700258 }
259
260
261 }
262}