blob: ee0cb0659be09913bec8528302cc470b974a3036 [file] [log] [blame]
sangho538108b2015-04-08 14:29:20 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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() {
156 StringBuilder sb = new StringBuilder("device: " + deviceId + ", ");
157
158 sb.append("port: " + this.port + ", ");
159 sb.append("pktRx: " + this.packetsReceived + ", ");
160 sb.append("pktTx: " + this.packetsSent + ", ");
161 sb.append("byteRx: " + this.bytesReceived + ", ");
162 sb.append("byteTx: " + this.bytesSent + ", ");
163 sb.append("pktRxErr: " + this.packetsRxErrors + ", ");
164 sb.append("pktTxErr: " + this.packetsTxErrors + ", ");
165 sb.append("pktRxDrp: " + this.packetsRxDropped + ", ");
166 sb.append("pktTxDrp: " + this.packetsTxDropped);
167
168 return sb.toString();
169 }
170
171 public static final class Builder {
172
173 DeviceId deviceId;
174 int port;
175 long packetsReceived;
176 long packetsSent;
177 long bytesReceived;
178 long bytesSent;
179 long packetsRxDropped;
180 long packetsTxDropped;
181 long packetsRxErrors;
182 long packetsTxErrors;
183 long durationSec;
184 long durationNano;
185
186 private Builder() {
187
188 }
189
190 /**
191 * Sets port number.
192 *
193 * @param port port number
194 * @return builder object
195 */
196 public Builder setPort(int port) {
197 this.port = port;
198
199 return this;
200 }
201
202 /**
203 * Sets the device identifier.
204 *
205 * @param deviceId device identifier
206 * @return builder object
207 */
208 public Builder setDeviceId(DeviceId deviceId) {
209 this.deviceId = deviceId;
210
211 return this;
212 }
213
214 /**
215 * Sets the number of packet received.
216 *
217 * @param packets number of packets received
218 * @return builder object
219 */
220 public Builder setPacketsReceived(long packets) {
221 packetsReceived = packets;
222
223 return this;
224 }
225
226 /**
227 * Sets the number of packets sent.
228 *
229 * @param packets number of packets sent
230 * @return builder object
231 */
232 public Builder setPacketsSent(long packets) {
233 packetsSent = packets;
234
235 return this;
236 }
237
238 /**
239 * Sets the number of received bytes.
240 *
241 * @param bytes number of received bytes.
242 * @return builder object
243 */
244 public Builder setBytesReceived(long bytes) {
245 bytesReceived = bytes;
246
247 return this;
248 }
249
250 /**
251 * Sets the number of sent bytes.
252 *
253 * @param bytes number of sent bytes
254 * @return builder object
255 */
256 public Builder setBytesSent(long bytes) {
257 bytesSent = bytes;
258
259 return this;
260 }
261
262 /**
263 * Sets the number of packets dropped by RX.
264 *
265 * @param packets number of packets dropped by RX
266 * @return builder object
267 */
268 public Builder setPacketsRxDropped(long packets) {
269 packetsRxDropped = packets;
270
271 return this;
272 }
273
274 /**
275 * Sets the number of packets dropped by TX.
276 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -0700277 * @param packets number of packets
sangho538108b2015-04-08 14:29:20 -0700278 * @return builder object
279 */
280 public Builder setPacketsTxDropped(long packets) {
281 packetsTxDropped = packets;
282
283 return this;
284 }
285
286 /**
287 * Sets the number of receive errors.
288 *
289 * @param packets number of receive errors
290 * @return builder object
291 */
292 public Builder setPacketsRxErrors(long packets) {
293 packetsRxErrors = packets;
294
295 return this;
296 }
297
298 /**
299 * Sets the number of transmit errors.
300 *
301 * @param packets number of transmit errors
302 * @return builder object
303 */
304 public Builder setPacketsTxErrors(long packets) {
305 packetsTxErrors = packets;
306
307 return this;
308 }
309
310 /**
311 * Sets the time port has been alive in seconds.
312 *
313 * @param sec time port has been alive in seconds
314 * @return builder object
315 */
316 public Builder setDurationSec(long sec) {
317 durationSec = sec;
318
319 return this;
320 }
321
322 /**
323 * Sets the time port has been alive in nano seconds.
324 *
325 * @param nano time port has been alive in nano seconds
326 * @return builder object
327 */
328 public Builder setDurationNano(long nano) {
329 durationNano = nano;
330
331 return this;
332 }
333
334 /**
335 * Creates a PortStatistics object.
336 *
337 * @return DefaultPortStatistics object
338 */
339 public DefaultPortStatistics build() {
340 return new DefaultPortStatistics(
341 deviceId,
342 port,
343 packetsReceived,
344 packetsSent,
345 bytesReceived,
346 bytesSent,
347 packetsRxDropped,
348 packetsTxDropped,
349 packetsRxErrors,
350 packetsTxErrors,
351 durationSec,
352 durationNano);
353 }
354
355 }
Saurav Das59232cf2016-04-27 18:35:50 -0700356
sangho538108b2015-04-08 14:29:20 -0700357}