Bump versions to 2.0.2-SNAPSHOT
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / RoutingTable.java
index c25aa52..62312c1 100644 (file)
@@ -8,76 +8,92 @@
 package org.opendaylight.controller.remote.rpc.registry;
 
 import akka.actor.ActorRef;
-import akka.japi.Option;
-import akka.japi.Pair;
-import org.opendaylight.controller.remote.rpc.registry.gossip.Copier;
-import org.opendaylight.controller.sal.connector.api.RpcRouter;
-
-import java.io.Serializable;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-public class RoutingTable implements Copier<RoutingTable>, Serializable {
-
-    private Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> table = new HashMap<>();
-    private ActorRef router;
-
-    @Override
-    public RoutingTable copy() {
-        RoutingTable copy = new RoutingTable();
-        copy.setTable(Collections.unmodifiableMap(table));
-        copy.setRouter(this.getRouter());
-
-        return copy;
-    }
-
-    public Option<Pair<ActorRef, Long>> getRouterFor(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
-        Long updatedTime = table.get(routeId);
-
-        if (updatedTime == null || router == null)
-            return Option.none();
-        else
-            return Option.option(new Pair<>(router, updatedTime));
-    }
-
-    public void addRoute(RpcRouter.RouteIdentifier<?,?,?> routeId){
-        table.put(routeId, System.currentTimeMillis());
-    }
-
-    public void removeRoute(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
-        table.remove(routeId);
-    }
-
-    public Boolean contains(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
-        return table.containsKey(routeId);
+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.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;
+
+public final class RoutingTable extends AbstractRoutingTable<RoutingTable, DOMRpcIdentifier> {
+    private static final class Proxy implements Externalizable {
+        private static final long serialVersionUID = 1L;
+
+        @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
+        private Collection<DOMRpcIdentifier> rpcs;
+        private ActorRef opsInvoker;
+
+        // 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.getItems();
+            opsInvoker = table.getInvoker();
+        }
+
+        @Override
+        public void writeExternal(final ObjectOutput out) throws IOException {
+            out.writeObject(Serialization.serializedActorPath(opsInvoker));
+
+            try (NormalizedNodeDataOutput nnout = NormalizedNodeStreamVersion.current().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 {
+            opsInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
+
+            final NormalizedNodeDataInput nnin = NormalizedNodeDataInput.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(opsInvoker, rpcs);
+        }
     }
 
-    ///
-    /// Getter, Setters
-    ///
-    //TODO: Remove public
-    public Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> getTable() {
-        return table;
-    }
+    private static final long serialVersionUID = 1L;
 
-    void setTable(Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> table) {
-        this.table = table;
+    RoutingTable(final ActorRef invoker, final Collection<DOMRpcIdentifier> table) {
+        super(invoker, table);
     }
 
-    public ActorRef getRouter() {
-        return router;
+    RoutingTable addRpcs(final Collection<DOMRpcIdentifier> toAdd) {
+        final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(getItems());
+        newRpcs.addAll(toAdd);
+        return new RoutingTable(getInvoker(), newRpcs);
     }
 
-    public void setRouter(ActorRef router) {
-        this.router = 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);
     }
 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.