537a578e2cab08554e06264903580467782cc5c4
[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 com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import java.util.List;
13 import java.util.Set;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.config.api.IdentityAttributeRef;
16 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpPeerState;
17 import org.opendaylight.controller.config.yang.bgp.rib.impl.RouteTable;
18 import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
19 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.route.PerTableTypeRouteCounter;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
21 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
22 import org.opendaylight.yangtools.yang.common.QName;
23
24 public final class BGPPeerStatsImpl implements BGPPeerStats {
25     private final Set<TablesKey> tablesKeySet;
26     private final PerTableTypeRouteCounter adjRibInRouteCounters;
27     private final PerTableTypeRouteCounter adjRibOutRouteCounters;
28     private final PerTableTypeRouteCounter effectiveRibInRouteCounters;
29     private final UnsignedInt32Counter sessionEstablishedCounter = new UnsignedInt32Counter("SESSION ESTABLISHED");
30     private final String peerName;
31
32     public BGPPeerStatsImpl(@Nonnull final String peerName, @Nonnull final Set<TablesKey> tablesKeySet) {
33         this.peerName = Preconditions.checkNotNull(peerName);
34         this.tablesKeySet = Preconditions.checkNotNull(tablesKeySet);
35         this.adjRibInRouteCounters = new PerTableTypeRouteCounter("["+ this.peerName +"] adj-rib-in route", tablesKeySet);
36         this.adjRibOutRouteCounters = new PerTableTypeRouteCounter("["+ this.peerName +"] adj-rib-out route", tablesKeySet);
37         this.effectiveRibInRouteCounters = new PerTableTypeRouteCounter("["+ this.peerName +"] effective-rib-in route", 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(adjRibInRouteCounters.getCounterOrDefault(tablesKey).getCountAsZeroBasedCounter32());
64         routeTable.setAdjRibOutRoutesCount(adjRibOutRouteCounters.getCounterOrDefault(tablesKey).getCountAsZeroBasedCounter32());
65         routeTable.setEffectiveRibInRoutesCount(effectiveRibInRouteCounters.getCounterOrDefault(tablesKey).getCountAsZeroBasedCounter32());
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.stream().forEach(tablesKey -> routes.add(createRouteTable(tablesKey)));
75         peerState.setRouteTable(routes);
76         peerState.setSessionEstablishedCount(this.sessionEstablishedCounter.getCountAsZeroBasedCounter32());
77         return peerState;
78     }
79
80     @Override
81     public UnsignedInt32Counter getSessionEstablishedCounter() {
82         return this.sessionEstablishedCounter;
83     }
84 }