2 * Copyright (c) 2013 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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import com.google.common.base.Preconditions;
11 import java.util.HashMap;
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;
29 final class RIBTables {
31 private static final Logger LOG = LoggerFactory.getLogger(RIBTables.class);
33 private final Map<TablesKey, AdjRIBsIn<?, ?>> tables = new HashMap<>();
34 private final RIBExtensionConsumerContext registry;
36 RIBTables(final RIBExtensionConsumerContext extensions) {
37 this.registry = Preconditions.checkNotNull(extensions);
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);
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);
53 final AdjRIBsFactory f = this.registry.getAdjRIBsInFactory(key.getAfi(), key.getSafi());
55 LOG.debug("RIBsInFactory not found for key {}, returning null", key);
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);
64 trans.put(LogicalDatastoreType.OPERATIONAL, basePath,
65 new TablesBuilder().setAfi(key.getAfi()).setSafi(key.getSafi())
66 .setAttributes(new AttributesBuilder().setUptodate(Boolean.TRUE).build()).build());