blob: 03828332a6a91dd9b400ac022746d322f8ad22c5 [file] [log] [blame]
sangho538108b2015-04-08 14:29:20 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
64 /**
65 * Creates a builder for DefaultPortStatistics object.
66 *
67 * @return builder object for DefaultPortStatistics object
68 */
69 public static DefaultPortStatistics.Builder builder() {
70 return new Builder();
71 }
72
73 @Override
74 public int port() {
75 return this.port;
76 }
77
78 @Override
79 public long packetsReceived() {
80 return this.packetsReceived;
81 }
82
83 @Override
84 public long packetsSent() {
85 return this.packetsSent;
86 }
87
88 @Override
89 public long bytesReceived() {
90 return this.bytesReceived;
91 }
92
93 @Override
94 public long bytesSent() {
95 return this.bytesSent;
96 }
97
98 @Override
99 public long packetsRxDropped() {
100 return this.packetsRxDropped;
101 }
102
103 @Override
104 public long packetsTxDropped() {
105 return this.packetsTxDropped;
106 }
107
108 @Override
109 public long packetsRxErrors() {
110 return this.packetsRxErrors;
111 }
112
113 @Override
114 public long packetsTxErrors() {
115 return this.packetsTxErrors;
116 }
117
118 @Override
119 public long durationSec() {
120 return this.durationSec;
121 }
122
123 @Override
124 public long durationNano() {
125 return this.durationNano;
126 }
127
128 @Override
129 public String toString() {
130 StringBuilder sb = new StringBuilder("device: " + deviceId + ", ");
131
132 sb.append("port: " + this.port + ", ");
133 sb.append("pktRx: " + this.packetsReceived + ", ");
134 sb.append("pktTx: " + this.packetsSent + ", ");
135 sb.append("byteRx: " + this.bytesReceived + ", ");
136 sb.append("byteTx: " + this.bytesSent + ", ");
137 sb.append("pktRxErr: " + this.packetsRxErrors + ", ");
138 sb.append("pktTxErr: " + this.packetsTxErrors + ", ");
139 sb.append("pktRxDrp: " + this.packetsRxDropped + ", ");
140 sb.append("pktTxDrp: " + this.packetsTxDropped);
141
142 return sb.toString();
143 }
144
145 public static final class Builder {
146
147 DeviceId deviceId;
148 int port;
149 long packetsReceived;
150 long packetsSent;
151 long bytesReceived;
152 long bytesSent;
153 long packetsRxDropped;
154 long packetsTxDropped;
155 long packetsRxErrors;
156 long packetsTxErrors;
157 long durationSec;
158 long durationNano;
159
160 private Builder() {
161
162 }
163
164 /**
165 * Sets port number.
166 *
167 * @param port port number
168 * @return builder object
169 */
170 public Builder setPort(int port) {
171 this.port = port;
172
173 return this;
174 }
175
176 /**
177 * Sets the device identifier.
178 *
179 * @param deviceId device identifier
180 * @return builder object
181 */
182 public Builder setDeviceId(DeviceId deviceId) {
183 this.deviceId = deviceId;
184
185 return this;
186 }
187
188 /**
189 * Sets the number of packet received.
190 *
191 * @param packets number of packets received
192 * @return builder object
193 */
194 public Builder setPacketsReceived(long packets) {
195 packetsReceived = packets;
196
197 return this;
198 }
199
200 /**
201 * Sets the number of packets sent.
202 *
203 * @param packets number of packets sent
204 * @return builder object
205 */
206 public Builder setPacketsSent(long packets) {
207 packetsSent = packets;
208
209 return this;
210 }
211
212 /**
213 * Sets the number of received bytes.
214 *
215 * @param bytes number of received bytes.
216 * @return builder object
217 */
218 public Builder setBytesReceived(long bytes) {
219 bytesReceived = bytes;
220
221 return this;
222 }
223
224 /**
225 * Sets the number of sent bytes.
226 *
227 * @param bytes number of sent bytes
228 * @return builder object
229 */
230 public Builder setBytesSent(long bytes) {
231 bytesSent = bytes;
232
233 return this;
234 }
235
236 /**
237 * Sets the number of packets dropped by RX.
238 *
239 * @param packets number of packets dropped by RX
240 * @return builder object
241 */
242 public Builder setPacketsRxDropped(long packets) {
243 packetsRxDropped = packets;
244
245 return this;
246 }
247
248 /**
249 * Sets the number of packets dropped by TX.
250 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -0700251 * @param packets number of packets
sangho538108b2015-04-08 14:29:20 -0700252 * @return builder object
253 */
254 public Builder setPacketsTxDropped(long packets) {
255 packetsTxDropped = packets;
256
257 return this;
258 }
259
260 /**
261 * Sets the number of receive errors.
262 *
263 * @param packets number of receive errors
264 * @return builder object
265 */
266 public Builder setPacketsRxErrors(long packets) {
267 packetsRxErrors = packets;
268
269 return this;
270 }
271
272 /**
273 * Sets the number of transmit errors.
274 *
275 * @param packets number of transmit errors
276 * @return builder object
277 */
278 public Builder setPacketsTxErrors(long packets) {
279 packetsTxErrors = packets;
280
281 return this;
282 }
283
284 /**
285 * Sets the time port has been alive in seconds.
286 *
287 * @param sec time port has been alive in seconds
288 * @return builder object
289 */
290 public Builder setDurationSec(long sec) {
291 durationSec = sec;
292
293 return this;
294 }
295
296 /**
297 * Sets the time port has been alive in nano seconds.
298 *
299 * @param nano time port has been alive in nano seconds
300 * @return builder object
301 */
302 public Builder setDurationNano(long nano) {
303 durationNano = nano;
304
305 return this;
306 }
307
308 /**
309 * Creates a PortStatistics object.
310 *
311 * @return DefaultPortStatistics object
312 */
313 public DefaultPortStatistics build() {
314 return new DefaultPortStatistics(
315 deviceId,
316 port,
317 packetsReceived,
318 packetsSent,
319 bytesReceived,
320 bytesSent,
321 packetsRxDropped,
322 packetsTxDropped,
323 packetsRxErrors,
324 packetsTxErrors,
325 durationSec,
326 durationNano);
327 }
328
329 }
330}