BUG-7573: add BucketStore source monitoring
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / RoutingTable.java
index d21d05d7fe9b02ae9a604a2f6c0567706ec2f68a..90b069e58785aa95c50b36734aee9d5eaadffab4 100644 (file)
@@ -8,78 +8,71 @@
 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.base.Preconditions;
 import java.io.Serializable;
 import java.util.HashMap;
 import java.util.Map;
+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;
+import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier;
+
+public class RoutingTable implements BucketData<RoutingTable>, Serializable {
+    private static final long serialVersionUID = 5592610415175278760L;
 
-public class RoutingTable implements Copier<RoutingTable>, Serializable {
+    private final Map<RouteIdentifier<?, ?, ?>, Long> table;
+    private final ActorRef router;
 
-    private Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> table = new HashMap<>();
-    private ActorRef router;
+    private RoutingTable(final ActorRef router, final Map<RouteIdentifier<?, ?, ?>, Long> table) {
+        this.router = Preconditions.checkNotNull(router);
+        this.table = Preconditions.checkNotNull(table);
+    }
+
+    RoutingTable(final ActorRef router) {
+        this(router, new HashMap<>());
+    }
 
     @Override
     public RoutingTable copy() {
-        RoutingTable copy = new RoutingTable();
-        copy.setTable(new HashMap<>(table));
-        copy.setRouter(this.getRouter());
-
-        return copy;
+        return new RoutingTable(router, new HashMap<>(table));
     }
 
-    public Option<Pair<ActorRef, Long>> getRouterFor(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
-        Long updatedTime = table.get(routeId);
+    @Override
+    public Optional<ActorRef> getWatchActor() {
+        return Optional.of(router);
+    }
 
-        if (updatedTime == null || router == null)
-            return Option.none();
-        else
-            return Option.option(new Pair<>(router, updatedTime));
+    public Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRoutes() {
+        return table.keySet();
     }
 
-    public void addRoute(RpcRouter.RouteIdentifier<?,?,?> routeId){
+    public void addRoute(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
         table.put(routeId, System.currentTimeMillis());
     }
 
-    public void removeRoute(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
+    public void removeRoute(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
         table.remove(routeId);
     }
 
-    public Boolean contains(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
+    public boolean contains(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
         return table.containsKey(routeId);
     }
 
-    public Boolean isEmpty(){
+    public boolean isEmpty() {
         return table.isEmpty();
     }
-    ///
-    /// Getter, Setters
-    ///
-    //TODO: Remove public
-    public Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> getTable() {
-        return table;
-    }
 
-    void setTable(Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> table) {
-        this.table = table;
+    public int size() {
+        return table.size();
     }
 
     public ActorRef getRouter() {
         return router;
     }
 
-    public void setRouter(ActorRef router) {
-        this.router = router;
-    }
-
     @Override
     public String toString() {
-        return "RoutingTable{" +
-                "table=" + table +
-                ", router=" + router +
-                '}';
+        return "RoutingTable{" + "table=" + table + ", router=" + router + '}';
     }
 }