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 | |
Laszlo Papp | 7cf6037 | 2018-01-11 00:06:43 +0000 | [diff] [blame] | 18 | import org.onosproject.net.AbstractAnnotated; |
| 19 | import org.onosproject.net.Annotations; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 21 | import org.onosproject.net.PortNumber; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Default implementation of immutable port statistics. |
| 25 | */ |
Laszlo Papp | 7cf6037 | 2018-01-11 00:06:43 +0000 | [diff] [blame] | 26 | public final class DefaultPortStatistics extends AbstractAnnotated implements PortStatistics { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 27 | |
| 28 | private final DeviceId deviceId; |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 29 | private final PortNumber portNumber; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 30 | 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 Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 42 | PortNumber portNumber, |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 43 | 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 Papp | 7cf6037 | 2018-01-11 00:06:43 +0000 | [diff] [blame] | 52 | long durationNano, |
| 53 | Annotations annotations) { |
| 54 | super(annotations); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 55 | this.deviceId = deviceId; |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 56 | this.portNumber = portNumber; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 57 | 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 Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 69 | // Constructor for serializer |
| 70 | private DefaultPortStatistics() { |
| 71 | this.deviceId = null; |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 72 | this.portNumber = null; |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 73 | 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 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 85 | /** |
| 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 Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 95 | public PortNumber portNumber() { |
| 96 | return this.portNumber; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 97 | } |
| 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 Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 150 | 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 |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 160 | public String toString() { |
Sho SHIMIZU | 8d50c8d | 2016-08-12 17:29:02 -0700 | [diff] [blame] | 161 | return "device: " + deviceId + ", " + |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 162 | "port: " + this.portNumber + ", " + |
Sho SHIMIZU | 8d50c8d | 2016-08-12 17:29:02 -0700 | [diff] [blame] | 163 | "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 Papp | 7cf6037 | 2018-01-11 00:06:43 +0000 | [diff] [blame] | 170 | "pktTxDrp: " + this.packetsTxDropped + ", " + |
| 171 | "annotations: " + annotations(); |
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; |
Laszlo Papp | 7cf6037 | 2018-01-11 00:06:43 +0000 | [diff] [blame] | 188 | Annotations annotations; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 189 | |
| 190 | private Builder() { |
| 191 | |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Sets port number. |
| 196 | * |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 197 | * @param portNumber port number |
| 198 | * @return builder object |
| 199 | */ |
| 200 | public Builder setPort(PortNumber portNumber) { |
| 201 | this.portNumber = portNumber; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 202 | |
| 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 Vachuska | e10f56b | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 281 | * @param packets number of packets |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 282 | * @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 Papp | 7cf6037 | 2018-01-11 00:06:43 +0000 | [diff] [blame] | 339 | * 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 | /** |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 351 | * Creates a PortStatistics object. |
| 352 | * |
| 353 | * @return DefaultPortStatistics object |
| 354 | */ |
| 355 | public DefaultPortStatistics build() { |
| 356 | return new DefaultPortStatistics( |
| 357 | deviceId, |
Andrea Campanella | c86154a | 2017-09-01 11:28:59 +0200 | [diff] [blame] | 358 | portNumber, |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 359 | packetsReceived, |
| 360 | packetsSent, |
| 361 | bytesReceived, |
| 362 | bytesSent, |
| 363 | packetsRxDropped, |
| 364 | packetsTxDropped, |
| 365 | packetsRxErrors, |
| 366 | packetsTxErrors, |
| 367 | durationSec, |
Laszlo Papp | 7cf6037 | 2018-01-11 00:06:43 +0000 | [diff] [blame] | 368 | durationNano, |
| 369 | annotations); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 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 | } |