Remove unneded RoutingTable time tracking
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / RoutingTable.java
index c25aa523e2e0823769a28e9dc01b32da711b0ae5..0e68eb87096828f89892b338969aaaea24f13954 100644 (file)
@@ -8,76 +8,69 @@
 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 com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
 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;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+import org.opendaylight.controller.remote.rpc.registry.gossip.BucketData;
+import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier;
+
+public class RoutingTable implements BucketData<RoutingTable>, Serializable {
+    private static final long serialVersionUID = 1L;
+
+    private final Set<RouteIdentifier<?, ?, ?>> rpcs;
+    private final ActorRef rpcInvoker;
+
+    private RoutingTable(final ActorRef rpcInvoker, final Set<RouteIdentifier<?, ?, ?>> table) {
+        this.rpcInvoker = Preconditions.checkNotNull(rpcInvoker);
+        this.rpcs = ImmutableSet.copyOf(table);
     }
 
-    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));
+    RoutingTable(final ActorRef rpcInvoker) {
+        this(rpcInvoker, ImmutableSet.of());
     }
 
-    public void addRoute(RpcRouter.RouteIdentifier<?,?,?> routeId){
-        table.put(routeId, System.currentTimeMillis());
+    @Override
+    public Optional<ActorRef> getWatchActor() {
+        return Optional.of(rpcInvoker);
     }
 
-    public void removeRoute(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
-        table.remove(routeId);
+    public Set<RouteIdentifier<?, ?, ?>> getRoutes() {
+        return rpcs;
     }
 
-    public Boolean contains(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
-        return table.containsKey(routeId);
+    ActorRef getRpcInvoker() {
+        return rpcInvoker;
     }
 
-    ///
-    /// Getter, Setters
-    ///
-    //TODO: Remove public
-    public Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> getTable() {
-        return table;
+    RoutingTable addRpcs(final Collection<RouteIdentifier<?, ?, ?>> toAdd) {
+        final Set<RouteIdentifier<?, ?, ?>> newRpcs = new HashSet<>(rpcs);
+        newRpcs.addAll(toAdd);
+        return new RoutingTable(rpcInvoker, newRpcs);
     }
 
-    void setTable(Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> table) {
-        this.table = table;
+    RoutingTable removeRpcs(final Collection<RouteIdentifier<?, ?, ?>> toRemove) {
+        final Set<RouteIdentifier<?, ?, ?>> newRpcs = new HashSet<>(rpcs);
+        newRpcs.removeAll(toRemove);
+        return new RoutingTable(rpcInvoker, newRpcs);
     }
 
-    public ActorRef getRouter() {
-        return router;
+    @VisibleForTesting
+    boolean contains(final RouteIdentifier<?, ?, ?> routeId) {
+        return rpcs.contains(routeId);
     }
 
-    public void setRouter(ActorRef router) {
-        this.router = router;
+    @VisibleForTesting
+    int size() {
+        return rpcs.size();
     }
 
     @Override
     public String toString() {
-        return "RoutingTable{" +
-                "table=" + table +
-                ", router=" + router +
-                '}';
+        return "RoutingTable{" + "rpcs=" + rpcs + ", rpcInvoker=" + rpcInvoker + '}';
     }
 }