blob: 138bb18811bb30416a2338d916fdae703bda8a2c [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 */
16package org.onosproject.incubator.net.meter;
17
18import org.onosproject.core.ApplicationId;
19import org.onosproject.net.DeviceId;
20
21import java.util.Collection;
22import java.util.Collections;
23import java.util.Optional;
24
25import 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;
40 private final Optional<MeterContext> context;
41
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,
50 Collection<Band> bands, Optional<MeterContext> context) {
51 this.deviceId = deviceId;
52 this.id = id;
53 this.appId = appId;
54 this.unit = unit;
55 this.burst = burst;
56 this.bands = bands;
57 this.context = context;
58 }
59
60 @Override
61 public DeviceId deviceId() {
62 return deviceId;
63 }
64
65 @Override
66 public MeterId id() {
67 return id;
68 }
69
70 @Override
71 public ApplicationId appId() {
72 return appId;
73 }
74
75 @Override
76 public Unit unit() {
77 return unit;
78 }
79
80 @Override
81 public boolean isBurst() {
82 return burst;
83 }
84
85 @Override
86 public Collection<Band> bands() {
87 return bands;
88 }
89
90 @Override
91 public Optional<MeterContext> context() {
92 return null;
93 }
94
alshabib7bb05012015-08-05 10:15:09 -070095 @Override
96 public MeterState state() {
97 return state;
98 }
99
100 @Override
101 public long life() {
102 return life;
103 }
104
105 @Override
106 public long referenceCount() {
107 return refCount;
108 }
109
110 @Override
111 public long packetsSeen() {
112 return packets;
113 }
114
115 @Override
116 public long bytesSeen() {
117 return bytes;
118 }
119
alshabib1d2bc402015-07-31 17:04:11 -0700120 public static Builder builder() {
121 return new Builder();
122 }
123
alshabib7bb05012015-08-05 10:15:09 -0700124 @Override
125 public void setState(MeterState state) {
126 this.state = state;
127 }
128
129 @Override
130 public void setLife(long life) {
131 this.life = life;
132 }
133
134 @Override
135 public void setReferenceCount(long count) {
136 this.refCount = count;
137 }
138
139 @Override
140 public void setProcessedPackets(long packets) {
141 this.packets = packets;
142 }
143
144 @Override
145 public void setProcessedBytes(long bytes) {
146 this.bytes = bytes;
147 }
148
alshabib1d2bc402015-07-31 17:04:11 -0700149 public static final class Builder implements Meter.Builder {
150
151 private MeterId id;
152 private ApplicationId appId;
153 private Unit unit = Unit.KB_PER_SEC;
154 private boolean burst = false;
155 private Collection<Band> bands;
156 private DeviceId deviceId;
157 private Optional<MeterContext> context;
158
159
160 @Override
161 public Meter.Builder forDevice(DeviceId deviceId) {
162 this.deviceId = deviceId;
163 return this;
164 }
165
166 @Override
alshabib7bb05012015-08-05 10:15:09 -0700167 public Meter.Builder withId(long id) {
alshabib1d2bc402015-07-31 17:04:11 -0700168 this.id = MeterId.meterId(id);
169 return this;
170 }
171
172 @Override
173 public Meter.Builder fromApp(ApplicationId appId) {
174 this.appId = appId;
175 return this;
176 }
177
178 @Override
179 public Meter.Builder withUnit(Unit unit) {
180 this.unit = unit;
181 return this;
182 }
183
184 @Override
185 public Meter.Builder burst() {
186 this.burst = true;
187 return this;
188 }
189
190 @Override
191 public Meter.Builder withBands(Collection<Band> bands) {
192 this.bands = Collections.unmodifiableCollection(bands);
193 return this;
194 }
195
196 @Override
197 public Meter.Builder withContext(MeterContext context) {
198 this.context = Optional.<MeterContext>ofNullable(context);
199 return this;
200 }
201
202 @Override
alshabib7bb05012015-08-05 10:15:09 -0700203 public DefaultMeter build() {
alshabib1d2bc402015-07-31 17:04:11 -0700204 checkNotNull(deviceId, "Must specify a device");
205 checkNotNull(bands, "Must have bands.");
206 checkArgument(bands.size() > 0, "Must have at least one band.");
207 checkNotNull(appId, "Must have an application id");
208 checkNotNull(id, "Must specify a meter id");
209 return new DefaultMeter(deviceId, id, appId, unit, burst, bands, context);
210 }
211
212
213 }
214}