2 * Copyright (c) 2015 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 static java.util.Objects.requireNonNull;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import org.opendaylight.protocol.bgp.rib.impl.spi.CodecsRegistry;
16 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
17 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
18 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
19 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.TablesKey;
22 final class RIBSupportContextRegistryImpl implements RIBSupportContextRegistry {
24 private final RIBExtensionConsumerContext extensionContext;
25 private final CodecsRegistry codecs;
26 private final LoadingCache<RIBSupport, RIBSupportContextImpl> contexts = CacheBuilder.newBuilder()
27 .build(new CacheLoader<RIBSupport, RIBSupportContextImpl>() {
29 public RIBSupportContextImpl load(final RIBSupport key) {
30 return createRIBSupportContext(key);
34 private RIBSupportContextRegistryImpl(final RIBExtensionConsumerContext extensions, final CodecsRegistry codecs) {
35 this.extensionContext = requireNonNull(extensions);
36 this.codecs = requireNonNull(codecs);
39 static RIBSupportContextRegistryImpl create(final RIBExtensionConsumerContext extensions, final CodecsRegistry codecs) {
40 return new RIBSupportContextRegistryImpl(extensions, codecs);
43 private RIBSupportContextImpl createRIBSupportContext(final RIBSupport support) {
44 return new RIBSupportContextImpl(support, this.codecs);
48 public RIBSupport getRIBSupport(final TablesKey key) {
49 final RIBSupportContext ribSupport = getRIBSupportContext(key);
50 if (ribSupport != null) {
51 return ribSupport.getRibSupport();
57 public RIBSupportContext getRIBSupportContext(final TablesKey key) {
58 final RIBSupport ribSupport = this.extensionContext.getRIBSupport(key);
59 if (ribSupport != null) {
60 return this.contexts.getUnchecked(ribSupport);