10a43a247ce9d18912949620c83b5249f19be2f4
[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 java.util.Objects.requireNonNull;
11 import static org.opendaylight.protocol.bgp.rib.impl.CountersUtil.toZeroBasedCounter32;
12
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.state.BGPPeerStateImpl;
22 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.route.PerTableTypeRouteCounter;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.ZeroBasedCounter32;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
25 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
26 import org.opendaylight.yangtools.yang.common.QName;
27
28 public final class BGPPeerStatsImpl implements BGPPeerStats {
29     private final Set<TablesKey> tablesKeySet;
30     private final PerTableTypeRouteCounter adjRibInRouteCounters;
31     private final PerTableTypeRouteCounter adjRibOutRouteCounters;
32     private final LongAdder sessionEstablishedCounter = new LongAdder();
33     private final BGPPeerStateImpl neighborState;
34
35     public BGPPeerStatsImpl(@Nonnull final String peerName, @Nonnull final Set<TablesKey> tablesKeySet,
36         @Nonnull final BGPPeerStateImpl neighborState) {
37         requireNonNull(peerName);
38         this.tablesKeySet = requireNonNull(tablesKeySet);
39         this.adjRibInRouteCounters = new PerTableTypeRouteCounter(tablesKeySet);
40         this.adjRibOutRouteCounters = new PerTableTypeRouteCounter(tablesKeySet);
41         this.neighborState = requireNonNull(neighborState);
42     }
43
44     public PerTableTypeRouteCounter getAdjRibInRouteCounters() {
45         return this.adjRibInRouteCounters;
46     }
47
48     public PerTableTypeRouteCounter getAdjRibOutRouteCounters() {
49         return this.adjRibOutRouteCounters;
50     }
51
52     private RouteTable createRouteTable(@Nonnull final TablesKey tablesKey) {
53         requireNonNull(tablesKey);
54         final RouteTable routeTable = new RouteTable();
55         final QName afiQName = BindingReflections.findQName(tablesKey.getAfi()).intern();
56         final QName safiQName = BindingReflections.findQName(tablesKey.getSafi()).intern();
57         routeTable.setAfi(new IdentityAttributeRef(afiQName.toString()));
58         routeTable.setSafi(new IdentityAttributeRef(safiQName.toString()));
59         // 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)
60         routeTable.setAdjRibInRoutesCount(toZeroBasedCounter32(this.adjRibInRouteCounters.getCounterOrDefault(tablesKey)));
61         routeTable.setAdjRibOutRoutesCount(toZeroBasedCounter32(this.adjRibOutRouteCounters.getCounterOrDefault(tablesKey)));
62         routeTable.setEffectiveRibInRoutesCount(new ZeroBasedCounter32(this.neighborState
63             .getPrefixesInstalledCount(tablesKey)));
64
65         return routeTable;
66     }
67
68     @Override
69     public BgpPeerState getBgpPeerState() {
70         final BgpPeerState peerState = new BgpPeerState();
71         final List<RouteTable> routes = Lists.newArrayList();
72         this.tablesKeySet.forEach(tablesKey -> routes.add(createRouteTable(tablesKey)));
73         peerState.setRouteTable(routes);
74         peerState.setSessionEstablishedCount(toZeroBasedCounter32(this.sessionEstablishedCounter));
75         return peerState;
76     }
77
78     @Override
79     public LongAdder getSessionEstablishedCounter() {
80         return this.sessionEstablishedCounter;
81     }
82 }