Fix followerDistributedDataStore tear down
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / RoutingTable.java
index 0c68b55aa36f2566a89c6a653b7bd43e66c4d33e..f0795a3a42b89fd83fd6e37e6c2141b7c653c977 100644 (file)
@@ -8,65 +8,96 @@
 package org.opendaylight.controller.remote.rpc.registry;
 
 import akka.actor.ActorRef;
-import com.google.common.base.Preconditions;
-import java.io.Serializable;
-import java.util.HashMap;
-import java.util.Map;
+import akka.serialization.JavaSerializer;
+import akka.serialization.Serialization;
+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.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
 import java.util.Set;
-import org.opendaylight.controller.remote.rpc.registry.gossip.Copier;
-import org.opendaylight.controller.sal.connector.api.RpcRouter;
-import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier;
+import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
+import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
+import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataOutput;
+import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 
-public class RoutingTable implements Copier<RoutingTable>, Serializable {
-    private static final long serialVersionUID = 5592610415175278760L;
+public final class RoutingTable extends AbstractRoutingTable<RoutingTable, DOMRpcIdentifier> {
+    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 opsInvoker;
 
-    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
+        }
 
-    RoutingTable(final ActorRef router) {
-        this(router, new HashMap<>());
-    }
+        Proxy(final RoutingTable table) {
+            rpcs = table.getItems();
+            opsInvoker = table.getInvoker();
+        }
 
-    @Override
-    public RoutingTable copy() {
-        return new RoutingTable(router, new HashMap<>(table));
-    }
+        @Override
+        public void writeExternal(final ObjectOutput out) throws IOException {
+            out.writeObject(Serialization.serializedActorPath(opsInvoker));
 
-    public Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRoutes() {
-        return table.keySet();
-    }
+            try (NormalizedNodeDataOutput nnout = NormalizedNodeStreamVersion.current().newDataOutput(out)) {
+                nnout.writeInt(rpcs.size());
+                for (DOMRpcIdentifier id : rpcs) {
+                    // TODO: we should be able to get by with just a QName
+                    nnout.writeSchemaNodeIdentifier(Absolute.of(id.getType()));
+                    nnout.writeYangInstanceIdentifier(id.getContextReference());
+                }
+            }
+        }
 
-    public void addRoute(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
-        table.put(routeId, System.currentTimeMillis());
-    }
+        @Override
+        public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
+            opsInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
 
-    public void removeRoute(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
-        table.remove(routeId);
-    }
+            final NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(in);
+            final int size = nnin.readInt();
+            rpcs = new ArrayList<>(size);
+            for (int i = 0; i < size; ++i) {
+                // TODO: we should be able to get by with just a QName
+                rpcs.add(DOMRpcIdentifier.create(nnin.readSchemaNodeIdentifier().firstNodeIdentifier(),
+                    nnin.readYangInstanceIdentifier()));
+            }
+        }
 
-    public boolean contains(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
-        return table.containsKey(routeId);
+        private Object readResolve() {
+            return new RoutingTable(opsInvoker, rpcs);
+        }
     }
 
-    public boolean isEmpty() {
-        return table.isEmpty();
+    private static final long serialVersionUID = 1L;
+
+    RoutingTable(final ActorRef invoker, final Collection<DOMRpcIdentifier> table) {
+        super(invoker, table);
     }
 
-    public int size() {
-        return table.size();
+    RoutingTable addRpcs(final Collection<DOMRpcIdentifier> toAdd) {
+        final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(getItems());
+        newRpcs.addAll(toAdd);
+        return new RoutingTable(getInvoker(), newRpcs);
     }
 
-    public ActorRef getRouter() {
-        return router;
+    RoutingTable removeRpcs(final Collection<DOMRpcIdentifier> toRemove) {
+        final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(getItems());
+        newRpcs.removeAll(toRemove);
+        return new RoutingTable(getInvoker(), newRpcs);
     }
 
     @Override
-    public String toString() {
-        return "RoutingTable{" + "table=" + table + ", router=" + router + '}';
+    Object writeReplace() {
+        return new Proxy(this);
     }
 }