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