Bump odlparent to 6.0.0
[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.Route;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
25 import org.opendaylight.yangtools.yang.binding.ChildOf;
26 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.Identifiable;
29 import org.opendaylight.yangtools.yang.binding.Identifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
31
32 final class RIBSupportContextRegistryImpl implements RIBSupportContextRegistry {
33
34     private final RIBExtensionConsumerContext extensionContext;
35     private final CodecsRegistry codecs;
36     private final LoadingCache<RIBSupport<?, ?, ?, ?>, RIBSupportContextImpl> contexts = CacheBuilder.newBuilder()
37             .build(new CacheLoader<RIBSupport<?, ?, ?, ?>, RIBSupportContextImpl>() {
38                 @Override
39                 public RIBSupportContextImpl load(final RIBSupport<?, ?, ?, ?> key) {
40                     return createRIBSupportContext(key);
41                 }
42             });
43
44     private RIBSupportContextRegistryImpl(final RIBExtensionConsumerContext extensions, final CodecsRegistry codecs) {
45         this.extensionContext = requireNonNull(extensions);
46         this.codecs = requireNonNull(codecs);
47     }
48
49     static RIBSupportContextRegistryImpl create(final RIBExtensionConsumerContext extensions,
50             final CodecsRegistry codecs) {
51         return new RIBSupportContextRegistryImpl(extensions, codecs);
52     }
53
54     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
55             justification = "https://github.com/spotbugs/spotbugs/issues/811")
56     private RIBSupportContextImpl createRIBSupportContext(final RIBSupport<?, ?, ?, ?> support) {
57         return new RIBSupportContextImpl(support, this.codecs);
58     }
59
60     @Override
61     public <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<? super C>,
62             R extends Route & ChildOf<? super S> & Identifiable<I>, I extends Identifier<R>>
63             RIBSupport<C, S, R, I> getRIBSupport(final TablesKey key) {
64         final RIBSupportContext ribSupport = getRIBSupportContext(key);
65         return ribSupport == null ? null : ribSupport.getRibSupport();
66     }
67
68     @Override
69     public RIBSupportContext getRIBSupportContext(final TablesKey key) {
70         final RIBSupport<?, ?, ?, ?> ribSupport = this.extensionContext.getRIBSupport(key);
71         return ribSupport == null ? null : this.contexts.getUnchecked(ribSupport);
72     }
73
74     @Override
75     public RIBSupportContext getRIBSupportContext(final NodeIdentifierWithPredicates key) {
76         final RIBSupport<?, ?, ?, ?> ribSupport = this.extensionContext.getRIBSupport(key);
77         return ribSupport == null ? null : this.contexts.getUnchecked(ribSupport);
78     }
79 }