Removed checkstyle warnings.
[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
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
16 import org.opendaylight.protocol.bgp.rib.RibReference;
17 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsIn;
18 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsInFactory;
19 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 final class RIBTables {
25
26     private static final Logger LOG = LoggerFactory.getLogger(RIBTables.class);
27
28     private final Map<TablesKey, AdjRIBsIn> tables = new HashMap<>();
29     private final RIBExtensionConsumerContext registry;
30
31     RIBTables(final RIBExtensionConsumerContext extensions) {
32         this.registry = Preconditions.checkNotNull(extensions);
33     }
34
35     public synchronized AdjRIBsIn get(final TablesKey key) {
36         LOG.debug("Looking for key {} in tables {}", key, this.tables);
37         final AdjRIBsIn ret = this.tables.get(key);
38         LOG.trace("Key found {}", ret);
39         return ret;
40     }
41
42     public synchronized AdjRIBsIn create(final DataModificationTransaction trans, final RibReference rib, final TablesKey key) {
43         if (this.tables.containsKey(key)) {
44             LOG.warn("Duplicate create request for key {}", key);
45             return this.tables.get(key);
46         }
47
48         final AdjRIBsInFactory f = this.registry.getAdjRIBsInFactory(key.getAfi(), key.getSafi());
49         if (f == null) {
50             LOG.debug("RIBsInFactory not found for key {}, returning null", key);
51             return null;
52         }
53
54         final AdjRIBsIn table = Preconditions.checkNotNull(f.createAdjRIBsIn(trans, rib, key));
55         LOG.debug("Table {} created for key {}", table, key);
56         this.tables.put(key, table);
57         return table;
58     }
59 }