blob: b9aae84d7c56b11d93c783c05d8e4175faae47c6 [file] [log] [blame]
Pier Ventref5d72362016-07-17 12:02:14 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Pier Ventref5d72362016-07-17 12:02:14 +02003 *
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
17package org.onosproject.net.behaviour;
18
Pier Ventref5d72362016-07-17 12:02:14 +020019import java.util.Map;
20import java.util.Objects;
21
Ray Milkey201f04b2017-09-25 10:13:19 -070022import com.google.common.base.MoreObjects;
23
Pier Ventref5d72362016-07-17 12:02:14 +020024/**
25 * Represents statistics associated to a mirroring.
26 */
27public final class MirroringStatistics {
28
Ray Milkey201f04b2017-09-25 10:13:19 -070029 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 Milkey0c484a72017-09-27 17:25:45 -070037 mirroringName = null;
38 txBytes = 0;
39 txPackets = 0;
Ray Milkey201f04b2017-09-25 10:13:19 -070040 }
Pier Ventref5d72362016-07-17 12:02:14 +020041
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 Milkey201f04b2017-09-25 10:13:19 -070049 private MirroringStatistics(String name, long bytes, long packets) {
Pier Ventref5d72362016-07-17 12:02:14 +020050 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 Milkey201f04b2017-09-25 10:13:19 -070090 public long packets() {
Pier Ventref5d72362016-07-17 12:02:14 +020091 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 Milkey201f04b2017-09-25 10:13:19 -0700119 .add("tx_packets", packets())
Pier Ventref5d72362016-07-17 12:02:14 +0200120 .toString();
121 }
122
123}