Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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 | */ |
| 15 | |
| 16 | package org.opencord.dhcpl2relay.impl; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.Sets; |
| 21 | |
| 22 | import java.util.Map; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | /** |
| 26 | * Snapshot of DHCP L2 Relay statistics. |
| 27 | */ |
| 28 | public class DhcpL2RelayStatistics { |
| 29 | |
| 30 | private final ImmutableMap<DhcpL2RelayCountersIdentifier, Long> counters; |
| 31 | |
| 32 | private DhcpL2RelayStatistics(ImmutableMap<DhcpL2RelayCountersIdentifier, Long> counters) { |
| 33 | this.counters = counters; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Creates a new empty statistics instance. |
| 38 | */ |
| 39 | public DhcpL2RelayStatistics() { |
| 40 | counters = ImmutableMap.of(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Gets the value of the counter with the given ID. Defaults to 0 if counter is not present. |
| 45 | * |
| 46 | * @param id counter ID |
| 47 | * @return counter value |
| 48 | */ |
| 49 | public long get(DhcpL2RelayCountersIdentifier id) { |
| 50 | return counters.getOrDefault(id, 0L); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Gets the map of counters. |
| 55 | * |
| 56 | * @return map of counters |
| 57 | */ |
| 58 | public Map<DhcpL2RelayCountersIdentifier, Long> counters() { |
| 59 | return counters; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Creates a new statistics instance with the given counter values. |
| 64 | * |
| 65 | * @param counters counters |
| 66 | * @return statistics |
| 67 | */ |
| 68 | public static DhcpL2RelayStatistics withCounters(Map<DhcpL2RelayCountersIdentifier, Long> counters) { |
| 69 | ImmutableMap.Builder<DhcpL2RelayCountersIdentifier, Long> builder = ImmutableMap.builder(); |
| 70 | |
| 71 | counters.forEach(builder::put); |
| 72 | |
| 73 | return new DhcpL2RelayStatistics(builder.build()); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Adds the given statistics instance to this one (sums the common counters) and returns |
| 78 | * a new instance containing the result. |
| 79 | * |
| 80 | * @param other other instance |
| 81 | * @return result |
| 82 | */ |
| 83 | public DhcpL2RelayStatistics add(DhcpL2RelayStatistics other) { |
| 84 | ImmutableMap.Builder<DhcpL2RelayCountersIdentifier, Long> builder = ImmutableMap.builder(); |
| 85 | |
| 86 | Set<DhcpL2RelayCountersIdentifier> keys = Sets.newHashSet(other.counters.keySet()); |
| 87 | |
| 88 | counters.forEach((id, value) -> { |
| 89 | builder.put(id, value + other.counters.getOrDefault(id, 0L)); |
| 90 | keys.remove(id); |
| 91 | }); |
| 92 | |
| 93 | keys.forEach(i -> builder.put(i, other.counters.get(i))); |
| 94 | |
| 95 | return new DhcpL2RelayStatistics(builder.build()); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public String toString() { |
| 100 | MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(this.getClass()); |
| 101 | counters.forEach((id, v) -> helper.add(id.toString(), v)); |
| 102 | return helper.toString(); |
| 103 | } |
| 104 | } |