Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / stats / peer / BGPPeerStatsImpl.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.peer;
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 com.google.common.collect.Lists;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.concurrent.atomic.LongAdder;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.config.api.IdentityAttributeRef;
20 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpPeerState;
21 import org.opendaylight.controller.config.yang.bgp.rib.impl.RouteTable;
22 import org.opendaylight.protocol.bgp.rib.impl.state.BGPPeerStateImpl;
23 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.route.PerTableTypeRouteCounter;
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.rib.TablesKey;
26 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
27 import org.opendaylight.yangtools.yang.common.QName;
28
29 public final class BGPPeerStatsImpl implements BGPPeerStats {
30     private final Set<TablesKey> tablesKeySet;
31     private final PerTableTypeRouteCounter adjRibInRouteCounters;
32     private final PerTableTypeRouteCounter adjRibOutRouteCounters;
33     private final LongAdder sessionEstablishedCounter = new LongAdder();
34     private final BGPPeerStateImpl neighborState;
35
36     public BGPPeerStatsImpl(@Nonnull final String peerName, @Nonnull final Set<TablesKey> tablesKeySet,
37         @Nonnull final BGPPeerStateImpl neighborState) {
38         requireNonNull(peerName);
39         this.tablesKeySet = requireNonNull(tablesKeySet);
40         this.adjRibInRouteCounters = new PerTableTypeRouteCounter(tablesKeySet);
41         this.adjRibOutRouteCounters = new PerTableTypeRouteCounter(tablesKeySet);
42         this.neighborState = requireNonNull(neighborState);
43     }
44
45     public PerTableTypeRouteCounter getAdjRibInRouteCounters() {
46         return this.adjRibInRouteCounters;
47     }
48
49     public PerTableTypeRouteCounter getAdjRibOutRouteCounters() {
50         return this.adjRibOutRouteCounters;
51     }
52
53     private RouteTable createRouteTable(@Nonnull final TablesKey tablesKey) {
54         requireNonNull(tablesKey);
55         final RouteTable routeTable = new RouteTable();
56         // FIXME: setTableType() is DEPRECATED, use setAfi() and setSafi() instead
57         routeTable.setTableType("afi=" + tablesKey.getAfi().getSimpleName() + ",safi=" + tablesKey.getSafi().getSimpleName());
58
59         final QName afiQName = BindingReflections.findQName(tablesKey.getAfi()).intern();
60         final QName safiQName = BindingReflections.findQName(tablesKey.getSafi()).intern();
61         routeTable.setAfi(new IdentityAttributeRef(afiQName.toString()));
62         routeTable.setSafi(new IdentityAttributeRef(safiQName.toString()));
63         // we want to get default counter in case particular route table is not initialized (e.g. adj-rib-out is not initialized in some cases)
64         routeTable.setAdjRibInRoutesCount(toZeroBasedCounter32(this.adjRibInRouteCounters.getCounterOrDefault(tablesKey)));
65         routeTable.setAdjRibOutRoutesCount(toZeroBasedCounter32(this.adjRibOutRouteCounters.getCounterOrDefault(tablesKey)));
66         routeTable.setEffectiveRibInRoutesCount(new ZeroBasedCounter32(this.neighborState
67             .getPrefixesInstalledCount(tablesKey)));
68
69         return routeTable;
70     }
71
72     @Override
73     public BgpPeerState getBgpPeerState() {
74         final BgpPeerState peerState = new BgpPeerState();
75         final List<RouteTable> routes = Lists.newArrayList();
76         this.tablesKeySet.forEach(tablesKey -> routes.add(createRouteTable(tablesKey)));
77         peerState.setRouteTable(routes);
78         peerState.setSessionEstablishedCount(toZeroBasedCounter32(this.sessionEstablishedCounter));
79         return peerState;
80     }
81
82     @Override
83     public LongAdder getSessionEstablishedCounter() {
84         return this.sessionEstablishedCounter;
85     }
86 }