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() { |
| 37 | // This should never happen |
| 38 | throw new UnsupportedOperationException(); |
| 39 | } |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Statistics associated to a named mirroring. |
| 43 | * |
| 44 | * @param name the name of the mirroring |
| 45 | * @param bytes transmitted bytes |
| 46 | * @param packets transmitted packets |
| 47 | */ |
Ray Milkey | 201f04b | 2017-09-25 10:13:19 -0700 | [diff] [blame^] | 48 | private MirroringStatistics(String name, long bytes, long packets) { |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 49 | this.mirroringName = MirroringName.mirroringName(name); |
| 50 | this.txBytes = bytes; |
| 51 | this.txPackets = packets; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * |
| 56 | * Creates a MirroringStatistics using the supplied information. |
| 57 | * |
| 58 | * @param name the name of the mirroring |
| 59 | * @param statistics the associated statistics |
| 60 | * @return the MirroringStatistics object |
| 61 | */ |
| 62 | public static MirroringStatistics mirroringStatistics(String name, Map<String, Integer> statistics) { |
| 63 | return new MirroringStatistics(name, statistics.get("tx_bytes"), statistics.get("tx_packets")); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the mirroring name string. |
| 68 | * |
| 69 | * @return name string |
| 70 | */ |
| 71 | public MirroringName name() { |
| 72 | return mirroringName; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the transmitted bytes. |
| 77 | * |
| 78 | * @return the bytes |
| 79 | */ |
| 80 | public long bytes() { |
| 81 | return txBytes; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the transmitted packtes. |
| 86 | * |
| 87 | * @return the packets |
| 88 | */ |
Ray Milkey | 201f04b | 2017-09-25 10:13:19 -0700 | [diff] [blame^] | 89 | public long packets() { |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 90 | return txPackets; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public int hashCode() { |
| 95 | return Objects.hash(name().name(), txBytes, txPackets); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public boolean equals(Object obj) { |
| 100 | if (this == obj) { |
| 101 | return true; |
| 102 | } |
| 103 | if (obj instanceof MirroringStatistics) { |
| 104 | final MirroringStatistics that = (MirroringStatistics) obj; |
| 105 | return this.getClass() == that.getClass() && |
| 106 | Objects.equals(this.mirroringName, that.mirroringName) && |
| 107 | Objects.equals(this.txBytes, that.txBytes) && |
| 108 | Objects.equals(this.txPackets, that.txPackets); |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public String toString() { |
| 115 | return MoreObjects.toStringHelper(getClass()) |
| 116 | .add("name", name()) |
| 117 | .add("tx_bytes", bytes()) |
Ray Milkey | 201f04b | 2017-09-25 10:13:19 -0700 | [diff] [blame^] | 118 | .add("tx_packets", packets()) |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 119 | .toString(); |
| 120 | } |
| 121 | |
| 122 | } |