blob: ad0256e757340f684b60e5a5b4074dc909974c35 [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;
21import org.onosproject.net.DeviceId;
22
23import java.util.Collection;
alshabib1d2bc402015-07-31 17:04:11 -070024
alshabib58fe6dc2015-08-19 17:16:13 -070025import static com.google.common.base.MoreObjects.toStringHelper;
alshabib1d2bc402015-07-31 17:04:11 -070026import static com.google.common.base.Preconditions.checkArgument;
27import static com.google.common.base.Preconditions.checkNotNull;
Frank Wangd7e3b4b2017-09-24 13:37:54 +090028import static org.onosproject.net.meter.MeterCellId.MeterCellType.INDEX;
alshabib1d2bc402015-07-31 17:04:11 -070029
30/**
31 * A default implementation of a meter.
32 */
alshabib7bb05012015-08-05 10:15:09 -070033public final class DefaultMeter implements Meter, MeterEntry {
alshabib1d2bc402015-07-31 17:04:11 -070034
35
Frank Wangd7e3b4b2017-09-24 13:37:54 +090036 private final MeterCellId cellId;
alshabib1d2bc402015-07-31 17:04:11 -070037 private final ApplicationId appId;
38 private final Unit unit;
39 private final boolean burst;
40 private final Collection<Band> bands;
41 private final DeviceId deviceId;
alshabib1d2bc402015-07-31 17:04:11 -070042
alshabib7bb05012015-08-05 10:15:09 -070043 private MeterState state;
44 private long life;
45 private long refCount;
46 private long packets;
47 private long bytes;
48
Frank Wangd7e3b4b2017-09-24 13:37:54 +090049 private DefaultMeter(DeviceId deviceId, MeterCellId cellId, ApplicationId appId,
50 Unit unit, boolean burst, Collection<Band> bands) {
alshabib1d2bc402015-07-31 17:04:11 -070051 this.deviceId = deviceId;
Frank Wangd7e3b4b2017-09-24 13:37:54 +090052 this.cellId = cellId;
alshabib1d2bc402015-07-31 17:04:11 -070053 this.appId = appId;
54 this.unit = unit;
55 this.burst = burst;
56 this.bands = bands;
alshabib1d2bc402015-07-31 17:04:11 -070057 }
58
59 @Override
60 public DeviceId deviceId() {
61 return deviceId;
62 }
63
64 @Override
65 public MeterId id() {
Frank Wangd7e3b4b2017-09-24 13:37:54 +090066 // Workaround until we remove this method. Deprecated in 1.13.
67 // Should use meterCellId() instead.
68 return cellId.type() == INDEX
69 ? (MeterId) cellId
70 : MeterId.meterId((cellId.hashCode()));
71 }
72
73 @Override
74 public MeterCellId meterCellId() {
75 return cellId;
alshabib1d2bc402015-07-31 17:04:11 -070076 }
77
78 @Override
79 public ApplicationId appId() {
80 return appId;
81 }
82
83 @Override
84 public Unit unit() {
85 return unit;
86 }
87
88 @Override
89 public boolean isBurst() {
90 return burst;
91 }
92
93 @Override
94 public Collection<Band> bands() {
95 return bands;
96 }
97
98 @Override
alshabib7bb05012015-08-05 10:15:09 -070099 public MeterState state() {
100 return state;
101 }
102
103 @Override
104 public long life() {
105 return life;
106 }
107
108 @Override
109 public long referenceCount() {
110 return refCount;
111 }
112
113 @Override
114 public long packetsSeen() {
115 return packets;
116 }
117
118 @Override
119 public long bytesSeen() {
120 return bytes;
121 }
122
alshabib1d2bc402015-07-31 17:04:11 -0700123 public static Builder builder() {
124 return new Builder();
125 }
126
alshabib7bb05012015-08-05 10:15:09 -0700127 @Override
128 public void setState(MeterState state) {
129 this.state = state;
130 }
131
132 @Override
133 public void setLife(long life) {
134 this.life = life;
135 }
136
137 @Override
138 public void setReferenceCount(long count) {
139 this.refCount = count;
140 }
141
142 @Override
143 public void setProcessedPackets(long packets) {
144 this.packets = packets;
145 }
146
147 @Override
148 public void setProcessedBytes(long bytes) {
149 this.bytes = bytes;
150 }
151
alshabib58fe6dc2015-08-19 17:16:13 -0700152 @Override
153 public String toString() {
154 return toStringHelper(this)
155 .add("device", deviceId)
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900156 .add("cellId", cellId)
alshabib58fe6dc2015-08-19 17:16:13 -0700157 .add("appId", appId.name())
158 .add("unit", unit)
159 .add("isBurst", burst)
160 .add("state", state)
161 .add("bands", bands).toString();
162 }
163
alshabibe1248b62015-08-20 17:21:55 -0700164 @Override
165 public boolean equals(Object o) {
166 if (this == o) {
167 return true;
168 }
169 if (o == null || getClass() != o.getClass()) {
170 return false;
171 }
172 DefaultMeter that = (DefaultMeter) o;
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900173 return Objects.equal(cellId, that.cellId) &&
alshabibe1248b62015-08-20 17:21:55 -0700174 Objects.equal(appId, that.appId) &&
175 Objects.equal(unit, that.unit) &&
176 Objects.equal(deviceId, that.deviceId);
177 }
178
179 @Override
180 public int hashCode() {
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900181 return Objects.hashCode(cellId, appId, unit, deviceId);
alshabibe1248b62015-08-20 17:21:55 -0700182 }
183
alshabib1d2bc402015-07-31 17:04:11 -0700184 public static final class Builder implements Meter.Builder {
185
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900186 private MeterCellId cellId;
alshabib1d2bc402015-07-31 17:04:11 -0700187 private ApplicationId appId;
188 private Unit unit = Unit.KB_PER_SEC;
189 private boolean burst = false;
190 private Collection<Band> bands;
191 private DeviceId deviceId;
alshabib1d2bc402015-07-31 17:04:11 -0700192
alshabib1d2bc402015-07-31 17:04:11 -0700193 @Override
194 public Meter.Builder forDevice(DeviceId deviceId) {
195 this.deviceId = deviceId;
196 return this;
197 }
198
199 @Override
alshabib58fe6dc2015-08-19 17:16:13 -0700200 public Meter.Builder withId(MeterId id) {
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900201 this.withCellId(id);
202 return this;
203 }
204
205 @Override
206 public Meter.Builder withCellId(MeterCellId cellId) {
207 this.cellId = cellId;
alshabib1d2bc402015-07-31 17:04:11 -0700208 return this;
209 }
210
211 @Override
212 public Meter.Builder fromApp(ApplicationId appId) {
213 this.appId = appId;
214 return this;
215 }
216
217 @Override
218 public Meter.Builder withUnit(Unit unit) {
219 this.unit = unit;
220 return this;
221 }
222
223 @Override
224 public Meter.Builder burst() {
225 this.burst = true;
226 return this;
227 }
228
229 @Override
230 public Meter.Builder withBands(Collection<Band> bands) {
alshabib58fe6dc2015-08-19 17:16:13 -0700231 this.bands = ImmutableSet.copyOf(bands);
alshabib1d2bc402015-07-31 17:04:11 -0700232 return this;
233 }
234
235 @Override
alshabib7bb05012015-08-05 10:15:09 -0700236 public DefaultMeter build() {
alshabib1d2bc402015-07-31 17:04:11 -0700237 checkNotNull(deviceId, "Must specify a device");
238 checkNotNull(bands, "Must have bands.");
Jon Hallcbd1b392017-01-18 20:15:44 -0800239 checkArgument(!bands.isEmpty(), "Must have at least one band.");
alshabib1d2bc402015-07-31 17:04:11 -0700240 checkNotNull(appId, "Must have an application id");
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900241 checkArgument(cellId != null, "Must specify a cell id.");
242 return new DefaultMeter(deviceId, cellId, appId, unit, burst, bands);
alshabib1d2bc402015-07-31 17:04:11 -0700243 }
244
245
246 }
247}