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