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.rev180329.Route;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
24 import org.opendaylight.yangtools.yang.binding.ChildOf;
25 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.Identifiable;
28 import org.opendaylight.yangtools.yang.binding.Identifier;
30 final class RIBSupportContextRegistryImpl implements RIBSupportContextRegistry {
32 private final RIBExtensionConsumerContext extensionContext;
33 private final CodecsRegistry codecs;
34 private final LoadingCache<RIBSupport<?, ?, ?, ?>, RIBSupportContextImpl> contexts = CacheBuilder.newBuilder()
35 .build(new CacheLoader<RIBSupport<?, ?, ?, ?>, RIBSupportContextImpl>() {
37 public RIBSupportContextImpl load(final RIBSupport<?, ?, ?, ?> key) {
38 return createRIBSupportContext(key);
42 private RIBSupportContextRegistryImpl(final RIBExtensionConsumerContext extensions, final CodecsRegistry codecs) {
43 this.extensionContext = requireNonNull(extensions);
44 this.codecs = requireNonNull(codecs);
47 static RIBSupportContextRegistryImpl create(final RIBExtensionConsumerContext extensions,
48 final CodecsRegistry codecs) {
49 return new RIBSupportContextRegistryImpl(extensions, codecs);
52 private RIBSupportContextImpl createRIBSupportContext(final RIBSupport<?, ?, ?, ?> support) {
53 return new RIBSupportContextImpl(support, this.codecs);
57 public <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<? super C>,
58 R extends Route & ChildOf<? super S> & Identifiable<I>, I extends Identifier<R>>
59 RIBSupport<C, S, R, I> getRIBSupport(final TablesKey key) {
60 final RIBSupportContext ribSupport = getRIBSupportContext(key);
61 return ribSupport == null ? null : ribSupport.getRibSupport();
65 public RIBSupportContext getRIBSupportContext(final TablesKey key) {
66 final RIBSupport<?, ?, ?, ?> ribSupport = this.extensionContext.getRIBSupport(key);
67 return ribSupport == null ? null : this.contexts.getUnchecked(ribSupport);