Add bundle activator and use it
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / RIBTables.java
1 package org.opendaylight.protocol.bgp.rib.impl;
2
3 import java.util.Comparator;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsIn;
8 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsInFactoryRegistry;
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.PathAttributes;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
11
12 final class RIBTables {
13         private final Map<TablesKey, AdjRIBsIn> tables = new HashMap<>();
14         private final Comparator<PathAttributes> comparator;
15         private final AdjRIBsInFactoryRegistry registry;
16
17         RIBTables(final Comparator<PathAttributes> comparator, final AdjRIBsInFactoryRegistry registry) {
18                 this.comparator = comparator;
19                 this.registry = registry;
20         }
21
22         public synchronized AdjRIBsIn get(final TablesKey key) {
23                 return tables.get(key);
24         }
25
26         public synchronized AdjRIBsIn getOrCreate(final TablesKey key) {
27                 final AdjRIBsIn table;
28
29                 if (!tables.containsKey(key)) {
30                         table = registry.getAdjRIBsInFactory(key.getAfi(), key.getSafi()).createAdjRIBsIn(comparator, key);
31                         tables.put(key, table);
32                 } else {
33                         table = tables.get(key);
34                 }
35
36                 return table;
37         }
38
39 }