Merge "BUG 1839 - HTTP delete of non existing data"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RoutedRpcListener.java
index a6eeac02708a20e8a746581d80c5054a776ed671..2aaac5a78ed531fc830bfca7540d2603a2e0f41b 100644 (file)
@@ -13,15 +13,14 @@ import akka.actor.ActorRef;
 import com.google.common.base.Preconditions;
 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
-import org.opendaylight.controller.remote.rpc.messages.AddRoutedRpc;
-import org.opendaylight.controller.remote.rpc.messages.RemoveRoutedRpc;
-import org.opendaylight.controller.remote.rpc.utils.ActorUtil;
+import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
 import org.opendaylight.controller.sal.connector.api.RpcRouter;
 import org.opendaylight.controller.sal.core.api.RpcRoutingContext;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
@@ -29,23 +28,24 @@ import java.util.Set;
 public class RoutedRpcListener implements RouteChangeListener<RpcRoutingContext, YangInstanceIdentifier>{
   private static final Logger LOG = LoggerFactory.getLogger(RoutedRpcListener.class);
   private final ActorRef rpcRegistry;
-  private final String actorPath;
 
-  public RoutedRpcListener(ActorRef rpcRegistry, String actorPath) {
+  public RoutedRpcListener(ActorRef rpcRegistry) {
     Preconditions.checkNotNull(rpcRegistry, "rpc registry actor should not be null");
-    Preconditions.checkNotNull(actorPath, "actor path of rpc broker on current node should not be null");
 
     this.rpcRegistry = rpcRegistry;
-    this.actorPath = actorPath;
   }
 
   @Override
   public void onRouteChange(RouteChange<RpcRoutingContext, YangInstanceIdentifier> routeChange) {
     Map<RpcRoutingContext, Set<YangInstanceIdentifier>> announcements = routeChange.getAnnouncements();
-    announce(getRouteIdentifiers(announcements));
+    if(announcements != null && announcements.size() > 0){
+      announce(getRouteIdentifiers(announcements));
+    }
 
     Map<RpcRoutingContext, Set<YangInstanceIdentifier>> removals = routeChange.getRemovals();
-    remove(getRouteIdentifiers(removals));
+    if(removals != null && removals.size() > 0 ) {
+      remove(getRouteIdentifiers(removals));
+    }
   }
 
   /**
@@ -53,14 +53,11 @@ public class RoutedRpcListener implements RouteChangeListener<RpcRoutingContext,
    * @param announcements
    */
   private void announce(Set<RpcRouter.RouteIdentifier<?, ?, ?>> announcements) {
-    LOG.debug("Announcing [{}]", announcements);
-    AddRoutedRpc addRpcMsg = new AddRoutedRpc(announcements, actorPath);
-    try {
-      ActorUtil.executeLocalOperation(rpcRegistry, addRpcMsg, ActorUtil.LOCAL_ASK_DURATION, ActorUtil.LOCAL_AWAIT_DURATION);
-    } catch (Exception e) {
-      // Just logging it because Akka API throws this exception
-      LOG.error(e.toString());
+    if(LOG.isDebugEnabled()) {
+        LOG.debug("Announcing [{}]", announcements);
     }
+    RpcRegistry.Messages.AddOrUpdateRoutes addRpcMsg = new RpcRegistry.Messages.AddOrUpdateRoutes(new ArrayList<>(announcements));
+    rpcRegistry.tell(addRpcMsg, ActorRef.noSender());
   }
 
   /**
@@ -68,14 +65,11 @@ public class RoutedRpcListener implements RouteChangeListener<RpcRoutingContext,
    * @param removals
    */
   private void remove(Set<RpcRouter.RouteIdentifier<?, ?, ?>> removals){
-    LOG.debug("Removing [{}]", removals);
-    RemoveRoutedRpc removeRpcMsg = new RemoveRoutedRpc(removals, actorPath);
-    try {
-      ActorUtil.executeLocalOperation(rpcRegistry, removeRpcMsg, ActorUtil.LOCAL_ASK_DURATION, ActorUtil.LOCAL_AWAIT_DURATION);
-    } catch (Exception e) {
-      // Just logging it because Akka API throws this exception
-      LOG.error(e.toString());
+    if(LOG.isDebugEnabled()) {
+        LOG.debug("Removing [{}]", removals);
     }
+    RpcRegistry.Messages.RemoveRoutes removeRpcMsg = new RpcRegistry.Messages.RemoveRoutes(new ArrayList<>(removals));
+    rpcRegistry.tell(removeRpcMsg, ActorRef.noSender());
   }
 
   /**