07bdf06f8e7a08a7d7f8efad930c2d94465d18de
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / stats / peer / route / PerTableTypeRouteCounter.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.impl.stats.peer.route;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Map;
12 import java.util.Set;
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;
19
20 public final class PerTableTypeRouteCounter {
21     private static final Logger LOG = LoggerFactory.getLogger(PerTableTypeRouteCounter.class);
22
23     private final Map<TablesKey, UnsignedInt32Counter> counters = new ConcurrentHashMap<>();
24     private final String counterName;
25
26     private final UnsignedInt32Counter createCounter(@Nonnull final TablesKey tablesKey) {
27         return new UnsignedInt32Counter(this.counterName + tablesKey.toString());
28     }
29
30     public PerTableTypeRouteCounter(@Nonnull final String counterName) {
31         this.counterName = Preconditions.checkNotNull(counterName);
32     }
33
34     public PerTableTypeRouteCounter(@Nonnull final String counterName, @Nonnull final Set<TablesKey> tablesKeySet) {
35         this(counterName);
36         init(tablesKeySet);
37     }
38
39     public final synchronized void init(@Nonnull Set<TablesKey> tablesKeySet) {
40         tablesKeySet.stream().forEach(tablesKey -> init(tablesKey));
41     }
42
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));
47         }
48         LOG.debug("Initializing route counter for tablesKey {}", tablesKey);
49         counter.resetCount();
50         return counter;
51     }
52
53     /**
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
56      * @param tablesKey
57      * @return
58      */
59     @Nonnull public final UnsignedInt32Counter getCounterOrDefault(@Nonnull final TablesKey tablesKey) {
60         return this.counters.getOrDefault(Preconditions.checkNotNull(tablesKey), createCounter(tablesKey));
61     }
62
63     /**
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
66      * @param tablesKey
67      * @return
68      */
69     public final UnsignedInt32Counter getCounterOrSetDefault(@Nonnull final TablesKey tablesKey) {
70         if (!this.counters.containsKey(tablesKey)) {
71             return init(tablesKey);
72         } else {
73             return this.counters.get(Preconditions.checkNotNull(tablesKey));
74         }
75     }
76
77     public final Map<TablesKey, UnsignedInt32Counter> getCounters() {
78         return this.counters;
79     }
80
81     public final void resetAll() {
82         LOG.debug("Resetting all route counters..");
83         this.counters.values().stream().forEach(v -> v.resetCount());
84     }
85 }