0c98ae144977440ad9dc6c4e58760dc6222ba1ea
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / RIBTables.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.util.HashMap;
12 import java.util.Map;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.protocol.bgp.rib.RibReference;
16 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsFactory;
17 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsIn;
18 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.LocRib;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.AttributesBuilder;
24 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @Deprecated
29 final class RIBTables {
30
31     private static final Logger LOG = LoggerFactory.getLogger(RIBTables.class);
32
33     private final Map<TablesKey, AdjRIBsIn<?, ?>> tables = new HashMap<>();
34     private final RIBExtensionConsumerContext registry;
35
36     RIBTables(final RIBExtensionConsumerContext extensions) {
37         this.registry = Preconditions.checkNotNull(extensions);
38     }
39
40     public synchronized AdjRIBsIn<?, ?> get(final TablesKey key) {
41         LOG.debug("Looking for key {} in tables {}", key, this.tables);
42         final AdjRIBsIn<?, ?> ret = this.tables.get(key);
43         LOG.trace("Key found {}", ret);
44         return ret;
45     }
46
47     public synchronized AdjRIBsIn<?, ?> create(final WriteTransaction trans, final RibReference rib, final TablesKey key) {
48         if (this.tables.containsKey(key)) {
49             LOG.warn("Duplicate create request for key {}", key);
50             return this.tables.get(key);
51         }
52
53         final AdjRIBsFactory f = this.registry.getAdjRIBsInFactory(key.getAfi(), key.getSafi());
54         if (f == null) {
55             LOG.debug("RIBsInFactory not found for key {}, returning null", key);
56             return null;
57         }
58
59         final KeyedInstanceIdentifier<Tables, TablesKey> basePath = rib.getInstanceIdentifier().child(LocRib.class).child(Tables.class, key);
60         final AdjRIBsIn<?, ?> table = Preconditions.checkNotNull(f.createAdjRIBs(basePath));
61         LOG.debug("Table {} created for key {}", table, key);
62         this.tables.put(key, table);
63
64         trans.put(LogicalDatastoreType.OPERATIONAL, basePath,
65                 new TablesBuilder().setAfi(key.getAfi()).setSafi(key.getSafi())
66                 .setAttributes(new AttributesBuilder().setUptodate(Boolean.TRUE).build()).build());
67
68         return table;
69     }
70 }