blob: ab6b44441406e953cf8f05aaa7ad235a06bfe80a [file] [log] [blame]
alshabib1d2bc402015-07-31 17:04:11 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
alshabib58fe6dc2015-08-19 17:16:13 -070018import com.google.common.collect.ImmutableSet;
alshabib1d2bc402015-07-31 17:04:11 -070019import org.onosproject.core.ApplicationId;
20import org.onosproject.net.DeviceId;
21
22import java.util.Collection;
alshabib1d2bc402015-07-31 17:04:11 -070023
alshabib58fe6dc2015-08-19 17:16:13 -070024import static com.google.common.base.MoreObjects.toStringHelper;
alshabib1d2bc402015-07-31 17:04:11 -070025import static com.google.common.base.Preconditions.checkArgument;
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * A default implementation of a meter.
30 */
alshabib7bb05012015-08-05 10:15:09 -070031public final class DefaultMeter implements Meter, MeterEntry {
alshabib1d2bc402015-07-31 17:04:11 -070032
33
34 private final MeterId id;
35 private final ApplicationId appId;
36 private final Unit unit;
37 private final boolean burst;
38 private final Collection<Band> bands;
39 private final DeviceId deviceId;
alshabib1d2bc402015-07-31 17:04:11 -070040
alshabib7bb05012015-08-05 10:15:09 -070041 private MeterState state;
42 private long life;
43 private long refCount;
44 private long packets;
45 private long bytes;
46
alshabib1d2bc402015-07-31 17:04:11 -070047 private DefaultMeter(DeviceId deviceId, MeterId id, ApplicationId appId,
48 Unit unit, boolean burst,
alshabibeadfc8e2015-08-18 15:40:46 -070049 Collection<Band> bands) {
alshabib1d2bc402015-07-31 17:04:11 -070050 this.deviceId = deviceId;
51 this.id = id;
52 this.appId = appId;
53 this.unit = unit;
54 this.burst = burst;
55 this.bands = bands;
alshabib1d2bc402015-07-31 17:04:11 -070056 }
57
58 @Override
59 public DeviceId deviceId() {
60 return deviceId;
61 }
62
63 @Override
64 public MeterId id() {
65 return id;
66 }
67
68 @Override
69 public ApplicationId appId() {
70 return appId;
71 }
72
73 @Override
74 public Unit unit() {
75 return unit;
76 }
77
78 @Override
79 public boolean isBurst() {
80 return burst;
81 }
82
83 @Override
84 public Collection<Band> bands() {
85 return bands;
86 }
87
88 @Override
alshabib7bb05012015-08-05 10:15:09 -070089 public MeterState state() {
90 return state;
91 }
92
93 @Override
94 public long life() {
95 return life;
96 }
97
98 @Override
99 public long referenceCount() {
100 return refCount;
101 }
102
103 @Override
104 public long packetsSeen() {
105 return packets;
106 }
107
108 @Override
109 public long bytesSeen() {
110 return bytes;
111 }
112
alshabib1d2bc402015-07-31 17:04:11 -0700113 public static Builder builder() {
114 return new Builder();
115 }
116
alshabib7bb05012015-08-05 10:15:09 -0700117 @Override
118 public void setState(MeterState state) {
119 this.state = state;
120 }
121
122 @Override
123 public void setLife(long life) {
124 this.life = life;
125 }
126
127 @Override
128 public void setReferenceCount(long count) {
129 this.refCount = count;
130 }
131
132 @Override
133 public void setProcessedPackets(long packets) {
134 this.packets = packets;
135 }
136
137 @Override
138 public void setProcessedBytes(long bytes) {
139 this.bytes = bytes;
140 }
141
alshabib58fe6dc2015-08-19 17:16:13 -0700142 @Override
143 public String toString() {
144 return toStringHelper(this)
145 .add("device", deviceId)
146 .add("id", id)
147 .add("appId", appId.name())
148 .add("unit", unit)
149 .add("isBurst", burst)
150 .add("state", state)
151 .add("bands", bands).toString();
152 }
153
alshabib1d2bc402015-07-31 17:04:11 -0700154 public static final class Builder implements Meter.Builder {
155
156 private MeterId id;
157 private ApplicationId appId;
158 private Unit unit = Unit.KB_PER_SEC;
159 private boolean burst = false;
160 private Collection<Band> bands;
161 private DeviceId deviceId;
alshabib1d2bc402015-07-31 17:04:11 -0700162
163
164 @Override
165 public Meter.Builder forDevice(DeviceId deviceId) {
166 this.deviceId = deviceId;
167 return this;
168 }
169
170 @Override
alshabib58fe6dc2015-08-19 17:16:13 -0700171 public Meter.Builder withId(MeterId id) {
172 this.id = id;
alshabib1d2bc402015-07-31 17:04:11 -0700173 return this;
174 }
175
176 @Override
177 public Meter.Builder fromApp(ApplicationId appId) {
178 this.appId = appId;
179 return this;
180 }
181
182 @Override
183 public Meter.Builder withUnit(Unit unit) {
184 this.unit = unit;
185 return this;
186 }
187
188 @Override
189 public Meter.Builder burst() {
190 this.burst = true;
191 return this;
192 }
193
194 @Override
195 public Meter.Builder withBands(Collection<Band> bands) {
alshabib58fe6dc2015-08-19 17:16:13 -0700196 this.bands = ImmutableSet.copyOf(bands);
alshabib1d2bc402015-07-31 17:04:11 -0700197 return this;
198 }
199
200 @Override
alshabib7bb05012015-08-05 10:15:09 -0700201 public DefaultMeter build() {
alshabib1d2bc402015-07-31 17:04:11 -0700202 checkNotNull(deviceId, "Must specify a device");
203 checkNotNull(bands, "Must have bands.");
204 checkArgument(bands.size() > 0, "Must have at least one band.");
205 checkNotNull(appId, "Must have an application id");
206 checkNotNull(id, "Must specify a meter id");
alshabibeadfc8e2015-08-18 15:40:46 -0700207 return new DefaultMeter(deviceId, id, appId, unit, burst, bands);
alshabib1d2bc402015-07-31 17:04:11 -0700208 }
209
210
211 }
212}