blob: 177688cb572ae85a348fc7c5729f378c10832240 [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;
alshabib1d2bc402015-07-31 17:04:11 -070023
24import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * A default implementation of a meter.
29 */
alshabib7bb05012015-08-05 10:15:09 -070030public final class DefaultMeter implements Meter, MeterEntry {
alshabib1d2bc402015-07-31 17:04:11 -070031
32
33 private final MeterId id;
34 private final ApplicationId appId;
35 private final Unit unit;
36 private final boolean burst;
37 private final Collection<Band> bands;
38 private final DeviceId deviceId;
alshabib1d2bc402015-07-31 17:04:11 -070039
alshabib7bb05012015-08-05 10:15:09 -070040 private MeterState state;
41 private long life;
42 private long refCount;
43 private long packets;
44 private long bytes;
45
alshabib1d2bc402015-07-31 17:04:11 -070046 private DefaultMeter(DeviceId deviceId, MeterId id, ApplicationId appId,
47 Unit unit, boolean burst,
alshabibeadfc8e2015-08-18 15:40:46 -070048 Collection<Band> bands) {
alshabib1d2bc402015-07-31 17:04:11 -070049 this.deviceId = deviceId;
50 this.id = id;
51 this.appId = appId;
52 this.unit = unit;
53 this.burst = burst;
54 this.bands = bands;
alshabib1d2bc402015-07-31 17:04:11 -070055 }
56
57 @Override
58 public DeviceId deviceId() {
59 return deviceId;
60 }
61
62 @Override
63 public MeterId id() {
64 return id;
65 }
66
67 @Override
68 public ApplicationId appId() {
69 return appId;
70 }
71
72 @Override
73 public Unit unit() {
74 return unit;
75 }
76
77 @Override
78 public boolean isBurst() {
79 return burst;
80 }
81
82 @Override
83 public Collection<Band> bands() {
84 return bands;
85 }
86
87 @Override
alshabib7bb05012015-08-05 10:15:09 -070088 public MeterState state() {
89 return state;
90 }
91
92 @Override
93 public long life() {
94 return life;
95 }
96
97 @Override
98 public long referenceCount() {
99 return refCount;
100 }
101
102 @Override
103 public long packetsSeen() {
104 return packets;
105 }
106
107 @Override
108 public long bytesSeen() {
109 return bytes;
110 }
111
alshabib1d2bc402015-07-31 17:04:11 -0700112 public static Builder builder() {
113 return new Builder();
114 }
115
alshabib7bb05012015-08-05 10:15:09 -0700116 @Override
117 public void setState(MeterState state) {
118 this.state = state;
119 }
120
121 @Override
122 public void setLife(long life) {
123 this.life = life;
124 }
125
126 @Override
127 public void setReferenceCount(long count) {
128 this.refCount = count;
129 }
130
131 @Override
132 public void setProcessedPackets(long packets) {
133 this.packets = packets;
134 }
135
136 @Override
137 public void setProcessedBytes(long bytes) {
138 this.bytes = bytes;
139 }
140
alshabib1d2bc402015-07-31 17:04:11 -0700141 public static final class Builder implements Meter.Builder {
142
143 private MeterId id;
144 private ApplicationId appId;
145 private Unit unit = Unit.KB_PER_SEC;
146 private boolean burst = false;
147 private Collection<Band> bands;
148 private DeviceId deviceId;
alshabib1d2bc402015-07-31 17:04:11 -0700149
150
151 @Override
152 public Meter.Builder forDevice(DeviceId deviceId) {
153 this.deviceId = deviceId;
154 return this;
155 }
156
157 @Override
alshabib7bb05012015-08-05 10:15:09 -0700158 public Meter.Builder withId(long id) {
alshabib1d2bc402015-07-31 17:04:11 -0700159 this.id = MeterId.meterId(id);
160 return this;
161 }
162
163 @Override
164 public Meter.Builder fromApp(ApplicationId appId) {
165 this.appId = appId;
166 return this;
167 }
168
169 @Override
170 public Meter.Builder withUnit(Unit unit) {
171 this.unit = unit;
172 return this;
173 }
174
175 @Override
176 public Meter.Builder burst() {
177 this.burst = true;
178 return this;
179 }
180
181 @Override
182 public Meter.Builder withBands(Collection<Band> bands) {
183 this.bands = Collections.unmodifiableCollection(bands);
184 return this;
185 }
186
187 @Override
alshabib7bb05012015-08-05 10:15:09 -0700188 public DefaultMeter build() {
alshabib1d2bc402015-07-31 17:04:11 -0700189 checkNotNull(deviceId, "Must specify a device");
190 checkNotNull(bands, "Must have bands.");
191 checkArgument(bands.size() > 0, "Must have at least one band.");
192 checkNotNull(appId, "Must have an application id");
193 checkNotNull(id, "Must specify a meter id");
alshabibeadfc8e2015-08-18 15:40:46 -0700194 return new DefaultMeter(deviceId, id, appId, unit, burst, bands);
alshabib1d2bc402015-07-31 17:04:11 -0700195 }
196
197
198 }
199}