Replace UnsignedInt32Counter by LongAdder
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / stats / peer / BGPPeerStatsImpl.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;
9
10 import static org.opendaylight.protocol.bgp.rib.impl.CountersUtil.toZeroBasedCounter32;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Lists;
14 import java.util.List;
15 import java.util.Set;
16 import java.util.concurrent.atomic.LongAdder;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.config.api.IdentityAttributeRef;
19 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpPeerState;
20 import org.opendaylight.controller.config.yang.bgp.rib.impl.RouteTable;
21 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.route.PerTableTypeRouteCounter;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
23 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
24 import org.opendaylight.yangtools.yang.common.QName;
25
26 public final class BGPPeerStatsImpl implements BGPPeerStats {
27     private final Set<TablesKey> tablesKeySet;
28     private final PerTableTypeRouteCounter adjRibInRouteCounters;
29     private final PerTableTypeRouteCounter adjRibOutRouteCounters;
30     private final PerTableTypeRouteCounter effectiveRibInRouteCounters;
31     private final LongAdder sessionEstablishedCounter = new LongAdder();
32
33     public BGPPeerStatsImpl(@Nonnull final Set<TablesKey> tablesKeySet) {
34         this.tablesKeySet = Preconditions.checkNotNull(tablesKeySet);
35         this.adjRibInRouteCounters = new PerTableTypeRouteCounter(tablesKeySet);
36         this.adjRibOutRouteCounters = new PerTableTypeRouteCounter(tablesKeySet);
37         this.effectiveRibInRouteCounters = new PerTableTypeRouteCounter(tablesKeySet);
38     }
39
40     public PerTableTypeRouteCounter getAdjRibInRouteCounters() {
41         return adjRibInRouteCounters;
42     }
43
44     public PerTableTypeRouteCounter getAdjRibOutRouteCounters() {
45         return adjRibOutRouteCounters;
46     }
47
48     public PerTableTypeRouteCounter getEffectiveRibInRouteCounters() {
49         return effectiveRibInRouteCounters;
50     }
51
52     private RouteTable createRouteTable(@Nonnull final TablesKey tablesKey) {
53         Preconditions.checkNotNull(tablesKey);
54         final RouteTable routeTable = new RouteTable();
55         // FIXME: setTableType() is DEPRECATED, use setAfi() and setSafi() instead
56         routeTable.setTableType("afi=" + tablesKey.getAfi().getSimpleName() + ",safi=" + tablesKey.getSafi().getSimpleName());
57
58         final QName afiQName = BindingReflections.findQName(tablesKey.getAfi()).intern();
59         final QName safiQName = BindingReflections.findQName(tablesKey.getSafi()).intern();
60         routeTable.setAfi(new IdentityAttributeRef(afiQName.toString()));
61         routeTable.setSafi(new IdentityAttributeRef(safiQName.toString()));
62         // we want to get default counter in case particular route table is not initialized (e.g. adj-rib-out is not initialized in some cases)
63         routeTable.setAdjRibInRoutesCount(toZeroBasedCounter32(this.adjRibInRouteCounters.getCounterOrDefault(tablesKey)));
64         routeTable.setAdjRibOutRoutesCount(toZeroBasedCounter32(this.adjRibOutRouteCounters.getCounterOrDefault(tablesKey)));
65         routeTable.setEffectiveRibInRoutesCount(toZeroBasedCounter32(this.effectiveRibInRouteCounters.getCounterOrDefault(tablesKey)));
66
67         return routeTable;
68     }
69
70     @Override
71     public BgpPeerState getBgpPeerState() {
72         final BgpPeerState peerState = new BgpPeerState();
73         final List<RouteTable> routes = Lists.newArrayList();
74         this.tablesKeySet.forEach(tablesKey -> routes.add(createRouteTable(tablesKey)));
75         peerState.setRouteTable(routes);
76         peerState.setSessionEstablishedCount(toZeroBasedCounter32(this.sessionEstablishedCounter));
77         return peerState;
78     }
79
80     @Override
81     public LongAdder getSessionEstablishedCounter() {
82         return this.sessionEstablishedCounter;
83     }
84 }