477066e75f8d62bf61192296f02c3b3af01bf64e
[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 static java.util.Objects.requireNonNull;
13
14 import java.util.HashMap;
15 import java.util.Map;
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;
30
31 public class BGPRIBStateImpl extends DefaultRibReference implements BGPRibState, BGPRibStateConsumer {
32     private final BgpId routeId;
33     private final AsNumber localAs;
34     @GuardedBy("this")
35     private final Map<TablesKey, TotalPathsCounter> totalPaths = new HashMap<>();
36     @GuardedBy("this")
37     private final Map<TablesKey, TotalPrefixesCounter> totalPrefixes = new HashMap<>();
38     @GuardedBy("this")
39     private boolean active;
40
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);
46     }
47
48     @Override
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()));
52     }
53
54     @Override
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()));
58     }
59
60     @Override
61     public final synchronized long getTotalPathsCount() {
62         return this.totalPaths.values().stream().mapToLong(TotalPathsCounter::getPathsCount).sum();
63     }
64
65     @Override
66     public final synchronized long getTotalPrefixesCount() {
67         return this.totalPrefixes.values().stream().mapToLong(TotalPrefixesCounter::getPrefixesCount).sum();
68     }
69
70     @Override
71     public final synchronized long getPathCount(TablesKey tablesKey) {
72         return this.totalPaths.get(tablesKey).getPathsCount();
73     }
74
75     @Override
76     public final synchronized long getPrefixesCount(TablesKey tablesKey) {
77         return this.totalPrefixes.get(tablesKey).getPrefixesCount();
78     }
79
80     @Override
81     public final AsNumber getAs() {
82         return this.localAs;
83     }
84
85     @Override
86     public final BgpId getRouteId() {
87         return this.routeId;
88     }
89
90     protected final synchronized void registerTotalPathCounter(@Nonnull final TablesKey key,
91         @Nonnull final TotalPathsCounter totalPathsCounter) {
92         this.totalPaths.put(key, totalPathsCounter);
93     }
94
95     protected final synchronized void registerTotalPrefixesCounter(@Nonnull final TablesKey key,
96         @Nonnull final TotalPrefixesCounter totalPrefixesCounter) {
97         this.totalPrefixes.put(key, totalPrefixesCounter);
98     }
99
100     @Override
101     public final synchronized boolean isActive() {
102         return this.active;
103     }
104
105     protected final synchronized void setActive(final boolean active) {
106         this.active = active;
107     }
108
109     @Override
110     public final BGPRibState getRIBState() {
111         return this;
112     }
113 }