BUG-7594: Rework sal-remoterpc-connector messages
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / RoutingTable.java
index 90b069e58785aa95c50b36734aee9d5eaadffab4..2f9513175776cd72b879437832ffa6c34d494f06 100644 (file)
 package org.opendaylight.controller.remote.rpc.registry;
 
 import akka.actor.ActorRef;
+import akka.serialization.JavaSerializer;
+import akka.serialization.Serialization;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
 import java.io.Serializable;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
 import java.util.Optional;
 import java.util.Set;
+import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
+import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
+import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketData;
-import org.opendaylight.controller.sal.connector.api.RpcRouter;
-import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier;
 
-public class RoutingTable implements BucketData<RoutingTable>, Serializable {
-    private static final long serialVersionUID = 5592610415175278760L;
+public final class RoutingTable implements BucketData<RoutingTable>, Serializable {
+    private static final class Proxy implements Externalizable {
+        private static final long serialVersionUID = 1L;
 
-    private final Map<RouteIdentifier<?, ?, ?>, Long> table;
-    private final ActorRef router;
+        @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
+        private Collection<DOMRpcIdentifier> rpcs;
+        private ActorRef rpcInvoker;
 
-    private RoutingTable(final ActorRef router, final Map<RouteIdentifier<?, ?, ?>, Long> table) {
-        this.router = Preconditions.checkNotNull(router);
-        this.table = Preconditions.checkNotNull(table);
-    }
+        // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
+        // be able to create instances via reflection.
+        @SuppressWarnings("checkstyle:RedundantModifier")
+        public Proxy() {
+            // For Externalizable
+        }
+
+        Proxy(final RoutingTable table) {
+            rpcs = table.getRoutes();
+            rpcInvoker = table.getRpcInvoker();
+        }
+
+        @Override
+        public void writeExternal(final ObjectOutput out) throws IOException {
+            out.writeObject(Serialization.serializedActorPath(rpcInvoker));
+
+            final NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(out);
+            nnout.writeInt(rpcs.size());
+            for (DOMRpcIdentifier id : rpcs) {
+                nnout.writeSchemaPath(id.getType());
+                nnout.writeYangInstanceIdentifier(id.getContextReference());
+            }
+        }
+
+        @Override
+        public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
+            rpcInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
 
-    RoutingTable(final ActorRef router) {
-        this(router, new HashMap<>());
+            final NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(in);
+            final int size = nnin.readInt();
+            rpcs = new ArrayList<>(size);
+            for (int i = 0; i < size; ++i) {
+                rpcs.add(DOMRpcIdentifier.create(nnin.readSchemaPath(), nnin.readYangInstanceIdentifier()));
+            }
+        }
+
+        private Object readResolve() {
+            return new RoutingTable(rpcInvoker, rpcs);
+        }
     }
 
-    @Override
-    public RoutingTable copy() {
-        return new RoutingTable(router, new HashMap<>(table));
+    private static final long serialVersionUID = 1L;
+
+    @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
+    private final Set<DOMRpcIdentifier> rpcs;
+    private final ActorRef rpcInvoker;
+
+    RoutingTable(final ActorRef rpcInvoker, final Collection<DOMRpcIdentifier> table) {
+        this.rpcInvoker = Preconditions.checkNotNull(rpcInvoker);
+        this.rpcs = ImmutableSet.copyOf(table);
     }
 
     @Override
     public Optional<ActorRef> getWatchActor() {
-        return Optional.of(router);
+        return Optional.of(rpcInvoker);
     }
 
-    public Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRoutes() {
-        return table.keySet();
+    public Set<DOMRpcIdentifier> getRoutes() {
+        return rpcs;
     }
 
-    public void addRoute(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
-        table.put(routeId, System.currentTimeMillis());
+    ActorRef getRpcInvoker() {
+        return rpcInvoker;
     }
 
-    public void removeRoute(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
-        table.remove(routeId);
+    RoutingTable addRpcs(final Collection<DOMRpcIdentifier> toAdd) {
+        final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(rpcs);
+        newRpcs.addAll(toAdd);
+        return new RoutingTable(rpcInvoker, newRpcs);
     }
 
-    public boolean contains(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
-        return table.containsKey(routeId);
+    RoutingTable removeRpcs(final Collection<DOMRpcIdentifier> toRemove) {
+        final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(rpcs);
+        newRpcs.removeAll(toRemove);
+        return new RoutingTable(rpcInvoker, newRpcs);
     }
 
-    public boolean isEmpty() {
-        return table.isEmpty();
+    private Object writeReplace() {
+        return new Proxy(this);
     }
 
-    public int size() {
-        return table.size();
+    @VisibleForTesting
+    boolean contains(final DOMRpcIdentifier routeId) {
+        return rpcs.contains(routeId);
     }
 
-    public ActorRef getRouter() {
-        return router;
+    @VisibleForTesting
+    int size() {
+        return rpcs.size();
     }
 
     @Override
     public String toString() {
-        return "RoutingTable{" + "table=" + table + ", router=" + router + '}';
+        return "RoutingTable{" + "rpcs=" + rpcs + ", rpcInvoker=" + rpcInvoker + '}';
     }
 }