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 static java.util.Objects.requireNonNull;
14 import com.google.common.base.Preconditions;
15 import java.util.HashMap;
17 import java.util.stream.Collectors;
18 import javax.annotation.Nonnull;
19 import javax.annotation.concurrent.GuardedBy;
20 import org.opendaylight.protocol.bgp.rib.DefaultRibReference;
21 import org.opendaylight.protocol.bgp.rib.impl.state.rib.TotalPathsCounter;
22 import org.opendaylight.protocol.bgp.rib.impl.state.rib.TotalPrefixesCounter;
23 import org.opendaylight.protocol.bgp.rib.spi.state.BGPRIBState;
24 import org.opendaylight.protocol.bgp.rib.spi.state.BGPRIBStateConsumer;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
30 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
32 public class BGPRIBStateImpl extends DefaultRibReference implements BGPRIBState, BGPRIBStateConsumer {
33 private final BgpId routeId;
34 private final AsNumber localAs;
36 private final Map<TablesKey, TotalPathsCounter> totalPaths = new HashMap<>();
38 private final Map<TablesKey, TotalPrefixesCounter> totalPrefixes = new HashMap<>();
40 protected BGPRIBStateImpl(final KeyedInstanceIdentifier<Rib, RibKey> instanceIdentifier,
41 @Nonnull final BgpId routeId, @Nonnull final AsNumber localAs) {
42 super(instanceIdentifier);
43 this.routeId = requireNonNull(routeId);
44 this.localAs = requireNonNull(localAs);
48 public final synchronized Map<TablesKey, Long> getPrefixesCount() {
49 return this.totalPrefixes.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
50 (entry) -> entry.getValue().getPrefixesCount()));
54 public final synchronized Map<TablesKey, Long> getPathsCount() {
55 return this.totalPaths.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
56 (entry) -> entry.getValue().getPathsCount()));
60 public final synchronized long getTotalPathsCount() {
61 return this.totalPaths.values().stream().mapToLong(TotalPathsCounter::getPathsCount).sum();
65 public final synchronized long getTotalPrefixesCount() {
66 return this.totalPrefixes.values().stream().mapToLong(TotalPrefixesCounter::getPrefixesCount).sum();
70 public final synchronized long getPathCount(TablesKey tablesKey) {
71 return this.totalPaths.get(tablesKey).getPathsCount();
75 public final synchronized long getPrefixesCount(TablesKey tablesKey) {
76 return this.totalPrefixes.get(tablesKey).getPrefixesCount();
80 public final AsNumber getAs() {
85 public final BgpId getRouteId() {
89 protected final synchronized void registerTotalPathCounter(@Nonnull final TablesKey key,
90 @Nonnull final TotalPathsCounter totalPathsCounter) {
91 this.totalPaths.put(key, totalPathsCounter);
94 protected final synchronized void registerTotalPrefixesCounter(@Nonnull final TablesKey key,
95 @Nonnull final TotalPrefixesCounter totalPrefixesCounter) {
96 this.totalPrefixes.put(key, totalPrefixesCounter);
100 public final BGPRIBState getRIBState() {