Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [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 | |
| 17 | package org.onosproject.net.behaviour; |
| 18 | |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 19 | import java.util.Map; |
| 20 | import java.util.Objects; |
| 21 | |
Ray Milkey | 201f04b | 2017-09-25 10:13:19 -0700 | [diff] [blame] | 22 | import com.google.common.base.MoreObjects; |
| 23 | |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 24 | /** |
| 25 | * Represents statistics associated to a mirroring. |
| 26 | */ |
| 27 | public final class MirroringStatistics { |
| 28 | |
Ray Milkey | 201f04b | 2017-09-25 10:13:19 -0700 | [diff] [blame] | 29 | private final MirroringName mirroringName; |
| 30 | private final long txBytes; |
| 31 | private final long txPackets; |
| 32 | |
| 33 | /** |
| 34 | * Hide private constructor to prevent calls to new(). |
| 35 | */ |
| 36 | private MirroringStatistics() { |
Ray Milkey | 0c484a7 | 2017-09-27 17:25:45 -0700 | [diff] [blame] | 37 | mirroringName = null; |
| 38 | txBytes = 0; |
| 39 | txPackets = 0; |
Ray Milkey | 201f04b | 2017-09-25 10:13:19 -0700 | [diff] [blame] | 40 | } |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Statistics associated to a named mirroring. |
| 44 | * |
| 45 | * @param name the name of the mirroring |
| 46 | * @param bytes transmitted bytes |
| 47 | * @param packets transmitted packets |
| 48 | */ |
Ray Milkey | 201f04b | 2017-09-25 10:13:19 -0700 | [diff] [blame] | 49 | private MirroringStatistics(String name, long bytes, long packets) { |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 50 | this.mirroringName = MirroringName.mirroringName(name); |
| 51 | this.txBytes = bytes; |
| 52 | this.txPackets = packets; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * |
| 57 | * Creates a MirroringStatistics using the supplied information. |
| 58 | * |
| 59 | * @param name the name of the mirroring |
| 60 | * @param statistics the associated statistics |
| 61 | * @return the MirroringStatistics object |
| 62 | */ |
| 63 | public static MirroringStatistics mirroringStatistics(String name, Map<String, Integer> statistics) { |
| 64 | return new MirroringStatistics(name, statistics.get("tx_bytes"), statistics.get("tx_packets")); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the mirroring name string. |
| 69 | * |
| 70 | * @return name string |
| 71 | */ |
| 72 | public MirroringName name() { |
| 73 | return mirroringName; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the transmitted bytes. |
| 78 | * |
| 79 | * @return the bytes |
| 80 | */ |
| 81 | public long bytes() { |
| 82 | return txBytes; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the transmitted packtes. |
| 87 | * |
| 88 | * @return the packets |
| 89 | */ |
Ray Milkey | 201f04b | 2017-09-25 10:13:19 -0700 | [diff] [blame] | 90 | public long packets() { |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 91 | return txPackets; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public int hashCode() { |
| 96 | return Objects.hash(name().name(), txBytes, txPackets); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean equals(Object obj) { |
| 101 | if (this == obj) { |
| 102 | return true; |
| 103 | } |
| 104 | if (obj instanceof MirroringStatistics) { |
| 105 | final MirroringStatistics that = (MirroringStatistics) obj; |
| 106 | return this.getClass() == that.getClass() && |
| 107 | Objects.equals(this.mirroringName, that.mirroringName) && |
| 108 | Objects.equals(this.txBytes, that.txBytes) && |
| 109 | Objects.equals(this.txPackets, that.txPackets); |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public String toString() { |
| 116 | return MoreObjects.toStringHelper(getClass()) |
| 117 | .add("name", name()) |
| 118 | .add("tx_bytes", bytes()) |
Ray Milkey | 201f04b | 2017-09-25 10:13:19 -0700 | [diff] [blame] | 119 | .add("tx_packets", packets()) |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 120 | .toString(); |
| 121 | } |
| 122 | |
| 123 | } |