Move future declaration
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / RIBSupportContextRegistryImpl.java
index 340a1b21d6aed5aed042dc2068f9370d2ed71fef..d145a3060cc53856cd2d780ce02a0a171f9785b2 100644 (file)
@@ -7,72 +7,66 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
+import org.opendaylight.protocol.bgp.rib.impl.spi.CodecsRegistry;
 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
-import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTree;
-import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTreeFactory;
-import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
-import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-
-class RIBSupportContextRegistryImpl implements RIBSupportContextRegistry {
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
+import org.opendaylight.yangtools.yang.binding.ChildOf;
+import org.opendaylight.yangtools.yang.binding.ChoiceIn;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 
-    private final LoadingCache<RIBSupport, RIBSupportContextImpl> contexts = CacheBuilder.newBuilder()
-            .build(new CacheLoader<RIBSupport, RIBSupportContextImpl>(){
+final class RIBSupportContextRegistryImpl implements RIBSupportContextRegistry {
 
+    private final RIBExtensionConsumerContext extensionContext;
+    private final CodecsRegistry codecs;
+    private final LoadingCache<RIBSupport<?, ?>, RIBSupportContextImpl> contexts = CacheBuilder.newBuilder()
+            .build(new CacheLoader<RIBSupport<?, ?>, RIBSupportContextImpl>() {
                 @Override
-                public RIBSupportContextImpl load(RIBSupport key) {
-                    return createContext(key);
-                };
+                public RIBSupportContextImpl load(final RIBSupport<?, ?> key) {
+                    return createRIBSupportContext(key);
+                }
             });
 
-    private final RIBExtensionConsumerContext extensionContext;
-    private final BindingCodecTreeFactory codecFactory;
-    private final GeneratedClassLoadingStrategy classContext;
-    private volatile BindingCodecTree latestCodecTree;
+    private RIBSupportContextRegistryImpl(final RIBExtensionConsumerContext extensions, final CodecsRegistry codecs) {
+        this.extensionContext = requireNonNull(extensions);
+        this.codecs = requireNonNull(codecs);
+    }
 
-    private RIBSupportContextRegistryImpl(RIBExtensionConsumerContext extensions, BindingCodecTreeFactory codecFactory,
-            GeneratedClassLoadingStrategy strategy) {
-        this.extensionContext = Preconditions.checkNotNull(extensions);
-        this.codecFactory = Preconditions.checkNotNull(codecFactory);
-        this.classContext = Preconditions.checkNotNull(strategy);
+    static RIBSupportContextRegistryImpl create(final RIBExtensionConsumerContext extensions,
+            final CodecsRegistry codecs) {
+        return new RIBSupportContextRegistryImpl(extensions, codecs);
     }
 
-    static RIBSupportContextRegistryImpl create(RIBExtensionConsumerContext extensions,
-            BindingCodecTreeFactory codecFactory, GeneratedClassLoadingStrategy classStrategy) {
-        return new RIBSupportContextRegistryImpl(extensions, codecFactory, classStrategy);
+    private RIBSupportContextImpl createRIBSupportContext(final RIBSupport<?, ?> support) {
+        return new RIBSupportContextImpl(support, this.codecs);
     }
 
     @Override
-    public RIBSupportContext getRIBSupportContext(TablesKey key) {
-        RIBSupport ribSupport = extensionContext.getRIBSupport(key);
-        if(ribSupport != null) {
-            return contexts.getUnchecked(ribSupport);
-        }
-        return null;
+    public <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<? super C>>
+            RIBSupport<C, S> getRIBSupport(final TablesKey key) {
+        final RIBSupportContext ribSupport = getRIBSupportContext(key);
+        return ribSupport == null ? null : ribSupport.getRibSupport();
     }
 
-    private RIBSupportContextImpl createContext(RIBSupport ribSupport) {
-        RIBSupportContextImpl ribContext = new RIBSupportContextImpl(ribSupport);
-        if(latestCodecTree != null) {
-            ribContext.onCodecTreeUpdated(latestCodecTree);
-        }
-        return ribContext;
+    @Override
+    public RIBSupportContext getRIBSupportContext(final TablesKey key) {
+        final RIBSupport<?, ?> ribSupport = this.extensionContext.getRIBSupport(key);
+        return ribSupport == null ? null : this.contexts.getUnchecked(ribSupport);
     }
 
-    void onSchemaContextUpdated(SchemaContext context) {
-        BindingRuntimeContext runtimeContext = BindingRuntimeContext.create(classContext, context);
-        latestCodecTree  = codecFactory.create(runtimeContext);
-        for(RIBSupportContextImpl rib : contexts.asMap().values()) {
-            rib.onCodecTreeUpdated(latestCodecTree);
-        }
+    @Override
+    public RIBSupportContext getRIBSupportContext(final NodeIdentifierWithPredicates key) {
+        final RIBSupport<?, ?> ribSupport = this.extensionContext.getRIBSupport(key);
+        return ribSupport == null ? null : this.contexts.getUnchecked(ribSupport);
     }
-
 }