BGPCEP-685: Add Peer Release session rpc
[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 static java.util.Objects.requireNonNull;
11
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.atomic.LongAdder;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public final class PerTableTypeRouteCounter {
22     private static final Logger LOG = LoggerFactory.getLogger(PerTableTypeRouteCounter.class);
23
24     private final Map<TablesKey, LongAdder> counters = new ConcurrentHashMap<>();
25
26     public PerTableTypeRouteCounter(@Nonnull final Set<TablesKey> tablesKeySet) {
27         init(tablesKeySet);
28     }
29
30     public PerTableTypeRouteCounter() {
31     }
32
33     private synchronized void init(@Nonnull final Set<TablesKey> tablesKeySet) {
34         tablesKeySet.forEach(this::init);
35     }
36
37     public final synchronized LongAdder init(@Nonnull final TablesKey tablesKey) {
38         LongAdder counter = this.counters.get(requireNonNull(tablesKey));
39         if (counter == null) {
40             counter = new LongAdder();
41             this.counters.put(tablesKey, counter);
42         }
43         LOG.debug("Initializing route counter for tablesKey {}", tablesKey);
44         counter.reset();
45         return counter;
46     }
47
48     /**
49      * Get the counter for given tablesKey. Return an empty counter if it doesn't exist
50      * NOTE: the created empty counter won't be put into the original map
51      *
52      * @param tablesKey
53      */
54     @Nonnull public final LongAdder getCounterOrDefault(@Nonnull final TablesKey tablesKey) {
55         return this.counters.getOrDefault(requireNonNull(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      */
64     public final LongAdder getCounterOrSetDefault(@Nonnull final TablesKey tablesKey) {
65         if (!this.counters.containsKey(tablesKey)) {
66             return init(tablesKey);
67         }
68
69         return this.counters.get(requireNonNull(tablesKey));
70     }
71
72     public final Map<TablesKey, LongAdder> getCounters() {
73         return this.counters;
74     }
75
76     public final void resetAll() {
77         LOG.debug("Resetting all route counters..");
78         this.counters.values().forEach(LongAdder::reset);
79     }
80
81     public void setValueToCounterOrSetDefault(final TablesKey tablesKey, final int size) {
82         final LongAdder counter = getCounterOrSetDefault(tablesKey);
83         counter.reset();
84         counter.add(size);
85     }
86 }