BUG-2383: refactor RIBSupportRegistry 69/16069/1
authorRobert Varga <rovarga@cisco.com>
Thu, 5 Mar 2015 09:12:17 +0000 (10:12 +0100)
committerRobert Varga <rovarga@cisco.com>
Thu, 5 Mar 2015 11:07:29 +0000 (12:07 +0100)
Migrate registration and lookup into RIBExtensionContexts.

Change-Id: Ia1cdfb1052abc923c79ecada0ca62c99d3ed731a
Signed-off-by: Robert Varga <rovarga@cisco.com>
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/AdjRibInWriter.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/EffectiveRibInWriter.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/RIBSupportRegistry.java [deleted file]
bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/AbstractRIBSupportRegistration.java [new file with mode: 0644]
bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/RIBExtensionConsumerContext.java
bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/RIBExtensionProviderContext.java
bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/RIBSupportRegistration.java [new file with mode: 0644]
bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/SimpleRIBExtensionProviderContext.java

index 5129b31a6e5a7d878f4f82346be3a1875a18fb90..b630e7b05722c369551c56fca6bf9654378f9c43 100644 (file)
@@ -19,6 +19,7 @@ import javax.annotation.concurrent.NotThreadSafe;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+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.message.rev130919.PathAttributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
@@ -88,7 +89,7 @@ final class AdjRibInWriter {
      * @param tableTypes New tables, must not be null
      * @return New writer
      */
-    AdjRibInWriter changeTableTypes(final RIBSupportRegistry registry, final Set<TablesKey> tableTypes) {
+    AdjRibInWriter changeTableTypes(final RIBExtensionConsumerContext registry, final Set<TablesKey> tableTypes) {
         if (tableTypes.equals(tables.keySet())) {
             return this;
         }
index 0d35700dc52005bb5587d58f2f631aebcc3a6f70..45a32878649c981d8da3fe7ebb87598ddad565d8 100644 (file)
@@ -21,6 +21,7 @@ import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+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.PeerId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
@@ -193,10 +194,10 @@ final class EffectiveRibInWriter {
      */
     private final class TableListener implements DOMDataTreeChangeListener {
         private final Map<YangInstanceIdentifier, ListenerRegistration<?>> routeListeners = new HashMap<>();
+        private final RIBExtensionConsumerContext registry;
         private final DOMDataTreeChangeService service;
-        private final RIBSupportRegistry registry;
 
-        TableListener(final DOMDataTreeChangeService service, final RIBSupportRegistry registry) {
+        TableListener(final DOMDataTreeChangeService service, final RIBExtensionConsumerContext registry) {
             this.registry = Preconditions.checkNotNull(registry);
             this.service = Preconditions.checkNotNull(service);
         }
@@ -219,14 +220,17 @@ final class EffectiveRibInWriter {
                     }
                     break;
                 case WRITE:
-                    // FIXME: use codec to translate or refactor registry
+                    // FIXME: use codec to translate
                     final RIBSupport ribSupport = registry.getRIBSupport(null);
-                    final TableRouteListener routeListener = new TableRouteListener(ribSupport, peerKey, tableKey);
+                    if (ribSupport != null) {
+                        final TableRouteListener routeListener = new TableRouteListener(ribSupport, peerKey, tableKey);
+                        final ListenerRegistration<?> r = service.registerDataTreeChangeListener(
+                            new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,  tc.getRootPath()), routeListener);
 
-                    final ListenerRegistration<?> r = service.registerDataTreeChangeListener(
-                        new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,  tc.getRootPath()), routeListener);
-
-                    routeListeners.put(tc.getRootPath(), r);
+                        routeListeners.put(tc.getRootPath(), r);
+                    } else {
+                        LOG.warn("No RIB support for table {}, ignoring advertisements from peer %s", tableKey, peerKey);
+                    }
                     break;
                 default:
                     // No-op
diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/RIBSupportRegistry.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/RIBSupportRegistry.java
deleted file mode 100644 (file)
index d6606b9..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.protocol.bgp.rib.impl;
-
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-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;
-
-// FIXME: move to rib-spi/integrate with RIBExtension
-public interface RIBSupportRegistry {
-
-    @Nullable RIBSupport getRIBSupport(@Nonnull TablesKey k);
-
-}
diff --git a/bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/AbstractRIBSupportRegistration.java b/bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/AbstractRIBSupportRegistration.java
new file mode 100644 (file)
index 0000000..7c51d19
--- /dev/null
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.rib.spi;
+
+import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
+
+public abstract class AbstractRIBSupportRegistration<T extends RIBSupport> extends AbstractObjectRegistration<T> implements RIBSupportRegistration<T> {
+    protected AbstractRIBSupportRegistration(final T instance) {
+        super(instance);
+    }
+}
index 74c681af9de2316a83b37c80686c741e1a171c35..2b74785de927cdebb7c63f6c42fc00042d6fdf66 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.protocol.bgp.rib.spi;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
 
@@ -26,4 +27,21 @@ public interface RIBExtensionConsumerContext {
      *         not implemented.
      */
     @Nullable AdjRIBsFactory getAdjRIBsInFactory(@Nonnull Class<? extends AddressFamily> afi, @Nonnull Class<? extends SubsequentAddressFamily> safi);
+
+    /**
+     * Acquire a RIB implementation factory for a AFI/SAFI combination.
+     * @param key AFI/SAFI key
+     * @return RIBSupport instance, or null if the AFI/SAFI is
+     *         not implemented.
+     */
+    @Nullable RIBSupport getRIBSupport(@Nonnull TablesKey key);
+
+    /**
+     * Acquire a RIB implementation factory for a AFI/SAFI combination.
+     * @param afi Address Family Identifier
+     * @param safi Subsequent Address Family identifier
+     * @return RIBSupport instance, or null if the AFI/SAFI is
+     *         not implemented.
+     */
+    @Nullable RIBSupport getRIBSupport(@Nonnull Class<? extends AddressFamily> afi, @Nonnull Class<? extends SubsequentAddressFamily> safi);
 }
\ No newline at end of file
index 0995b85e046650f9fbb5c05a78063dc3ffb55a8c..9eca6605dc34d16f71578366252f693aaa8222e4 100644 (file)
@@ -26,4 +26,15 @@ public interface RIBExtensionProviderContext extends RIBExtensionConsumerContext
      */
     AutoCloseable registerAdjRIBsInFactory(Class<? extends AddressFamily> afi, Class<? extends SubsequentAddressFamily> safi,
             AdjRIBsFactory factory);
+
+    /**
+     * Register a RIBSupport instance for a particular AFI/SAFI combination.
+     *
+     * @param afi Address Family identifier
+     * @param safi Subsequent Address Family identifier
+     * @param support RIBSupport instance
+     * @return Registration handle. Call {@link RIBSupportRegistration#close()} method to remove it.
+     * @throws NullPointerException if any of the arguments is null
+     */
+    <T extends RIBSupport> RIBSupportRegistration<T> registerRIBSupport(Class<? extends AddressFamily> afi, Class<? extends SubsequentAddressFamily> safi, T support);
 }
\ No newline at end of file
diff --git a/bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/RIBSupportRegistration.java b/bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/RIBSupportRegistration.java
new file mode 100644 (file)
index 0000000..1660b7c
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.rib.spi;
+
+import org.opendaylight.yangtools.concepts.ObjectRegistration;
+
+/**
+ * A registration of a {@link RIBSupport} instance.
+ *
+ * @param <T> {@link RIBSupport} type
+ */
+public interface RIBSupportRegistration<T extends RIBSupport> extends ObjectRegistration<T> {
+    @Override
+    void close();
+}
index bea86bce9433b307fb2a300a389745b79937c148..41e4d2a98012942f3cdccac5a31d0a0366f37690 100644 (file)
@@ -7,16 +7,17 @@
  */
 package org.opendaylight.protocol.bgp.rib.spi;
 
-import java.util.Map;
+import com.google.common.base.Preconditions;
 import java.util.concurrent.ConcurrentHashMap;
-
+import java.util.concurrent.ConcurrentMap;
 import org.opendaylight.protocol.concepts.AbstractRegistration;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
 
 public class SimpleRIBExtensionProviderContext implements RIBExtensionProviderContext {
-    private final Map<TablesKey, AdjRIBsFactory> factories = new ConcurrentHashMap<>();
+    private final ConcurrentMap<TablesKey, AdjRIBsFactory> factories = new ConcurrentHashMap<>();
+    private final ConcurrentMap<TablesKey, RIBSupport> supports = new ConcurrentHashMap<>();
 
     @Override
     public final synchronized AbstractRegistration registerAdjRIBsInFactory(final Class<? extends AddressFamily> afi,
@@ -29,11 +30,10 @@ public class SimpleRIBExtensionProviderContext implements RIBExtensionProviderCo
 
         this.factories.put(key, factory);
 
-        final Object lock = this;
         return new AbstractRegistration() {
             @Override
             protected void removeRegistration() {
-                synchronized (lock) {
+                synchronized (SimpleRIBExtensionProviderContext.this) {
                     SimpleRIBExtensionProviderContext.this.factories.remove(key);
                 }
             }
@@ -45,4 +45,30 @@ public class SimpleRIBExtensionProviderContext implements RIBExtensionProviderCo
             final Class<? extends SubsequentAddressFamily> safi) {
         return this.factories.get(new TablesKey(afi, safi));
     }
+
+    @Override
+    public <T extends RIBSupport> RIBSupportRegistration<T> registerRIBSupport(final Class<? extends AddressFamily> afi,
+            final Class<? extends SubsequentAddressFamily> safi, final T support) {
+        final TablesKey key = new TablesKey(afi, safi);
+
+        final RIBSupport prev = this.supports.putIfAbsent(key, support);
+        Preconditions.checkArgument(prev == null, "AFI %s SAFI %s is already registered with %s", afi, safi, prev);
+
+        return new AbstractRIBSupportRegistration<T>(support) {
+            @Override
+            protected void removeRegistration() {
+                SimpleRIBExtensionProviderContext.this.supports.remove(key);
+            }
+        };
+    }
+
+    @Override
+    public RIBSupport getRIBSupport(final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
+        return getRIBSupport(new TablesKey(afi, safi));
+    }
+
+    @Override
+    public RIBSupport getRIBSupport(final TablesKey key) {
+        return this.supports.get(Preconditions.checkNotNull(key));
+    }
 }