sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.net.device; |
| 17 | |
| 18 | import org.onosproject.net.DeviceId; |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 19 | import org.onosproject.net.PortNumber; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Default implementation of immutable port statistics. |
| 23 | */ |
| 24 | public final class DefaultPortStatistics implements PortStatistics { |
| 25 | |
| 26 | private final DeviceId deviceId; |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 27 | private final PortNumber portNumber; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 28 | 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 Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 40 | PortNumber portNumber, |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 41 | 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 Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 52 | this.portNumber = portNumber; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 53 | 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 Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 65 | // Constructor for serializer |
| 66 | private DefaultPortStatistics() { |
| 67 | this.deviceId = null; |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 68 | this.portNumber = null; |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 69 | 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 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 81 | /** |
| 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 Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 92 | return (int) this.portNumber.toLong(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public PortNumber portNumber() { |
| 97 | return this.portNumber; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 98 | } |
| 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 Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 151 | 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 |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 161 | public String toString() { |
Sho SHIMIZU | 8d50c8d | 2016-08-12 17:29:02 -0700 | [diff] [blame] | 162 | return "device: " + deviceId + ", " + |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 163 | "port: " + this.portNumber + ", " + |
Sho SHIMIZU | 8d50c8d | 2016-08-12 17:29:02 -0700 | [diff] [blame] | 164 | "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; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | public static final class Builder { |
| 175 | |
| 176 | DeviceId deviceId; |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 177 | PortNumber portNumber; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 178 | 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 Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 198 | * @deprecated ONOS 1.12 Magpie |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 199 | */ |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 200 | @Deprecated |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 201 | public Builder setPort(int port) { |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 202 | 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; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 215 | |
| 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 Vachuska | e10f56b | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 294 | * @param packets number of packets |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 295 | * @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 Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 359 | portNumber, |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 360 | packetsReceived, |
| 361 | packetsSent, |
| 362 | bytesReceived, |
| 363 | bytesSent, |
| 364 | packetsRxDropped, |
| 365 | packetsTxDropped, |
| 366 | packetsRxErrors, |
| 367 | packetsTxErrors, |
| 368 | durationSec, |
| 369 | durationNano); |
| 370 | } |
| 371 | |
| 372 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 373 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 374 | } |