2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl.stats.peer;
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.protocol.bgp.rib.impl.CountersUtil.toZeroBasedCounter32;
13 import com.google.common.collect.Lists;
14 import java.util.List;
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;
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;
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);
44 public PerTableTypeRouteCounter getAdjRibInRouteCounters() {
45 return this.adjRibInRouteCounters;
48 public PerTableTypeRouteCounter getAdjRibOutRouteCounters() {
49 return this.adjRibOutRouteCounters;
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)));
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));
79 public LongAdder getSessionEstablishedCounter() {
80 return this.sessionEstablishedCounter;