BUG-5032: BGP Operational State
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / state / BGPRIBStateImpl.java
1 /*
2  * Copyright (c) 2016 Cisco 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
9 package org.opendaylight.protocol.bgp.rib.impl.state;
10
11
12 import com.google.common.base.Preconditions;
13 import java.util.HashMap;
14 import java.util.Map;
15 import java.util.stream.Collectors;
16 import javax.annotation.Nonnull;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.protocol.bgp.rib.DefaultRibReference;
19 import org.opendaylight.protocol.bgp.rib.impl.state.rib.TotalPathsCounter;
20 import org.opendaylight.protocol.bgp.rib.impl.state.rib.TotalPrefixesCounter;
21 import org.opendaylight.protocol.bgp.rib.spi.state.BGPRIBState;
22 import org.opendaylight.protocol.bgp.rib.spi.state.BGPRIBStateConsumer;
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.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey;
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.yangtools.yang.binding.KeyedInstanceIdentifier;
29
30 public class BGPRIBStateImpl extends DefaultRibReference implements BGPRIBState, BGPRIBStateConsumer {
31     private final BgpId routeId;
32     private final AsNumber localAs;
33     @GuardedBy("this")
34     private final Map<TablesKey, TotalPathsCounter> totalPaths = new HashMap<>();
35     @GuardedBy("this")
36     private final Map<TablesKey, TotalPrefixesCounter> totalPrefixes = new HashMap<>();
37
38     protected BGPRIBStateImpl(final KeyedInstanceIdentifier<Rib, RibKey> instanceIdentifier,
39         @Nonnull final BgpId routeId, @Nonnull final AsNumber localAs) {
40         super(instanceIdentifier);
41         this.routeId = Preconditions.checkNotNull(routeId);
42         this.localAs = Preconditions.checkNotNull(localAs);
43     }
44
45     @Override
46     public final synchronized Map<TablesKey, Long> getPrefixesCount() {
47         return this.totalPrefixes.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
48             (entry) -> entry.getValue().getPrefixesCount()));
49     }
50
51     @Override
52     public final synchronized Map<TablesKey, Long> getPathsCount() {
53         return this.totalPaths.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
54             (entry) -> entry.getValue().getPathsCount()));
55     }
56
57     @Override
58     public final synchronized long getTotalPathsCount() {
59         return this.totalPaths.values().stream().mapToLong(TotalPathsCounter::getPathsCount).sum();
60     }
61
62     @Override
63     public final synchronized long getTotalPrefixesCount() {
64         return this.totalPrefixes.values().stream().mapToLong(TotalPrefixesCounter::getPrefixesCount).sum();
65     }
66
67     @Override
68     public final synchronized long getPathCount(TablesKey tablesKey) {
69         return this.totalPaths.get(tablesKey).getPathsCount();
70     }
71
72     @Override
73     public final synchronized long getPrefixesCount(TablesKey tablesKey) {
74         return this.totalPrefixes.get(tablesKey).getPrefixesCount();
75     }
76
77     @Override
78     public final AsNumber getAs() {
79         return this.localAs;
80     }
81
82     @Override
83     public final BgpId getRouteId() {
84         return this.routeId;
85     }
86
87     protected final synchronized void registerTotalPathCounter(@Nonnull final TablesKey key,
88         @Nonnull final TotalPathsCounter totalPathsCounter) {
89         this.totalPaths.put(key, totalPathsCounter);
90     }
91
92     protected final synchronized void registerTotalPrefixesCounter(@Nonnull final TablesKey key,
93         @Nonnull final TotalPrefixesCounter totalPrefixesCounter) {
94         this.totalPrefixes.put(key, totalPrefixesCounter);
95     }
96
97     @Override
98     public final BGPRIBState getRIBState() {
99         return this;
100     }
101 }