2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.rib.impl.stats.peer.route;
10 import com.google.common.base.Preconditions;
13 import java.util.concurrent.ConcurrentHashMap;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public final class PerTableTypeRouteCounter {
21 private static final Logger LOG = LoggerFactory.getLogger(PerTableTypeRouteCounter.class);
23 private final Map<TablesKey, UnsignedInt32Counter> counters = new ConcurrentHashMap<>();
24 private final String counterName;
26 private final UnsignedInt32Counter createCounter(@Nonnull final TablesKey tablesKey) {
27 return new UnsignedInt32Counter(this.counterName + tablesKey.toString());
30 public PerTableTypeRouteCounter(@Nonnull final String counterName) {
31 this.counterName = Preconditions.checkNotNull(counterName);
34 public PerTableTypeRouteCounter(@Nonnull final String counterName, @Nonnull final Set<TablesKey> tablesKeySet) {
39 public final synchronized void init(@Nonnull Set<TablesKey> tablesKeySet) {
40 tablesKeySet.stream().forEach(tablesKey -> init(tablesKey));
43 public final synchronized UnsignedInt32Counter init(@Nonnull final TablesKey tablesKey) {
44 UnsignedInt32Counter counter = this.counters.get(Preconditions.checkNotNull(tablesKey));
45 if (counter == null) {
46 this.counters.put(tablesKey, counter = createCounter(tablesKey));
48 LOG.debug("Initializing route counter for tablesKey {}", tablesKey);
54 * Get the counter for given tablesKey. Return an empty counter if it doesn't exist
55 * NOTE: the created empty counter won't be put into the original map
59 @Nonnull public final UnsignedInt32Counter getCounterOrDefault(@Nonnull final TablesKey tablesKey) {
60 return this.counters.getOrDefault(Preconditions.checkNotNull(tablesKey), createCounter(tablesKey));
64 * Get the counter with given tablesKey. Create an empty counter if it doesn't exist
65 * This method will put the created empty counter back to map
69 public final UnsignedInt32Counter getCounterOrSetDefault(@Nonnull final TablesKey tablesKey) {
70 if (!this.counters.containsKey(tablesKey)) {
71 return init(tablesKey);
73 return this.counters.get(Preconditions.checkNotNull(tablesKey));
77 public final Map<TablesKey, UnsignedInt32Counter> getCounters() {
81 public final void resetAll() {
82 LOG.debug("Resetting all route counters..");
83 this.counters.values().stream().forEach(v -> v.resetCount());