blob: 8827eccbdf6e3cd080ff8471baba34319cca6dec [file] [log] [blame]
sangho538108b2015-04-08 14:29:20 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho538108b2015-04-08 14:29:20 -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 */
16package org.onosproject.net.device;
17
18import org.onosproject.net.DeviceId;
19
20/**
21 * Default implementation of immutable port statistics.
22 */
23public final class DefaultPortStatistics implements PortStatistics {
24
25 private final DeviceId deviceId;
26 private final int port;
27 private final long packetsReceived;
28 private final long packetsSent;
29 private final long bytesReceived;
30 private final long bytesSent;
31 private final long packetsRxDropped;
32 private final long packetsTxDropped;
33 private final long packetsRxErrors;
34 private final long packetsTxErrors;
35 private final long durationSec;
36 private final long durationNano;
37
38 private DefaultPortStatistics(DeviceId deviceId,
39 int port,
40 long packetsReceived,
41 long packetsSent,
42 long bytesReceived,
43 long bytesSent,
44 long packetsRxDropped,
45 long packetsTxDropped,
46 long packetsRxErrors,
47 long packetsTxErrors,
48 long durationSec,
49 long durationNano) {
50 this.deviceId = deviceId;
51 this.port = port;
52 this.packetsReceived = packetsReceived;
53 this.packetsSent = packetsSent;
54 this.bytesReceived = bytesReceived;
55 this.bytesSent = bytesSent;
56 this.packetsRxDropped = packetsRxDropped;
57 this.packetsTxDropped = packetsTxDropped;
58 this.packetsRxErrors = packetsRxErrors;
59 this.packetsTxErrors = packetsTxErrors;
60 this.durationSec = durationSec;
61 this.durationNano = durationNano;
62 }
63
Thomas Vachuskafdbc4c22015-05-29 15:53:01 -070064 // Constructor for serializer
65 private DefaultPortStatistics() {
66 this.deviceId = null;
67 this.port = 0;
68 this.packetsReceived = 0;
69 this.packetsSent = 0;
70 this.bytesReceived = 0;
71 this.bytesSent = 0;
72 this.packetsRxDropped = 0;
73 this.packetsTxDropped = 0;
74 this.packetsRxErrors = 0;
75 this.packetsTxErrors = 0;
76 this.durationSec = 0;
77 this.durationNano = 0;
78 }
79
sangho538108b2015-04-08 14:29:20 -070080 /**
81 * Creates a builder for DefaultPortStatistics object.
82 *
83 * @return builder object for DefaultPortStatistics object
84 */
85 public static DefaultPortStatistics.Builder builder() {
86 return new Builder();
87 }
88
89 @Override
90 public int port() {
91 return this.port;
92 }
93
94 @Override
95 public long packetsReceived() {
96 return this.packetsReceived;
97 }
98
99 @Override
100 public long packetsSent() {
101 return this.packetsSent;
102 }
103
104 @Override
105 public long bytesReceived() {
106 return this.bytesReceived;
107 }
108
109 @Override
110 public long bytesSent() {
111 return this.bytesSent;
112 }
113
114 @Override
115 public long packetsRxDropped() {
116 return this.packetsRxDropped;
117 }
118
119 @Override
120 public long packetsTxDropped() {
121 return this.packetsTxDropped;
122 }
123
124 @Override
125 public long packetsRxErrors() {
126 return this.packetsRxErrors;
127 }
128
129 @Override
130 public long packetsTxErrors() {
131 return this.packetsTxErrors;
132 }
133
134 @Override
135 public long durationSec() {
136 return this.durationSec;
137 }
138
139 @Override
140 public long durationNano() {
141 return this.durationNano;
142 }
143
144 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700145 public boolean isZero() {
146 return bytesReceived() == 0 &&
147 bytesSent() == 0 &&
148 packetsReceived() == 0 &&
149 packetsRxDropped() == 0 &&
150 packetsSent() == 0 &&
151 packetsTxDropped() == 0;
152 }
153
154 @Override
sangho538108b2015-04-08 14:29:20 -0700155 public String toString() {
Sho SHIMIZU8d50c8d2016-08-12 17:29:02 -0700156 return "device: " + deviceId + ", " +
157 "port: " + this.port + ", " +
158 "pktRx: " + this.packetsReceived + ", " +
159 "pktTx: " + this.packetsSent + ", " +
160 "byteRx: " + this.bytesReceived + ", " +
161 "byteTx: " + this.bytesSent + ", " +
162 "pktRxErr: " + this.packetsRxErrors + ", " +
163 "pktTxErr: " + this.packetsTxErrors + ", " +
164 "pktRxDrp: " + this.packetsRxDropped + ", " +
165 "pktTxDrp: " + this.packetsTxDropped;
sangho538108b2015-04-08 14:29:20 -0700166 }
167
168 public static final class Builder {
169
170 DeviceId deviceId;
171 int port;
172 long packetsReceived;
173 long packetsSent;
174 long bytesReceived;
175 long bytesSent;
176 long packetsRxDropped;
177 long packetsTxDropped;
178 long packetsRxErrors;
179 long packetsTxErrors;
180 long durationSec;
181 long durationNano;
182
183 private Builder() {
184
185 }
186
187 /**
188 * Sets port number.
189 *
190 * @param port port number
191 * @return builder object
192 */
193 public Builder setPort(int port) {
194 this.port = port;
195
196 return this;
197 }
198
199 /**
200 * Sets the device identifier.
201 *
202 * @param deviceId device identifier
203 * @return builder object
204 */
205 public Builder setDeviceId(DeviceId deviceId) {
206 this.deviceId = deviceId;
207
208 return this;
209 }
210
211 /**
212 * Sets the number of packet received.
213 *
214 * @param packets number of packets received
215 * @return builder object
216 */
217 public Builder setPacketsReceived(long packets) {
218 packetsReceived = packets;
219
220 return this;
221 }
222
223 /**
224 * Sets the number of packets sent.
225 *
226 * @param packets number of packets sent
227 * @return builder object
228 */
229 public Builder setPacketsSent(long packets) {
230 packetsSent = packets;
231
232 return this;
233 }
234
235 /**
236 * Sets the number of received bytes.
237 *
238 * @param bytes number of received bytes.
239 * @return builder object
240 */
241 public Builder setBytesReceived(long bytes) {
242 bytesReceived = bytes;
243
244 return this;
245 }
246
247 /**
248 * Sets the number of sent bytes.
249 *
250 * @param bytes number of sent bytes
251 * @return builder object
252 */
253 public Builder setBytesSent(long bytes) {
254 bytesSent = bytes;
255
256 return this;
257 }
258
259 /**
260 * Sets the number of packets dropped by RX.
261 *
262 * @param packets number of packets dropped by RX
263 * @return builder object
264 */
265 public Builder setPacketsRxDropped(long packets) {
266 packetsRxDropped = packets;
267
268 return this;
269 }
270
271 /**
272 * Sets the number of packets dropped by TX.
273 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -0700274 * @param packets number of packets
sangho538108b2015-04-08 14:29:20 -0700275 * @return builder object
276 */
277 public Builder setPacketsTxDropped(long packets) {
278 packetsTxDropped = packets;
279
280 return this;
281 }
282
283 /**
284 * Sets the number of receive errors.
285 *
286 * @param packets number of receive errors
287 * @return builder object
288 */
289 public Builder setPacketsRxErrors(long packets) {
290 packetsRxErrors = packets;
291
292 return this;
293 }
294
295 /**
296 * Sets the number of transmit errors.
297 *
298 * @param packets number of transmit errors
299 * @return builder object
300 */
301 public Builder setPacketsTxErrors(long packets) {
302 packetsTxErrors = packets;
303
304 return this;
305 }
306
307 /**
308 * Sets the time port has been alive in seconds.
309 *
310 * @param sec time port has been alive in seconds
311 * @return builder object
312 */
313 public Builder setDurationSec(long sec) {
314 durationSec = sec;
315
316 return this;
317 }
318
319 /**
320 * Sets the time port has been alive in nano seconds.
321 *
322 * @param nano time port has been alive in nano seconds
323 * @return builder object
324 */
325 public Builder setDurationNano(long nano) {
326 durationNano = nano;
327
328 return this;
329 }
330
331 /**
332 * Creates a PortStatistics object.
333 *
334 * @return DefaultPortStatistics object
335 */
336 public DefaultPortStatistics build() {
337 return new DefaultPortStatistics(
338 deviceId,
339 port,
340 packetsReceived,
341 packetsSent,
342 bytesReceived,
343 bytesSent,
344 packetsRxDropped,
345 packetsTxDropped,
346 packetsRxErrors,
347 packetsTxErrors,
348 durationSec,
349 durationNano);
350 }
351
352 }
Saurav Das59232cf2016-04-27 18:35:50 -0700353
sangho538108b2015-04-08 14:29:20 -0700354}