2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.rib.impl.state;
12 import com.google.common.base.Preconditions;
13 import java.util.HashMap;
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;
30 public class BGPRIBStateImpl extends DefaultRibReference implements BGPRIBState, BGPRIBStateConsumer {
31 private final BgpId routeId;
32 private final AsNumber localAs;
34 private final Map<TablesKey, TotalPathsCounter> totalPaths = new HashMap<>();
36 private final Map<TablesKey, TotalPrefixesCounter> totalPrefixes = new HashMap<>();
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);
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()));
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()));
58 public final synchronized long getTotalPathsCount() {
59 return this.totalPaths.values().stream().mapToLong(TotalPathsCounter::getPathsCount).sum();
63 public final synchronized long getTotalPrefixesCount() {
64 return this.totalPrefixes.values().stream().mapToLong(TotalPrefixesCounter::getPrefixesCount).sum();
68 public final synchronized long getPathCount(TablesKey tablesKey) {
69 return this.totalPaths.get(tablesKey).getPathsCount();
73 public final synchronized long getPrefixesCount(TablesKey tablesKey) {
74 return this.totalPrefixes.get(tablesKey).getPrefixesCount();
78 public final AsNumber getAs() {
83 public final BgpId getRouteId() {
87 protected final synchronized void registerTotalPathCounter(@Nonnull final TablesKey key,
88 @Nonnull final TotalPathsCounter totalPathsCounter) {
89 this.totalPaths.put(key, totalPathsCounter);
92 protected final synchronized void registerTotalPrefixesCounter(@Nonnull final TablesKey key,
93 @Nonnull final TotalPrefixesCounter totalPrefixesCounter) {
94 this.totalPrefixes.put(key, totalPrefixesCounter);
98 public final BGPRIBState getRIBState() {