Replace Preconditions.CheckNotNull per RequireNonNull
[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 com.google.common.base.Preconditions;
15 import java.util.HashMap;
16 import java.util.Map;
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;
31
32 public class BGPRIBStateImpl extends DefaultRibReference implements BGPRIBState, BGPRIBStateConsumer {
33     private final BgpId routeId;
34     private final AsNumber localAs;
35     @GuardedBy("this")
36     private final Map<TablesKey, TotalPathsCounter> totalPaths = new HashMap<>();
37     @GuardedBy("this")
38     private final Map<TablesKey, TotalPrefixesCounter> totalPrefixes = new HashMap<>();
39
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);
45     }
46
47     @Override
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()));
51     }
52
53     @Override
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()));
57     }
58
59     @Override
60     public final synchronized long getTotalPathsCount() {
61         return this.totalPaths.values().stream().mapToLong(TotalPathsCounter::getPathsCount).sum();
62     }
63
64     @Override
65     public final synchronized long getTotalPrefixesCount() {
66         return this.totalPrefixes.values().stream().mapToLong(TotalPrefixesCounter::getPrefixesCount).sum();
67     }
68
69     @Override
70     public final synchronized long getPathCount(TablesKey tablesKey) {
71         return this.totalPaths.get(tablesKey).getPathsCount();
72     }
73
74     @Override
75     public final synchronized long getPrefixesCount(TablesKey tablesKey) {
76         return this.totalPrefixes.get(tablesKey).getPrefixesCount();
77     }
78
79     @Override
80     public final AsNumber getAs() {
81         return this.localAs;
82     }
83
84     @Override
85     public final BgpId getRouteId() {
86         return this.routeId;
87     }
88
89     protected final synchronized void registerTotalPathCounter(@Nonnull final TablesKey key,
90         @Nonnull final TotalPathsCounter totalPathsCounter) {
91         this.totalPaths.put(key, totalPathsCounter);
92     }
93
94     protected final synchronized void registerTotalPrefixesCounter(@Nonnull final TablesKey key,
95         @Nonnull final TotalPrefixesCounter totalPrefixesCounter) {
96         this.totalPrefixes.put(key, totalPrefixesCounter);
97     }
98
99     @Override
100     public final BGPRIBState getRIBState() {
101         return this;
102     }
103 }