blob: e2e049db347792c8d83e756b45d18ba12e057ba2 [file] [log] [blame]
alshabib1d2bc402015-07-31 17:04:11 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
28
29/**
30 * A default implementation of a meter.
31 */
alshabib7bb05012015-08-05 10:15:09 -070032public final class DefaultMeter implements Meter, MeterEntry {
alshabib1d2bc402015-07-31 17:04:11 -070033
34
35 private final MeterId id;
36 private final ApplicationId appId;
37 private final Unit unit;
38 private final boolean burst;
39 private final Collection<Band> bands;
40 private final DeviceId deviceId;
alshabib1d2bc402015-07-31 17:04:11 -070041
alshabib7bb05012015-08-05 10:15:09 -070042 private MeterState state;
43 private long life;
44 private long refCount;
45 private long packets;
46 private long bytes;
47
alshabib1d2bc402015-07-31 17:04:11 -070048 private DefaultMeter(DeviceId deviceId, MeterId id, ApplicationId appId,
49 Unit unit, boolean burst,
alshabibeadfc8e2015-08-18 15:40:46 -070050 Collection<Band> bands) {
alshabib1d2bc402015-07-31 17:04:11 -070051 this.deviceId = deviceId;
52 this.id = id;
53 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() {
66 return id;
67 }
68
69 @Override
70 public ApplicationId appId() {
71 return appId;
72 }
73
74 @Override
75 public Unit unit() {
76 return unit;
77 }
78
79 @Override
80 public boolean isBurst() {
81 return burst;
82 }
83
84 @Override
85 public Collection<Band> bands() {
86 return bands;
87 }
88
89 @Override
alshabib7bb05012015-08-05 10:15:09 -070090 public MeterState state() {
91 return state;
92 }
93
94 @Override
95 public long life() {
96 return life;
97 }
98
99 @Override
100 public long referenceCount() {
101 return refCount;
102 }
103
104 @Override
105 public long packetsSeen() {
106 return packets;
107 }
108
109 @Override
110 public long bytesSeen() {
111 return bytes;
112 }
113
alshabib1d2bc402015-07-31 17:04:11 -0700114 public static Builder builder() {
115 return new Builder();
116 }
117
alshabib7bb05012015-08-05 10:15:09 -0700118 @Override
119 public void setState(MeterState state) {
120 this.state = state;
121 }
122
123 @Override
124 public void setLife(long life) {
125 this.life = life;
126 }
127
128 @Override
129 public void setReferenceCount(long count) {
130 this.refCount = count;
131 }
132
133 @Override
134 public void setProcessedPackets(long packets) {
135 this.packets = packets;
136 }
137
138 @Override
139 public void setProcessedBytes(long bytes) {
140 this.bytes = bytes;
141 }
142
alshabib58fe6dc2015-08-19 17:16:13 -0700143 @Override
144 public String toString() {
145 return toStringHelper(this)
146 .add("device", deviceId)
147 .add("id", id)
148 .add("appId", appId.name())
149 .add("unit", unit)
150 .add("isBurst", burst)
151 .add("state", state)
152 .add("bands", bands).toString();
153 }
154
alshabibe1248b62015-08-20 17:21:55 -0700155 @Override
156 public boolean equals(Object o) {
157 if (this == o) {
158 return true;
159 }
160 if (o == null || getClass() != o.getClass()) {
161 return false;
162 }
163 DefaultMeter that = (DefaultMeter) o;
164 return Objects.equal(id, that.id) &&
165 Objects.equal(appId, that.appId) &&
166 Objects.equal(unit, that.unit) &&
167 Objects.equal(deviceId, that.deviceId);
168 }
169
170 @Override
171 public int hashCode() {
172 return Objects.hashCode(id, appId, unit, deviceId);
173 }
174
alshabib1d2bc402015-07-31 17:04:11 -0700175 public static final class Builder implements Meter.Builder {
176
177 private MeterId id;
178 private ApplicationId appId;
179 private Unit unit = Unit.KB_PER_SEC;
180 private boolean burst = false;
181 private Collection<Band> bands;
182 private DeviceId deviceId;
alshabib1d2bc402015-07-31 17:04:11 -0700183
184
185 @Override
186 public Meter.Builder forDevice(DeviceId deviceId) {
187 this.deviceId = deviceId;
188 return this;
189 }
190
191 @Override
alshabib58fe6dc2015-08-19 17:16:13 -0700192 public Meter.Builder withId(MeterId id) {
193 this.id = id;
alshabib1d2bc402015-07-31 17:04:11 -0700194 return this;
195 }
196
197 @Override
198 public Meter.Builder fromApp(ApplicationId appId) {
199 this.appId = appId;
200 return this;
201 }
202
203 @Override
204 public Meter.Builder withUnit(Unit unit) {
205 this.unit = unit;
206 return this;
207 }
208
209 @Override
210 public Meter.Builder burst() {
211 this.burst = true;
212 return this;
213 }
214
215 @Override
216 public Meter.Builder withBands(Collection<Band> bands) {
alshabib58fe6dc2015-08-19 17:16:13 -0700217 this.bands = ImmutableSet.copyOf(bands);
alshabib1d2bc402015-07-31 17:04:11 -0700218 return this;
219 }
220
221 @Override
alshabib7bb05012015-08-05 10:15:09 -0700222 public DefaultMeter build() {
alshabib1d2bc402015-07-31 17:04:11 -0700223 checkNotNull(deviceId, "Must specify a device");
224 checkNotNull(bands, "Must have bands.");
225 checkArgument(bands.size() > 0, "Must have at least one band.");
226 checkNotNull(appId, "Must have an application id");
227 checkNotNull(id, "Must specify a meter id");
alshabibeadfc8e2015-08-18 15:40:46 -0700228 return new DefaultMeter(deviceId, id, appId, unit, burst, bands);
alshabib1d2bc402015-07-31 17:04:11 -0700229 }
230
231
232 }
233}