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.rib.impl;
10 import static org.opendaylight.protocol.bgp.rib.impl.CountersUtil.toZeroBasedCounter32;
12 import com.google.common.base.Preconditions;
13 import java.util.ArrayList;
14 import java.util.List;
16 import java.util.concurrent.atomic.LongAdder;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.controller.config.api.IdentityAttributeRef;
20 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpRenderState;
21 import org.opendaylight.controller.config.yang.bgp.rib.impl.LocRibRouteTable;
22 import org.opendaylight.protocol.bgp.rib.spi.state.BGPRIBState;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.ZeroBasedCounter32;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
29 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
30 import org.opendaylight.yangtools.yang.common.QName;
32 public final class BGPRenderStatsImpl implements BGPRenderStats {
33 private final BgpId bgpId;
34 private final RibId ribId;
35 private final ClusterIdentifier clusterId;
36 private final AsNumber localAs;
37 private final LongAdder configuredPeerCounter = new LongAdder();
38 private final LongAdder connectedPeerCounter = new LongAdder();
39 private final BGPRIBState globalState;
40 private final Set<TablesKey> tablesKeys;
42 public BGPRenderStatsImpl(@Nonnull final BgpId bgpId, @Nonnull final RibId ribId, @Nonnull final AsNumber localAs,
43 @Nullable final ClusterIdentifier clusterId, @Nonnull final BGPRIBState globalState,
44 @Nonnull final Set<TablesKey> tablesKeys) {
45 this.bgpId = Preconditions.checkNotNull(bgpId);
46 this.ribId = Preconditions.checkNotNull(ribId);
47 this.globalState = Preconditions.checkNotNull(globalState);
48 this.tablesKeys = Preconditions.checkNotNull(tablesKeys);
49 this.localAs = localAs;
50 this.clusterId = clusterId;
54 public BgpRenderState getBgpRenderState() {
55 final BgpRenderState renderState = new BgpRenderState();
56 renderState.setRibId(this.ribId);
57 renderState.setBgpRibId(this.bgpId);
58 renderState.setClusterId(this.clusterId);
59 renderState.setLocalAs(this.localAs);
60 renderState.setConfiguredPeerCount(toZeroBasedCounter32(this.configuredPeerCounter));
61 renderState.setConnectedPeerCount(toZeroBasedCounter32(this.connectedPeerCounter));
62 // fill in the the statistic part
63 final LongAdder totalRouteCount = new LongAdder();
64 final List<LocRibRouteTable> locRibRouteTableList = new ArrayList<>();
65 this.tablesKeys.forEach(e -> generateCounters(e, locRibRouteTableList, totalRouteCount));
66 renderState.setLocRibRouteTable(locRibRouteTableList);
67 renderState.setLocRibRoutesCount(toZeroBasedCounter32(totalRouteCount));
71 private void generateCounters(final TablesKey tablesKey, final List<LocRibRouteTable> locRibRouteTableList,
72 final LongAdder totalRouteCount) {
73 final LocRibRouteTable table = new LocRibRouteTable();
74 final QName afi = BindingReflections.getQName(tablesKey.getAfi()).intern();
75 final QName safi = BindingReflections.getQName(tablesKey.getSafi()).intern();
76 table.setAfi(new IdentityAttributeRef(afi.toString()));
77 table.setSafi(new IdentityAttributeRef(safi.toString()));
78 final long count = this.globalState.getPathCount(tablesKey);
79 table.setRoutesCount(new ZeroBasedCounter32(count));
80 locRibRouteTableList.add(table);
81 totalRouteCount.add(count);
86 public LongAdder getConfiguredPeerCounter() {
87 return this.configuredPeerCounter;
91 public LongAdder getConnectedPeerCounter() {
92 return this.connectedPeerCounter;