Bump versions to 0.21.6-SNAPSHOT
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / RIBSupportContextRegistryImpl.java
1 /*
2  * Copyright (c) 2015 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 static java.util.Objects.requireNonNull;
11
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.rib.Tables;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
23 import org.opendaylight.yangtools.yang.binding.ChildOf;
24 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27
28 final class RIBSupportContextRegistryImpl implements RIBSupportContextRegistry {
29
30     private final RIBExtensionConsumerContext extensionContext;
31     private final CodecsRegistry codecs;
32     private final LoadingCache<RIBSupport<?, ?>, RIBSupportContextImpl> contexts = CacheBuilder.newBuilder()
33             .build(new CacheLoader<RIBSupport<?, ?>, RIBSupportContextImpl>() {
34                 @Override
35                 public RIBSupportContextImpl load(final RIBSupport<?, ?> key) {
36                     return createRIBSupportContext(key);
37                 }
38             });
39
40     private RIBSupportContextRegistryImpl(final RIBExtensionConsumerContext extensions, final CodecsRegistry codecs) {
41         this.extensionContext = requireNonNull(extensions);
42         this.codecs = requireNonNull(codecs);
43     }
44
45     static RIBSupportContextRegistryImpl create(final RIBExtensionConsumerContext extensions,
46             final CodecsRegistry codecs) {
47         return new RIBSupportContextRegistryImpl(extensions, codecs);
48     }
49
50     private RIBSupportContextImpl createRIBSupportContext(final RIBSupport<?, ?> support) {
51         return new RIBSupportContextImpl(support, this.codecs);
52     }
53
54     @Override
55     public <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<? super C>>
56             RIBSupport<C, S> getRIBSupport(final TablesKey key) {
57         final RIBSupportContext ribSupport = getRIBSupportContext(key);
58         return ribSupport == null ? null : ribSupport.getRibSupport();
59     }
60
61     @Override
62     public RIBSupportContext getRIBSupportContext(final TablesKey key) {
63         final RIBSupport<?, ?> ribSupport = this.extensionContext.getRIBSupport(key);
64         return ribSupport == null ? null : this.contexts.getUnchecked(ribSupport);
65     }
66
67     @Override
68     public RIBSupportContext getRIBSupportContext(final NodeIdentifierWithPredicates key) {
69         final RIBSupport<?, ?> ribSupport = this.extensionContext.getRIBSupport(key);
70         return ribSupport == null ? null : this.contexts.getUnchecked(ribSupport);
71     }
72 }