blob: e46def2b612db2c84796fd8bfb75689db235e317 [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
Laszlo Papp7cf60372018-01-11 00:06:43 +000018import org.onosproject.net.AbstractAnnotated;
19import org.onosproject.net.Annotations;
sangho538108b2015-04-08 14:29:20 -070020import org.onosproject.net.DeviceId;
Andrea Campanellac86154a2017-09-01 11:28:59 +020021import org.onosproject.net.PortNumber;
sangho538108b2015-04-08 14:29:20 -070022
23/**
24 * Default implementation of immutable port statistics.
25 */
Laszlo Papp7cf60372018-01-11 00:06:43 +000026public final class DefaultPortStatistics extends AbstractAnnotated implements PortStatistics {
sangho538108b2015-04-08 14:29:20 -070027
28 private final DeviceId deviceId;
Andrea Campanellac86154a2017-09-01 11:28:59 +020029 private final PortNumber portNumber;
sangho538108b2015-04-08 14:29:20 -070030 private final long packetsReceived;
31 private final long packetsSent;
32 private final long bytesReceived;
33 private final long bytesSent;
34 private final long packetsRxDropped;
35 private final long packetsTxDropped;
36 private final long packetsRxErrors;
37 private final long packetsTxErrors;
38 private final long durationSec;
39 private final long durationNano;
40
41 private DefaultPortStatistics(DeviceId deviceId,
Andrea Campanellac86154a2017-09-01 11:28:59 +020042 PortNumber portNumber,
sangho538108b2015-04-08 14:29:20 -070043 long packetsReceived,
44 long packetsSent,
45 long bytesReceived,
46 long bytesSent,
47 long packetsRxDropped,
48 long packetsTxDropped,
49 long packetsRxErrors,
50 long packetsTxErrors,
51 long durationSec,
Laszlo Papp7cf60372018-01-11 00:06:43 +000052 long durationNano,
53 Annotations annotations) {
54 super(annotations);
sangho538108b2015-04-08 14:29:20 -070055 this.deviceId = deviceId;
Andrea Campanellac86154a2017-09-01 11:28:59 +020056 this.portNumber = portNumber;
sangho538108b2015-04-08 14:29:20 -070057 this.packetsReceived = packetsReceived;
58 this.packetsSent = packetsSent;
59 this.bytesReceived = bytesReceived;
60 this.bytesSent = bytesSent;
61 this.packetsRxDropped = packetsRxDropped;
62 this.packetsTxDropped = packetsTxDropped;
63 this.packetsRxErrors = packetsRxErrors;
64 this.packetsTxErrors = packetsTxErrors;
65 this.durationSec = durationSec;
66 this.durationNano = durationNano;
67 }
68
Thomas Vachuskafdbc4c22015-05-29 15:53:01 -070069 // Constructor for serializer
70 private DefaultPortStatistics() {
71 this.deviceId = null;
Andrea Campanellac86154a2017-09-01 11:28:59 +020072 this.portNumber = null;
Thomas Vachuskafdbc4c22015-05-29 15:53:01 -070073 this.packetsReceived = 0;
74 this.packetsSent = 0;
75 this.bytesReceived = 0;
76 this.bytesSent = 0;
77 this.packetsRxDropped = 0;
78 this.packetsTxDropped = 0;
79 this.packetsRxErrors = 0;
80 this.packetsTxErrors = 0;
81 this.durationSec = 0;
82 this.durationNano = 0;
83 }
84
sangho538108b2015-04-08 14:29:20 -070085 /**
86 * Creates a builder for DefaultPortStatistics object.
87 *
88 * @return builder object for DefaultPortStatistics object
89 */
90 public static DefaultPortStatistics.Builder builder() {
91 return new Builder();
92 }
93
94 @Override
95 public int port() {
Andrea Campanellac86154a2017-09-01 11:28:59 +020096 return (int) this.portNumber.toLong();
97 }
98
99 @Override
100 public PortNumber portNumber() {
101 return this.portNumber;
sangho538108b2015-04-08 14:29:20 -0700102 }
103
104 @Override
105 public long packetsReceived() {
106 return this.packetsReceived;
107 }
108
109 @Override
110 public long packetsSent() {
111 return this.packetsSent;
112 }
113
114 @Override
115 public long bytesReceived() {
116 return this.bytesReceived;
117 }
118
119 @Override
120 public long bytesSent() {
121 return this.bytesSent;
122 }
123
124 @Override
125 public long packetsRxDropped() {
126 return this.packetsRxDropped;
127 }
128
129 @Override
130 public long packetsTxDropped() {
131 return this.packetsTxDropped;
132 }
133
134 @Override
135 public long packetsRxErrors() {
136 return this.packetsRxErrors;
137 }
138
139 @Override
140 public long packetsTxErrors() {
141 return this.packetsTxErrors;
142 }
143
144 @Override
145 public long durationSec() {
146 return this.durationSec;
147 }
148
149 @Override
150 public long durationNano() {
151 return this.durationNano;
152 }
153
154 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700155 public boolean isZero() {
156 return bytesReceived() == 0 &&
157 bytesSent() == 0 &&
158 packetsReceived() == 0 &&
159 packetsRxDropped() == 0 &&
160 packetsSent() == 0 &&
161 packetsTxDropped() == 0;
162 }
163
164 @Override
sangho538108b2015-04-08 14:29:20 -0700165 public String toString() {
Sho SHIMIZU8d50c8d2016-08-12 17:29:02 -0700166 return "device: " + deviceId + ", " +
Andrea Campanellac86154a2017-09-01 11:28:59 +0200167 "port: " + this.portNumber + ", " +
Sho SHIMIZU8d50c8d2016-08-12 17:29:02 -0700168 "pktRx: " + this.packetsReceived + ", " +
169 "pktTx: " + this.packetsSent + ", " +
170 "byteRx: " + this.bytesReceived + ", " +
171 "byteTx: " + this.bytesSent + ", " +
172 "pktRxErr: " + this.packetsRxErrors + ", " +
173 "pktTxErr: " + this.packetsTxErrors + ", " +
174 "pktRxDrp: " + this.packetsRxDropped + ", " +
Laszlo Papp7cf60372018-01-11 00:06:43 +0000175 "pktTxDrp: " + this.packetsTxDropped + ", " +
176 "annotations: " + annotations();
sangho538108b2015-04-08 14:29:20 -0700177 }
178
179 public static final class Builder {
180
181 DeviceId deviceId;
Andrea Campanellac86154a2017-09-01 11:28:59 +0200182 PortNumber portNumber;
sangho538108b2015-04-08 14:29:20 -0700183 long packetsReceived;
184 long packetsSent;
185 long bytesReceived;
186 long bytesSent;
187 long packetsRxDropped;
188 long packetsTxDropped;
189 long packetsRxErrors;
190 long packetsTxErrors;
191 long durationSec;
192 long durationNano;
Laszlo Papp7cf60372018-01-11 00:06:43 +0000193 Annotations annotations;
sangho538108b2015-04-08 14:29:20 -0700194
195 private Builder() {
196
197 }
198
199 /**
200 * Sets port number.
201 *
202 * @param port port number
203 * @return builder object
Andrea Campanellac86154a2017-09-01 11:28:59 +0200204 * @deprecated ONOS 1.12 Magpie
sangho538108b2015-04-08 14:29:20 -0700205 */
Andrea Campanellac86154a2017-09-01 11:28:59 +0200206 @Deprecated
sangho538108b2015-04-08 14:29:20 -0700207 public Builder setPort(int port) {
Andrea Campanellac86154a2017-09-01 11:28:59 +0200208 this.portNumber = PortNumber.portNumber(port);
209
210 return this;
211 }
212
213 /**
214 * Sets port number.
215 *
216 * @param portNumber port number
217 * @return builder object
218 */
219 public Builder setPort(PortNumber portNumber) {
220 this.portNumber = portNumber;
sangho538108b2015-04-08 14:29:20 -0700221
222 return this;
223 }
224
225 /**
226 * Sets the device identifier.
227 *
228 * @param deviceId device identifier
229 * @return builder object
230 */
231 public Builder setDeviceId(DeviceId deviceId) {
232 this.deviceId = deviceId;
233
234 return this;
235 }
236
237 /**
238 * Sets the number of packet received.
239 *
240 * @param packets number of packets received
241 * @return builder object
242 */
243 public Builder setPacketsReceived(long packets) {
244 packetsReceived = packets;
245
246 return this;
247 }
248
249 /**
250 * Sets the number of packets sent.
251 *
252 * @param packets number of packets sent
253 * @return builder object
254 */
255 public Builder setPacketsSent(long packets) {
256 packetsSent = packets;
257
258 return this;
259 }
260
261 /**
262 * Sets the number of received bytes.
263 *
264 * @param bytes number of received bytes.
265 * @return builder object
266 */
267 public Builder setBytesReceived(long bytes) {
268 bytesReceived = bytes;
269
270 return this;
271 }
272
273 /**
274 * Sets the number of sent bytes.
275 *
276 * @param bytes number of sent bytes
277 * @return builder object
278 */
279 public Builder setBytesSent(long bytes) {
280 bytesSent = bytes;
281
282 return this;
283 }
284
285 /**
286 * Sets the number of packets dropped by RX.
287 *
288 * @param packets number of packets dropped by RX
289 * @return builder object
290 */
291 public Builder setPacketsRxDropped(long packets) {
292 packetsRxDropped = packets;
293
294 return this;
295 }
296
297 /**
298 * Sets the number of packets dropped by TX.
299 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -0700300 * @param packets number of packets
sangho538108b2015-04-08 14:29:20 -0700301 * @return builder object
302 */
303 public Builder setPacketsTxDropped(long packets) {
304 packetsTxDropped = packets;
305
306 return this;
307 }
308
309 /**
310 * Sets the number of receive errors.
311 *
312 * @param packets number of receive errors
313 * @return builder object
314 */
315 public Builder setPacketsRxErrors(long packets) {
316 packetsRxErrors = packets;
317
318 return this;
319 }
320
321 /**
322 * Sets the number of transmit errors.
323 *
324 * @param packets number of transmit errors
325 * @return builder object
326 */
327 public Builder setPacketsTxErrors(long packets) {
328 packetsTxErrors = packets;
329
330 return this;
331 }
332
333 /**
334 * Sets the time port has been alive in seconds.
335 *
336 * @param sec time port has been alive in seconds
337 * @return builder object
338 */
339 public Builder setDurationSec(long sec) {
340 durationSec = sec;
341
342 return this;
343 }
344
345 /**
346 * Sets the time port has been alive in nano seconds.
347 *
348 * @param nano time port has been alive in nano seconds
349 * @return builder object
350 */
351 public Builder setDurationNano(long nano) {
352 durationNano = nano;
353
354 return this;
355 }
356
357 /**
Laszlo Papp7cf60372018-01-11 00:06:43 +0000358 * Sets the annotations.
359 *
360 * @param anns annotations
361 * @return builder object
362 */
363 public Builder setAnnotations(Annotations anns) {
364 annotations = anns;
365
366 return this;
367 }
368
369 /**
sangho538108b2015-04-08 14:29:20 -0700370 * Creates a PortStatistics object.
371 *
372 * @return DefaultPortStatistics object
373 */
374 public DefaultPortStatistics build() {
375 return new DefaultPortStatistics(
376 deviceId,
Andrea Campanellac86154a2017-09-01 11:28:59 +0200377 portNumber,
sangho538108b2015-04-08 14:29:20 -0700378 packetsReceived,
379 packetsSent,
380 bytesReceived,
381 bytesSent,
382 packetsRxDropped,
383 packetsTxDropped,
384 packetsRxErrors,
385 packetsTxErrors,
386 durationSec,
Laszlo Papp7cf60372018-01-11 00:06:43 +0000387 durationNano,
388 annotations);
sangho538108b2015-04-08 14:29:20 -0700389 }
390
391 }
Saurav Das59232cf2016-04-27 18:35:50 -0700392
sangho538108b2015-04-08 14:29:20 -0700393}