blob: 633995f374ca728ec3dbf04f135b5b4e6a2db0cc [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() {
37 // This should never happen
38 throw new UnsupportedOperationException();
39 }
Pier Ventref5d72362016-07-17 12:02:14 +020040
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 Milkey201f04b2017-09-25 10:13:19 -070048 private MirroringStatistics(String name, long bytes, long packets) {
Pier Ventref5d72362016-07-17 12:02:14 +020049 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 Milkey201f04b2017-09-25 10:13:19 -070089 public long packets() {
Pier Ventref5d72362016-07-17 12:02:14 +020090 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 Milkey201f04b2017-09-25 10:13:19 -0700118 .add("tx_packets", packets())
Pier Ventref5d72362016-07-17 12:02:14 +0200119 .toString();
120 }
121
122}