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