Add support for reusable streaming
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RpcListener.java
index 0d0335e18d0a27c15e69e5fdc0f864444e611fd0..20f32cb0da4a4a26a625c1ae901d90ea34016ca3 100644 (file)
@@ -5,60 +5,54 @@
  * 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.controller.remote.rpc;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
 
 import akka.actor.ActorRef;
-import com.google.common.base.Preconditions;
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.List;
-import javax.annotation.Nonnull;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
-import org.opendaylight.controller.sal.connector.api.RpcRouter;
+import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.AddOrUpdateRoutes;
+import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.RemoveRoutes;
+import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
+import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
+import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class RpcListener implements DOMRpcAvailabilityListener{
+/**
+ * A {@link DOMRpcAvailabilityListener} reacting to RPC implementations different than {@link RemoteRpcImplementation}.
+ * The knowledge of such implementations is forwarded to {@link RpcRegistry}, which is responsible for advertising
+ * their presence to other nodes.
+ */
+final class RpcListener implements DOMRpcAvailabilityListener {
+    private static final Logger LOG = LoggerFactory.getLogger(RpcListener.class);
+
+    private final ActorRef rpcRegistry;
 
-  private static final Logger LOG = LoggerFactory.getLogger(RpcListener.class);
-  private final ActorRef rpcRegistry;
+    RpcListener(final ActorRef rpcRegistry) {
+        this.rpcRegistry = requireNonNull(rpcRegistry);
+    }
 
-  public RpcListener(final ActorRef rpcRegistry) {
-    this.rpcRegistry = rpcRegistry;
-  }
+    @Override
+    public void onRpcAvailable(final Collection<DOMRpcIdentifier> rpcs) {
+        checkArgument(rpcs != null, "Input Collection of DOMRpcIdentifier can not be null.");
+        LOG.debug("Adding registration for [{}]", rpcs);
+
+        rpcRegistry.tell(new AddOrUpdateRoutes(rpcs), ActorRef.noSender());
+    }
 
     @Override
-    public void onRpcAvailable(@Nonnull final Collection<DOMRpcIdentifier> rpcs) {
-        Preconditions.checkArgument(rpcs != null, "Input Collection of DOMRpcIdentifier can not be null.");
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Adding registration for [{}]", rpcs);
-        }
-        final List<RpcRouter.RouteIdentifier<?,?,?>> routeIds = new ArrayList<>();
+    public void onRpcUnavailable(final Collection<DOMRpcIdentifier> rpcs) {
+        checkArgument(rpcs != null, "Input Collection of DOMRpcIdentifier can not be null.");
 
-        for (final DOMRpcIdentifier rpc : rpcs) {
-            final RpcRouter.RouteIdentifier<?,?,?> routeId = new RouteIdentifierImpl(null, rpc.getType().getLastComponent(), rpc.getContextReference());
-            routeIds.add(routeId);
-        }
-        final RpcRegistry.Messages.AddOrUpdateRoutes addRpcMsg = new RpcRegistry.Messages.AddOrUpdateRoutes(routeIds);
-        rpcRegistry.tell(addRpcMsg, ActorRef.noSender());
+        LOG.debug("Removing registration for [{}]", rpcs);
+        rpcRegistry.tell(new RemoveRoutes(rpcs), ActorRef.noSender());
     }
 
     @Override
-    public void onRpcUnavailable(@Nonnull final Collection<DOMRpcIdentifier> rpcs) {
-        Preconditions.checkArgument(rpcs != null, "Input Collection of DOMRpcIdentifier can not be null.");
-        if(LOG.isDebugEnabled()) {
-            LOG.debug("Removing registration for [{}]", rpcs);
-        }
-        final List<RpcRouter.RouteIdentifier<?,?,?>> routeIds = new ArrayList<>();
-        for (final DOMRpcIdentifier rpc : rpcs) {
-            final RpcRouter.RouteIdentifier<?,?,?> routeId = new RouteIdentifierImpl(null, rpc.getType().getLastComponent(), rpc.getContextReference());
-            routeIds.add(routeId);
-        }
-        final RpcRegistry.Messages.RemoveRoutes removeRpcMsg = new RpcRegistry.Messages.RemoveRoutes(routeIds);
-        rpcRegistry.tell(removeRpcMsg, ActorRef.noSender());
+    public boolean acceptsImplementation(final DOMRpcImplementation impl) {
+        return !(impl instanceof RemoteRpcImplementation);
     }
 }