blob: 681846fff082aee3512b1df35ef2aa91176c1a22 [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;
alshabib1d2bc402015-07-31 17:04:11 -070026
alshabib58fe6dc2015-08-19 17:16:13 -070027import static com.google.common.base.MoreObjects.toStringHelper;
alshabib1d2bc402015-07-31 17:04:11 -070028import static com.google.common.base.Preconditions.checkArgument;
29import static com.google.common.base.Preconditions.checkNotNull;
Frank Wangd7e3b4b2017-09-24 13:37:54 +090030import static org.onosproject.net.meter.MeterCellId.MeterCellType.INDEX;
alshabib1d2bc402015-07-31 17:04:11 -070031
32/**
33 * A default implementation of a meter.
34 */
Andrea Campanella5bdbe432021-05-03 15:59:19 +020035public final class DefaultMeter extends AbstractAnnotated implements Meter, MeterEntry {
alshabib1d2bc402015-07-31 17:04:11 -070036
37
Frank Wangd7e3b4b2017-09-24 13:37:54 +090038 private final MeterCellId cellId;
alshabib1d2bc402015-07-31 17:04:11 -070039 private final ApplicationId appId;
40 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
Frank Wangd7e3b4b2017-09-24 13:37:54 +090051 private DefaultMeter(DeviceId deviceId, MeterCellId cellId, ApplicationId appId,
Andrea Campanella5bdbe432021-05-03 15:59:19 +020052 Unit unit, boolean burst, Collection<Band> bands,
53 Annotations... annotations) {
54 super(annotations);
alshabib1d2bc402015-07-31 17:04:11 -070055 this.deviceId = deviceId;
Frank Wangd7e3b4b2017-09-24 13:37:54 +090056 this.cellId = cellId;
alshabib1d2bc402015-07-31 17:04:11 -070057 this.appId = appId;
58 this.unit = unit;
59 this.burst = burst;
60 this.bands = bands;
alshabib1d2bc402015-07-31 17:04:11 -070061 }
62
63 @Override
64 public DeviceId deviceId() {
65 return deviceId;
66 }
67
68 @Override
69 public MeterId id() {
Frank Wangd7e3b4b2017-09-24 13:37:54 +090070 // Workaround until we remove this method. Deprecated in 1.13.
71 // Should use meterCellId() instead.
72 return cellId.type() == INDEX
73 ? (MeterId) cellId
74 : MeterId.meterId((cellId.hashCode()));
75 }
76
77 @Override
78 public MeterCellId meterCellId() {
79 return cellId;
alshabib1d2bc402015-07-31 17:04:11 -070080 }
81
82 @Override
83 public ApplicationId appId() {
84 return appId;
85 }
86
87 @Override
88 public Unit unit() {
89 return unit;
90 }
91
92 @Override
93 public boolean isBurst() {
94 return burst;
95 }
96
97 @Override
98 public Collection<Band> bands() {
99 return bands;
100 }
101
102 @Override
alshabib7bb05012015-08-05 10:15:09 -0700103 public MeterState state() {
104 return state;
105 }
106
107 @Override
108 public long life() {
109 return life;
110 }
111
112 @Override
113 public long referenceCount() {
114 return refCount;
115 }
116
117 @Override
118 public long packetsSeen() {
119 return packets;
120 }
121
122 @Override
123 public long bytesSeen() {
124 return bytes;
125 }
126
alshabib1d2bc402015-07-31 17:04:11 -0700127 public static Builder builder() {
128 return new Builder();
129 }
130
alshabib7bb05012015-08-05 10:15:09 -0700131 @Override
132 public void setState(MeterState state) {
133 this.state = state;
134 }
135
136 @Override
137 public void setLife(long life) {
138 this.life = life;
139 }
140
141 @Override
142 public void setReferenceCount(long count) {
143 this.refCount = count;
144 }
145
146 @Override
147 public void setProcessedPackets(long packets) {
148 this.packets = packets;
149 }
150
151 @Override
152 public void setProcessedBytes(long bytes) {
153 this.bytes = bytes;
154 }
155
alshabib58fe6dc2015-08-19 17:16:13 -0700156 @Override
157 public String toString() {
158 return toStringHelper(this)
159 .add("device", deviceId)
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900160 .add("cellId", cellId)
alshabib58fe6dc2015-08-19 17:16:13 -0700161 .add("appId", appId.name())
162 .add("unit", unit)
163 .add("isBurst", burst)
164 .add("state", state)
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200165 .add("bands", bands)
166 .add("annotations", annotations())
167 .toString();
alshabib58fe6dc2015-08-19 17:16:13 -0700168 }
169
alshabibe1248b62015-08-20 17:21:55 -0700170 @Override
171 public boolean equals(Object o) {
172 if (this == o) {
173 return true;
174 }
175 if (o == null || getClass() != o.getClass()) {
176 return false;
177 }
178 DefaultMeter that = (DefaultMeter) o;
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900179 return Objects.equal(cellId, that.cellId) &&
alshabibe1248b62015-08-20 17:21:55 -0700180 Objects.equal(appId, that.appId) &&
181 Objects.equal(unit, that.unit) &&
182 Objects.equal(deviceId, that.deviceId);
183 }
184
185 @Override
186 public int hashCode() {
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900187 return Objects.hashCode(cellId, appId, unit, deviceId);
alshabibe1248b62015-08-20 17:21:55 -0700188 }
189
alshabib1d2bc402015-07-31 17:04:11 -0700190 public static final class Builder implements Meter.Builder {
191
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900192 private MeterCellId cellId;
alshabib1d2bc402015-07-31 17:04:11 -0700193 private ApplicationId appId;
194 private Unit unit = Unit.KB_PER_SEC;
195 private boolean burst = false;
196 private Collection<Band> bands;
197 private DeviceId deviceId;
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200198 private Annotations annotations;
alshabib1d2bc402015-07-31 17:04:11 -0700199
alshabib1d2bc402015-07-31 17:04:11 -0700200 @Override
201 public Meter.Builder forDevice(DeviceId deviceId) {
202 this.deviceId = deviceId;
203 return this;
204 }
205
206 @Override
alshabib58fe6dc2015-08-19 17:16:13 -0700207 public Meter.Builder withId(MeterId id) {
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900208 this.withCellId(id);
209 return this;
210 }
211
212 @Override
213 public Meter.Builder withCellId(MeterCellId cellId) {
214 this.cellId = cellId;
alshabib1d2bc402015-07-31 17:04:11 -0700215 return this;
216 }
217
218 @Override
219 public Meter.Builder fromApp(ApplicationId appId) {
220 this.appId = appId;
221 return this;
222 }
223
224 @Override
225 public Meter.Builder withUnit(Unit unit) {
226 this.unit = unit;
227 return this;
228 }
229
230 @Override
231 public Meter.Builder burst() {
232 this.burst = true;
233 return this;
234 }
235
236 @Override
237 public Meter.Builder withBands(Collection<Band> bands) {
alshabib58fe6dc2015-08-19 17:16:13 -0700238 this.bands = ImmutableSet.copyOf(bands);
alshabib1d2bc402015-07-31 17:04:11 -0700239 return this;
240 }
241
242 @Override
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200243 public Builder withAnnotations(Annotations anns) {
244 this.annotations = anns;
245 return this;
246 }
247
248 @Override
alshabib7bb05012015-08-05 10:15:09 -0700249 public DefaultMeter build() {
alshabib1d2bc402015-07-31 17:04:11 -0700250 checkNotNull(deviceId, "Must specify a device");
251 checkNotNull(bands, "Must have bands.");
Jon Hallcbd1b392017-01-18 20:15:44 -0800252 checkArgument(!bands.isEmpty(), "Must have at least one band.");
alshabib1d2bc402015-07-31 17:04:11 -0700253 checkNotNull(appId, "Must have an application id");
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900254 checkArgument(cellId != null, "Must specify a cell id.");
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200255 return new DefaultMeter(deviceId, cellId, appId, unit, burst, bands,
256 annotations);
alshabib1d2bc402015-07-31 17:04:11 -0700257 }
258
259
260 }
261}