BUG-5032: BGP Operational State
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / stats / rib / impl / BGPRenderStatsImpl.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.rib.impl;
9
10 import static org.opendaylight.protocol.bgp.rib.impl.CountersUtil.toZeroBasedCounter32;
11
12 import com.google.common.base.Preconditions;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Set;
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;
31
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;
41
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;
51     }
52
53     @Override
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));
68         return renderState;
69     }
70
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);
82
83     }
84
85     @Override
86     public LongAdder getConfiguredPeerCounter() {
87         return this.configuredPeerCounter;
88     }
89
90     @Override
91     public LongAdder getConnectedPeerCounter() {
92         return this.connectedPeerCounter;
93     }
94 }