BUG-5032: BGP Operational State
[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 java.util.concurrent.atomic.LongAdder;
15 import javax.annotation.Nonnull;
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, LongAdder> counters = new ConcurrentHashMap<>();
24
25     public PerTableTypeRouteCounter(@Nonnull final Set<TablesKey> tablesKeySet) {
26         init(tablesKeySet);
27     }
28
29     public PerTableTypeRouteCounter() {
30     }
31
32     private synchronized void init(@Nonnull Set<TablesKey> tablesKeySet) {
33         tablesKeySet.forEach(this::init);
34     }
35
36     public final synchronized LongAdder init(@Nonnull final TablesKey tablesKey) {
37         LongAdder counter = this.counters.get(Preconditions.checkNotNull(tablesKey));
38         if (counter == null) {
39             counter = new LongAdder();
40             this.counters.put(tablesKey, counter);
41         }
42         LOG.debug("Initializing route counter for tablesKey {}", tablesKey);
43         counter.reset();
44         return counter;
45     }
46
47     /**
48      * Get the counter for given tablesKey. Return an empty counter if it doesn't exist
49      * NOTE: the created empty counter won't be put into the original map
50      *
51      * @param tablesKey
52      * @return
53      */
54     @Nonnull public final LongAdder getCounterOrDefault(@Nonnull final TablesKey tablesKey) {
55         return this.counters.getOrDefault(Preconditions.checkNotNull(tablesKey), new LongAdder());
56     }
57
58     /**
59      * Get the counter with given tablesKey.  Create an empty counter if it doesn't exist
60      * This method will put the created empty counter back to map
61      *
62      * @param tablesKey
63      * @return
64      */
65     public final LongAdder getCounterOrSetDefault(@Nonnull final TablesKey tablesKey) {
66         if (!this.counters.containsKey(tablesKey)) {
67             return init(tablesKey);
68         } else {
69             return this.counters.get(Preconditions.checkNotNull(tablesKey));
70         }
71     }
72
73     public final Map<TablesKey, LongAdder> getCounters() {
74         return this.counters;
75     }
76
77     public final void resetAll() {
78         LOG.debug("Resetting all route counters..");
79         this.counters.values().forEach(LongAdder::reset);
80     }
81
82     public void setValueToCounterOrSetDefault(final TablesKey tablesKey, final int size) {
83         final LongAdder counter = getCounterOrSetDefault(tablesKey);
84         counter.reset();
85         counter.add(size);
86     }
87 }