blob: fccec73fe7f3a5b201cf5e3821575f148a56b033 [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
Andrea Campanellac86154a2017-09-01 11:28:59 +020095 public PortNumber portNumber() {
96 return this.portNumber;
sangho538108b2015-04-08 14:29:20 -070097 }
98
99 @Override
100 public long packetsReceived() {
101 return this.packetsReceived;
102 }
103
104 @Override
105 public long packetsSent() {
106 return this.packetsSent;
107 }
108
109 @Override
110 public long bytesReceived() {
111 return this.bytesReceived;
112 }
113
114 @Override
115 public long bytesSent() {
116 return this.bytesSent;
117 }
118
119 @Override
120 public long packetsRxDropped() {
121 return this.packetsRxDropped;
122 }
123
124 @Override
125 public long packetsTxDropped() {
126 return this.packetsTxDropped;
127 }
128
129 @Override
130 public long packetsRxErrors() {
131 return this.packetsRxErrors;
132 }
133
134 @Override
135 public long packetsTxErrors() {
136 return this.packetsTxErrors;
137 }
138
139 @Override
140 public long durationSec() {
141 return this.durationSec;
142 }
143
144 @Override
145 public long durationNano() {
146 return this.durationNano;
147 }
148
149 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700150 public boolean isZero() {
151 return bytesReceived() == 0 &&
152 bytesSent() == 0 &&
153 packetsReceived() == 0 &&
154 packetsRxDropped() == 0 &&
155 packetsSent() == 0 &&
156 packetsTxDropped() == 0;
157 }
158
159 @Override
sangho538108b2015-04-08 14:29:20 -0700160 public String toString() {
Sho SHIMIZU8d50c8d2016-08-12 17:29:02 -0700161 return "device: " + deviceId + ", " +
Andrea Campanellac86154a2017-09-01 11:28:59 +0200162 "port: " + this.portNumber + ", " +
Sho SHIMIZU8d50c8d2016-08-12 17:29:02 -0700163 "pktRx: " + this.packetsReceived + ", " +
164 "pktTx: " + this.packetsSent + ", " +
165 "byteRx: " + this.bytesReceived + ", " +
166 "byteTx: " + this.bytesSent + ", " +
167 "pktRxErr: " + this.packetsRxErrors + ", " +
168 "pktTxErr: " + this.packetsTxErrors + ", " +
169 "pktRxDrp: " + this.packetsRxDropped + ", " +
Laszlo Papp7cf60372018-01-11 00:06:43 +0000170 "pktTxDrp: " + this.packetsTxDropped + ", " +
171 "annotations: " + annotations();
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;
Laszlo Papp7cf60372018-01-11 00:06:43 +0000188 Annotations annotations;
sangho538108b2015-04-08 14:29:20 -0700189
190 private Builder() {
191
192 }
193
194 /**
195 * Sets port number.
196 *
Andrea Campanellac86154a2017-09-01 11:28:59 +0200197 * @param portNumber port number
198 * @return builder object
199 */
200 public Builder setPort(PortNumber portNumber) {
201 this.portNumber = portNumber;
sangho538108b2015-04-08 14:29:20 -0700202
203 return this;
204 }
205
206 /**
207 * Sets the device identifier.
208 *
209 * @param deviceId device identifier
210 * @return builder object
211 */
212 public Builder setDeviceId(DeviceId deviceId) {
213 this.deviceId = deviceId;
214
215 return this;
216 }
217
218 /**
219 * Sets the number of packet received.
220 *
221 * @param packets number of packets received
222 * @return builder object
223 */
224 public Builder setPacketsReceived(long packets) {
225 packetsReceived = packets;
226
227 return this;
228 }
229
230 /**
231 * Sets the number of packets sent.
232 *
233 * @param packets number of packets sent
234 * @return builder object
235 */
236 public Builder setPacketsSent(long packets) {
237 packetsSent = packets;
238
239 return this;
240 }
241
242 /**
243 * Sets the number of received bytes.
244 *
245 * @param bytes number of received bytes.
246 * @return builder object
247 */
248 public Builder setBytesReceived(long bytes) {
249 bytesReceived = bytes;
250
251 return this;
252 }
253
254 /**
255 * Sets the number of sent bytes.
256 *
257 * @param bytes number of sent bytes
258 * @return builder object
259 */
260 public Builder setBytesSent(long bytes) {
261 bytesSent = bytes;
262
263 return this;
264 }
265
266 /**
267 * Sets the number of packets dropped by RX.
268 *
269 * @param packets number of packets dropped by RX
270 * @return builder object
271 */
272 public Builder setPacketsRxDropped(long packets) {
273 packetsRxDropped = packets;
274
275 return this;
276 }
277
278 /**
279 * Sets the number of packets dropped by TX.
280 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -0700281 * @param packets number of packets
sangho538108b2015-04-08 14:29:20 -0700282 * @return builder object
283 */
284 public Builder setPacketsTxDropped(long packets) {
285 packetsTxDropped = packets;
286
287 return this;
288 }
289
290 /**
291 * Sets the number of receive errors.
292 *
293 * @param packets number of receive errors
294 * @return builder object
295 */
296 public Builder setPacketsRxErrors(long packets) {
297 packetsRxErrors = packets;
298
299 return this;
300 }
301
302 /**
303 * Sets the number of transmit errors.
304 *
305 * @param packets number of transmit errors
306 * @return builder object
307 */
308 public Builder setPacketsTxErrors(long packets) {
309 packetsTxErrors = packets;
310
311 return this;
312 }
313
314 /**
315 * Sets the time port has been alive in seconds.
316 *
317 * @param sec time port has been alive in seconds
318 * @return builder object
319 */
320 public Builder setDurationSec(long sec) {
321 durationSec = sec;
322
323 return this;
324 }
325
326 /**
327 * Sets the time port has been alive in nano seconds.
328 *
329 * @param nano time port has been alive in nano seconds
330 * @return builder object
331 */
332 public Builder setDurationNano(long nano) {
333 durationNano = nano;
334
335 return this;
336 }
337
338 /**
Laszlo Papp7cf60372018-01-11 00:06:43 +0000339 * Sets the annotations.
340 *
341 * @param anns annotations
342 * @return builder object
343 */
344 public Builder setAnnotations(Annotations anns) {
345 annotations = anns;
346
347 return this;
348 }
349
350 /**
sangho538108b2015-04-08 14:29:20 -0700351 * Creates a PortStatistics object.
352 *
353 * @return DefaultPortStatistics object
354 */
355 public DefaultPortStatistics build() {
356 return new DefaultPortStatistics(
357 deviceId,
Andrea Campanellac86154a2017-09-01 11:28:59 +0200358 portNumber,
sangho538108b2015-04-08 14:29:20 -0700359 packetsReceived,
360 packetsSent,
361 bytesReceived,
362 bytesSent,
363 packetsRxDropped,
364 packetsTxDropped,
365 packetsRxErrors,
366 packetsTxErrors,
367 durationSec,
Laszlo Papp7cf60372018-01-11 00:06:43 +0000368 durationNano,
369 annotations);
sangho538108b2015-04-08 14:29:20 -0700370 }
371
372 }
Saurav Das59232cf2016-04-27 18:35:50 -0700373
sangho538108b2015-04-08 14:29:20 -0700374}