Drop <R, I> generic from RIBSupport
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import org.opendaylight.protocol.bgp.rib.impl.spi.CodecsRegistry;
17 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
18 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
19 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
20 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
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.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28
29 final class RIBSupportContextRegistryImpl implements RIBSupportContextRegistry {
30
31     private final RIBExtensionConsumerContext extensionContext;
32     private final CodecsRegistry codecs;
33     private final LoadingCache<RIBSupport<?, ?>, RIBSupportContextImpl> contexts = CacheBuilder.newBuilder()
34             .build(new CacheLoader<RIBSupport<?, ?>, RIBSupportContextImpl>() {
35                 @Override
36                 public RIBSupportContextImpl load(final RIBSupport<?, ?> key) {
37                     return createRIBSupportContext(key);
38                 }
39             });
40
41     private RIBSupportContextRegistryImpl(final RIBExtensionConsumerContext extensions, final CodecsRegistry codecs) {
42         this.extensionContext = requireNonNull(extensions);
43         this.codecs = requireNonNull(codecs);
44     }
45
46     static RIBSupportContextRegistryImpl create(final RIBExtensionConsumerContext extensions,
47             final CodecsRegistry codecs) {
48         return new RIBSupportContextRegistryImpl(extensions, codecs);
49     }
50
51     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
52             justification = "https://github.com/spotbugs/spotbugs/issues/811")
53     private RIBSupportContextImpl createRIBSupportContext(final RIBSupport<?, ?> support) {
54         return new RIBSupportContextImpl(support, this.codecs);
55     }
56
57     @Override
58     public <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<? super C>>
59             RIBSupport<C, S> getRIBSupport(final TablesKey key) {
60         final RIBSupportContext ribSupport = getRIBSupportContext(key);
61         return ribSupport == null ? null : ribSupport.getRibSupport();
62     }
63
64     @Override
65     public RIBSupportContext getRIBSupportContext(final TablesKey key) {
66         final RIBSupport<?, ?> ribSupport = this.extensionContext.getRIBSupport(key);
67         return ribSupport == null ? null : this.contexts.getUnchecked(ribSupport);
68     }
69
70     @Override
71     public RIBSupportContext getRIBSupportContext(final NodeIdentifierWithPredicates key) {
72         final RIBSupport<?, ?> ribSupport = this.extensionContext.getRIBSupport(key);
73         return ribSupport == null ? null : this.contexts.getUnchecked(ribSupport);
74     }
75 }