c5495ccb9e003117465571ec7a354a136cc9d725
[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 com.google.common.base.Preconditions;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.controller.config.api.IdentityAttributeRef;
17 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpRenderState;
18 import org.opendaylight.controller.config.yang.bgp.rib.impl.LocRibRouteTable;
19 import org.opendaylight.protocol.bgp.rib.impl.stats.UnsignedInt32Counter;
20 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.route.PerTableTypeRouteCounter;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
26 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
27 import org.opendaylight.yangtools.yang.common.QName;
28
29 public class BGPRenderStatsImpl implements BGPRenderStats {
30     private final PerTableTypeRouteCounter locRibRouteCounter = new PerTableTypeRouteCounter("loc-rib route");
31     private final BgpId bgpId;
32     private final RibId ribId;
33     private final ClusterIdentifier clusterId;
34     private final AsNumber localAs;
35     private final UnsignedInt32Counter configuredPeerCounter;
36     private final UnsignedInt32Counter connectedPeerCounter;
37
38     public BGPRenderStatsImpl(@Nonnull final BgpId bgpId, @Nonnull final RibId ribId, @Nonnull final AsNumber localAs, @Nullable final ClusterIdentifier clusterId) {
39         this.bgpId = Preconditions.checkNotNull(bgpId);
40         this.ribId = Preconditions.checkNotNull(ribId);
41         this.localAs = localAs;
42         this.clusterId = clusterId;
43         this.configuredPeerCounter = new UnsignedInt32Counter("Configured Peer of BGP-RIB " + this.ribId.getValue());
44         this.connectedPeerCounter = new UnsignedInt32Counter("Connected Peer of BGP-RIB " + this.ribId.getValue());
45     }
46
47     @Override
48     public BgpRenderState getBgpRenderState() {
49         final BgpRenderState renderState = new BgpRenderState();
50         renderState.setRibId(this.ribId);
51         renderState.setBgpRibId(this.bgpId);
52         renderState.setClusterId(this.clusterId);
53         renderState.setLocalAs(this.localAs);
54         renderState.setConfiguredPeerCount(this.configuredPeerCounter.getCountAsZeroBasedCounter32());
55         renderState.setConnectedPeerCount(this.connectedPeerCounter.getCountAsZeroBasedCounter32());
56         // fill in the the statistic part
57         final UnsignedInt32Counter totalRouteCount = new UnsignedInt32Counter("Total Loc-Rib Route Count");
58         final List<LocRibRouteTable> locRibRouteTableList = new ArrayList<>();
59         this.locRibRouteCounter.getCounters().entrySet().stream().forEach(e -> generateCounters(e, locRibRouteTableList, totalRouteCount));
60         renderState.setLocRibRouteTable(locRibRouteTableList);
61         renderState.setLocRibRoutesCount(totalRouteCount.getCountAsZeroBasedCounter32());
62         return renderState;
63     }
64
65     private void generateCounters(final Map.Entry<TablesKey, UnsignedInt32Counter> e, final List<LocRibRouteTable> locRibRouteTableList,
66         final UnsignedInt32Counter totalRouteCount) {
67         final LocRibRouteTable table = new LocRibRouteTable();
68         final QName afi = BindingReflections.getQName(e.getKey().getAfi()).intern();
69         final QName safi = BindingReflections.getQName(e.getKey().getSafi()).intern();
70         table.setAfi(new IdentityAttributeRef(afi.toString()));
71         table.setSafi(new IdentityAttributeRef(safi.toString()));
72         table.setRoutesCount(e.getValue().getCountAsZeroBasedCounter32());
73         locRibRouteTableList.add(table);
74         totalRouteCount.increaseCount(e.getValue().getCount());
75
76     }
77
78     @Override
79     public PerTableTypeRouteCounter getLocRibRouteCounter() {
80         return this.locRibRouteCounter;
81     }
82
83     @Override
84     public UnsignedInt32Counter getConfiguredPeerCounter() {
85         return this.configuredPeerCounter;
86     }
87
88     @Override
89     public UnsignedInt32Counter getConnectedPeerCounter() {
90         return this.connectedPeerCounter;
91     }
92 }