blob: 6e0ab42756c6b6e00907dfa64657e9762d942b76 [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;
Andrea Campanellac86154a2017-09-01 11:28:59 +020019import org.onosproject.net.PortNumber;
sangho538108b2015-04-08 14:29:20 -070020
21/**
22 * Default implementation of immutable port statistics.
23 */
24public final class DefaultPortStatistics implements PortStatistics {
25
26 private final DeviceId deviceId;
Andrea Campanellac86154a2017-09-01 11:28:59 +020027 private final PortNumber portNumber;
sangho538108b2015-04-08 14:29:20 -070028 private final long packetsReceived;
29 private final long packetsSent;
30 private final long bytesReceived;
31 private final long bytesSent;
32 private final long packetsRxDropped;
33 private final long packetsTxDropped;
34 private final long packetsRxErrors;
35 private final long packetsTxErrors;
36 private final long durationSec;
37 private final long durationNano;
38
39 private DefaultPortStatistics(DeviceId deviceId,
Andrea Campanellac86154a2017-09-01 11:28:59 +020040 PortNumber portNumber,
sangho538108b2015-04-08 14:29:20 -070041 long packetsReceived,
42 long packetsSent,
43 long bytesReceived,
44 long bytesSent,
45 long packetsRxDropped,
46 long packetsTxDropped,
47 long packetsRxErrors,
48 long packetsTxErrors,
49 long durationSec,
50 long durationNano) {
51 this.deviceId = deviceId;
Andrea Campanellac86154a2017-09-01 11:28:59 +020052 this.portNumber = portNumber;
sangho538108b2015-04-08 14:29:20 -070053 this.packetsReceived = packetsReceived;
54 this.packetsSent = packetsSent;
55 this.bytesReceived = bytesReceived;
56 this.bytesSent = bytesSent;
57 this.packetsRxDropped = packetsRxDropped;
58 this.packetsTxDropped = packetsTxDropped;
59 this.packetsRxErrors = packetsRxErrors;
60 this.packetsTxErrors = packetsTxErrors;
61 this.durationSec = durationSec;
62 this.durationNano = durationNano;
63 }
64
Thomas Vachuskafdbc4c22015-05-29 15:53:01 -070065 // Constructor for serializer
66 private DefaultPortStatistics() {
67 this.deviceId = null;
Andrea Campanellac86154a2017-09-01 11:28:59 +020068 this.portNumber = null;
Thomas Vachuskafdbc4c22015-05-29 15:53:01 -070069 this.packetsReceived = 0;
70 this.packetsSent = 0;
71 this.bytesReceived = 0;
72 this.bytesSent = 0;
73 this.packetsRxDropped = 0;
74 this.packetsTxDropped = 0;
75 this.packetsRxErrors = 0;
76 this.packetsTxErrors = 0;
77 this.durationSec = 0;
78 this.durationNano = 0;
79 }
80
sangho538108b2015-04-08 14:29:20 -070081 /**
82 * Creates a builder for DefaultPortStatistics object.
83 *
84 * @return builder object for DefaultPortStatistics object
85 */
86 public static DefaultPortStatistics.Builder builder() {
87 return new Builder();
88 }
89
90 @Override
91 public int port() {
Andrea Campanellac86154a2017-09-01 11:28:59 +020092 return (int) this.portNumber.toLong();
93 }
94
95 @Override
96 public PortNumber portNumber() {
97 return this.portNumber;
sangho538108b2015-04-08 14:29:20 -070098 }
99
100 @Override
101 public long packetsReceived() {
102 return this.packetsReceived;
103 }
104
105 @Override
106 public long packetsSent() {
107 return this.packetsSent;
108 }
109
110 @Override
111 public long bytesReceived() {
112 return this.bytesReceived;
113 }
114
115 @Override
116 public long bytesSent() {
117 return this.bytesSent;
118 }
119
120 @Override
121 public long packetsRxDropped() {
122 return this.packetsRxDropped;
123 }
124
125 @Override
126 public long packetsTxDropped() {
127 return this.packetsTxDropped;
128 }
129
130 @Override
131 public long packetsRxErrors() {
132 return this.packetsRxErrors;
133 }
134
135 @Override
136 public long packetsTxErrors() {
137 return this.packetsTxErrors;
138 }
139
140 @Override
141 public long durationSec() {
142 return this.durationSec;
143 }
144
145 @Override
146 public long durationNano() {
147 return this.durationNano;
148 }
149
150 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700151 public boolean isZero() {
152 return bytesReceived() == 0 &&
153 bytesSent() == 0 &&
154 packetsReceived() == 0 &&
155 packetsRxDropped() == 0 &&
156 packetsSent() == 0 &&
157 packetsTxDropped() == 0;
158 }
159
160 @Override
sangho538108b2015-04-08 14:29:20 -0700161 public String toString() {
Sho SHIMIZU8d50c8d2016-08-12 17:29:02 -0700162 return "device: " + deviceId + ", " +
Andrea Campanellac86154a2017-09-01 11:28:59 +0200163 "port: " + this.portNumber + ", " +
Sho SHIMIZU8d50c8d2016-08-12 17:29:02 -0700164 "pktRx: " + this.packetsReceived + ", " +
165 "pktTx: " + this.packetsSent + ", " +
166 "byteRx: " + this.bytesReceived + ", " +
167 "byteTx: " + this.bytesSent + ", " +
168 "pktRxErr: " + this.packetsRxErrors + ", " +
169 "pktTxErr: " + this.packetsTxErrors + ", " +
170 "pktRxDrp: " + this.packetsRxDropped + ", " +
171 "pktTxDrp: " + this.packetsTxDropped;
sangho538108b2015-04-08 14:29:20 -0700172 }
173
174 public static final class Builder {
175
176 DeviceId deviceId;
Andrea Campanellac86154a2017-09-01 11:28:59 +0200177 PortNumber portNumber;
sangho538108b2015-04-08 14:29:20 -0700178 long packetsReceived;
179 long packetsSent;
180 long bytesReceived;
181 long bytesSent;
182 long packetsRxDropped;
183 long packetsTxDropped;
184 long packetsRxErrors;
185 long packetsTxErrors;
186 long durationSec;
187 long durationNano;
188
189 private Builder() {
190
191 }
192
193 /**
194 * Sets port number.
195 *
196 * @param port port number
197 * @return builder object
Andrea Campanellac86154a2017-09-01 11:28:59 +0200198 * @deprecated ONOS 1.12 Magpie
sangho538108b2015-04-08 14:29:20 -0700199 */
Andrea Campanellac86154a2017-09-01 11:28:59 +0200200 @Deprecated
sangho538108b2015-04-08 14:29:20 -0700201 public Builder setPort(int port) {
Andrea Campanellac86154a2017-09-01 11:28:59 +0200202 this.portNumber = PortNumber.portNumber(port);
203
204 return this;
205 }
206
207 /**
208 * Sets port number.
209 *
210 * @param portNumber port number
211 * @return builder object
212 */
213 public Builder setPort(PortNumber portNumber) {
214 this.portNumber = portNumber;
sangho538108b2015-04-08 14:29:20 -0700215
216 return this;
217 }
218
219 /**
220 * Sets the device identifier.
221 *
222 * @param deviceId device identifier
223 * @return builder object
224 */
225 public Builder setDeviceId(DeviceId deviceId) {
226 this.deviceId = deviceId;
227
228 return this;
229 }
230
231 /**
232 * Sets the number of packet received.
233 *
234 * @param packets number of packets received
235 * @return builder object
236 */
237 public Builder setPacketsReceived(long packets) {
238 packetsReceived = packets;
239
240 return this;
241 }
242
243 /**
244 * Sets the number of packets sent.
245 *
246 * @param packets number of packets sent
247 * @return builder object
248 */
249 public Builder setPacketsSent(long packets) {
250 packetsSent = packets;
251
252 return this;
253 }
254
255 /**
256 * Sets the number of received bytes.
257 *
258 * @param bytes number of received bytes.
259 * @return builder object
260 */
261 public Builder setBytesReceived(long bytes) {
262 bytesReceived = bytes;
263
264 return this;
265 }
266
267 /**
268 * Sets the number of sent bytes.
269 *
270 * @param bytes number of sent bytes
271 * @return builder object
272 */
273 public Builder setBytesSent(long bytes) {
274 bytesSent = bytes;
275
276 return this;
277 }
278
279 /**
280 * Sets the number of packets dropped by RX.
281 *
282 * @param packets number of packets dropped by RX
283 * @return builder object
284 */
285 public Builder setPacketsRxDropped(long packets) {
286 packetsRxDropped = packets;
287
288 return this;
289 }
290
291 /**
292 * Sets the number of packets dropped by TX.
293 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -0700294 * @param packets number of packets
sangho538108b2015-04-08 14:29:20 -0700295 * @return builder object
296 */
297 public Builder setPacketsTxDropped(long packets) {
298 packetsTxDropped = packets;
299
300 return this;
301 }
302
303 /**
304 * Sets the number of receive errors.
305 *
306 * @param packets number of receive errors
307 * @return builder object
308 */
309 public Builder setPacketsRxErrors(long packets) {
310 packetsRxErrors = packets;
311
312 return this;
313 }
314
315 /**
316 * Sets the number of transmit errors.
317 *
318 * @param packets number of transmit errors
319 * @return builder object
320 */
321 public Builder setPacketsTxErrors(long packets) {
322 packetsTxErrors = packets;
323
324 return this;
325 }
326
327 /**
328 * Sets the time port has been alive in seconds.
329 *
330 * @param sec time port has been alive in seconds
331 * @return builder object
332 */
333 public Builder setDurationSec(long sec) {
334 durationSec = sec;
335
336 return this;
337 }
338
339 /**
340 * Sets the time port has been alive in nano seconds.
341 *
342 * @param nano time port has been alive in nano seconds
343 * @return builder object
344 */
345 public Builder setDurationNano(long nano) {
346 durationNano = nano;
347
348 return this;
349 }
350
351 /**
352 * Creates a PortStatistics object.
353 *
354 * @return DefaultPortStatistics object
355 */
356 public DefaultPortStatistics build() {
357 return new DefaultPortStatistics(
358 deviceId,
Andrea Campanellac86154a2017-09-01 11:28:59 +0200359 portNumber,
sangho538108b2015-04-08 14:29:20 -0700360 packetsReceived,
361 packetsSent,
362 bytesReceived,
363 bytesSent,
364 packetsRxDropped,
365 packetsTxDropped,
366 packetsRxErrors,
367 packetsTxErrors,
368 durationSec,
369 durationNano);
370 }
371
372 }
Saurav Das59232cf2016-04-27 18:35:50 -0700373
sangho538108b2015-04-08 14:29:20 -0700374}