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 java.util.HashMap;
16 import java.util.stream.Collectors;
17 import javax.annotation.Nonnull;
18 import javax.annotation.concurrent.GuardedBy;
19 import org.opendaylight.protocol.bgp.rib.DefaultRibReference;
20 import org.opendaylight.protocol.bgp.rib.impl.state.rib.TotalPathsCounter;
21 import org.opendaylight.protocol.bgp.rib.impl.state.rib.TotalPrefixesCounter;
22 import org.opendaylight.protocol.bgp.rib.spi.state.BGPRibState;
23 import org.opendaylight.protocol.bgp.rib.spi.state.BGPRibStateConsumer;
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.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.RibKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
29 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
31 public class BGPRIBStateImpl extends DefaultRibReference implements BGPRibState, BGPRibStateConsumer {
32 private final BgpId routeId;
33 private final AsNumber localAs;
35 private final Map<TablesKey, TotalPathsCounter> totalPaths = new HashMap<>();
37 private final Map<TablesKey, TotalPrefixesCounter> totalPrefixes = new HashMap<>();
39 private boolean active;
41 protected BGPRIBStateImpl(final KeyedInstanceIdentifier<Rib, RibKey> instanceIdentifier,
42 @Nonnull final BgpId routeId, @Nonnull final AsNumber localAs) {
43 super(instanceIdentifier);
44 this.routeId = requireNonNull(routeId);
45 this.localAs = requireNonNull(localAs);
49 public final synchronized Map<TablesKey, Long> getTablesPrefixesCount() {
50 return this.totalPrefixes.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
51 (entry) -> entry.getValue().getPrefixesCount()));
55 public final synchronized Map<TablesKey, Long> getPathsCount() {
56 return this.totalPaths.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
57 (entry) -> entry.getValue().getPathsCount()));
61 public final synchronized long getTotalPathsCount() {
62 return this.totalPaths.values().stream().mapToLong(TotalPathsCounter::getPathsCount).sum();
66 public final synchronized long getTotalPrefixesCount() {
67 return this.totalPrefixes.values().stream().mapToLong(TotalPrefixesCounter::getPrefixesCount).sum();
71 public final synchronized long getPathCount(TablesKey tablesKey) {
72 return this.totalPaths.get(tablesKey).getPathsCount();
76 public final synchronized long getPrefixesCount(TablesKey tablesKey) {
77 return this.totalPrefixes.get(tablesKey).getPrefixesCount();
81 public final AsNumber getAs() {
86 public final BgpId getRouteId() {
90 protected final synchronized void registerTotalPathCounter(@Nonnull final TablesKey key,
91 @Nonnull final TotalPathsCounter totalPathsCounter) {
92 this.totalPaths.put(key, totalPathsCounter);
95 protected final synchronized void registerTotalPrefixesCounter(@Nonnull final TablesKey key,
96 @Nonnull final TotalPrefixesCounter totalPrefixesCounter) {
97 this.totalPrefixes.put(key, totalPrefixesCounter);
101 public final synchronized boolean isActive() {
105 protected final synchronized void setActive(final boolean active) {
106 this.active = active;
110 public final BGPRibState getRIBState() {