blob: f3e29feb8f21a55e4dfb69f307061ac6eb3453bc [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
38
Frank Wangd7e3b4b2017-09-24 13:37:54 +090039 private final MeterCellId cellId;
Wailok Shum79919522021-08-22 19:35:34 +080040 private final Optional<ApplicationId> appId;
alshabib1d2bc402015-07-31 17:04:11 -070041 private final Unit unit;
42 private final boolean burst;
43 private final Collection<Band> bands;
44 private final DeviceId deviceId;
alshabib1d2bc402015-07-31 17:04:11 -070045
alshabib7bb05012015-08-05 10:15:09 -070046 private MeterState state;
47 private long life;
48 private long refCount;
49 private long packets;
50 private long bytes;
51
Wailok Shum79919522021-08-22 19:35:34 +080052 private DefaultMeter(DeviceId deviceId, MeterCellId cellId,
53 Optional<ApplicationId> appId, Unit unit,
54 boolean burst, Collection<Band> bands,
Andrea Campanella5bdbe432021-05-03 15:59:19 +020055 Annotations... annotations) {
56 super(annotations);
alshabib1d2bc402015-07-31 17:04:11 -070057 this.deviceId = deviceId;
Frank Wangd7e3b4b2017-09-24 13:37:54 +090058 this.cellId = cellId;
alshabib1d2bc402015-07-31 17:04:11 -070059 this.appId = appId;
60 this.unit = unit;
61 this.burst = burst;
62 this.bands = bands;
alshabib1d2bc402015-07-31 17:04:11 -070063 }
64
65 @Override
66 public DeviceId deviceId() {
67 return deviceId;
68 }
69
70 @Override
71 public MeterId id() {
Frank Wangd7e3b4b2017-09-24 13:37:54 +090072 // 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;
alshabib1d2bc402015-07-31 17:04:11 -070082 }
83
84 @Override
85 public ApplicationId appId() {
Wailok Shum79919522021-08-22 19:35:34 +080086 return appId.orElse(null);
87 // TODO: Deprecate this API because AppId becomes optional in Meter
alshabib1d2bc402015-07-31 17:04:11 -070088 }
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
alshabib7bb05012015-08-05 10:15:09 -0700106 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
alshabib1d2bc402015-07-31 17:04:11 -0700130 public static Builder builder() {
131 return new Builder();
132 }
133
alshabib7bb05012015-08-05 10:15:09 -0700134 @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
alshabib58fe6dc2015-08-19 17:16:13 -0700159 @Override
160 public String toString() {
161 return toStringHelper(this)
162 .add("device", deviceId)
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900163 .add("cellId", cellId)
Wailok Shum79919522021-08-22 19:35:34 +0800164 .add("appId", appId.orElse(null))
alshabib58fe6dc2015-08-19 17:16:13 -0700165 .add("unit", unit)
166 .add("isBurst", burst)
167 .add("state", state)
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200168 .add("bands", bands)
169 .add("annotations", annotations())
170 .toString();
alshabib58fe6dc2015-08-19 17:16:13 -0700171 }
172
alshabibe1248b62015-08-20 17:21:55 -0700173 @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 Wangd7e3b4b2017-09-24 13:37:54 +0900182 return Objects.equal(cellId, that.cellId) &&
alshabibe1248b62015-08-20 17:21:55 -0700183 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 Wangd7e3b4b2017-09-24 13:37:54 +0900190 return Objects.hashCode(cellId, appId, unit, deviceId);
alshabibe1248b62015-08-20 17:21:55 -0700191 }
192
alshabib1d2bc402015-07-31 17:04:11 -0700193 public static final class Builder implements Meter.Builder {
194
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900195 private MeterCellId cellId;
Wailok Shum79919522021-08-22 19:35:34 +0800196 private Optional<ApplicationId> appId = Optional.empty();
alshabib1d2bc402015-07-31 17:04:11 -0700197 private Unit unit = Unit.KB_PER_SEC;
198 private boolean burst = false;
199 private Collection<Band> bands;
200 private DeviceId deviceId;
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200201 private Annotations annotations;
alshabib1d2bc402015-07-31 17:04:11 -0700202
alshabib1d2bc402015-07-31 17:04:11 -0700203 @Override
204 public Meter.Builder forDevice(DeviceId deviceId) {
205 this.deviceId = deviceId;
206 return this;
207 }
208
209 @Override
alshabib58fe6dc2015-08-19 17:16:13 -0700210 public Meter.Builder withId(MeterId id) {
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900211 this.withCellId(id);
212 return this;
213 }
214
215 @Override
216 public Meter.Builder withCellId(MeterCellId cellId) {
217 this.cellId = cellId;
alshabib1d2bc402015-07-31 17:04:11 -0700218 return this;
219 }
220
221 @Override
222 public Meter.Builder fromApp(ApplicationId appId) {
Wailok Shum79919522021-08-22 19:35:34 +0800223 this.appId = Optional.ofNullable(appId);
alshabib1d2bc402015-07-31 17:04:11 -0700224 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) {
alshabib58fe6dc2015-08-19 17:16:13 -0700241 this.bands = ImmutableSet.copyOf(bands);
alshabib1d2bc402015-07-31 17:04:11 -0700242 return this;
243 }
244
245 @Override
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200246 public Builder withAnnotations(Annotations anns) {
247 this.annotations = anns;
248 return this;
249 }
250
251 @Override
alshabib7bb05012015-08-05 10:15:09 -0700252 public DefaultMeter build() {
alshabib1d2bc402015-07-31 17:04:11 -0700253 checkNotNull(deviceId, "Must specify a device");
254 checkNotNull(bands, "Must have bands.");
Jon Hallcbd1b392017-01-18 20:15:44 -0800255 checkArgument(!bands.isEmpty(), "Must have at least one band.");
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900256 checkArgument(cellId != null, "Must specify a cell id.");
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200257 return new DefaultMeter(deviceId, cellId, appId, unit, burst, bands,
258 annotations);
alshabib1d2bc402015-07-31 17:04:11 -0700259 }
260
261
262 }
263}