BGPCEP-768: Fix switch case
[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.rev171207.rib.TablesKey;
21
22 final class RIBSupportContextRegistryImpl implements RIBSupportContextRegistry {
23
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>() {
28                 @Override
29                 public RIBSupportContextImpl load(final RIBSupport key) {
30                     return createRIBSupportContext(key);
31                 }
32             });
33
34     private RIBSupportContextRegistryImpl(final RIBExtensionConsumerContext extensions, final CodecsRegistry codecs) {
35         this.extensionContext = requireNonNull(extensions);
36         this.codecs = requireNonNull(codecs);
37     }
38
39     static RIBSupportContextRegistryImpl create(final RIBExtensionConsumerContext extensions, final CodecsRegistry codecs) {
40         return new RIBSupportContextRegistryImpl(extensions, codecs);
41     }
42
43     private RIBSupportContextImpl createRIBSupportContext(final RIBSupport support) {
44         return new RIBSupportContextImpl(support, this.codecs);
45     }
46
47     @Override
48     public RIBSupport getRIBSupport(final TablesKey key) {
49         final RIBSupportContext ribSupport = getRIBSupportContext(key);
50         if (ribSupport != null) {
51             return ribSupport.getRibSupport();
52         }
53         return null;
54     }
55
56     @Override
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);
61         }
62         return null;
63     }
64 }