package org.opendaylight.controller.remote.rpc.registry;
import akka.actor.ActorRef;
-import akka.japi.Option;
-import akka.japi.Pair;
+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.HashMap;
-import java.util.Map;
-import org.opendaylight.controller.remote.rpc.registry.gossip.Copier;
-import org.opendaylight.controller.sal.connector.api.RpcRouter;
+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 Copier<RoutingTable>, Serializable {
- private static final long serialVersionUID = 5592610415175278760L;
+public class RoutingTable implements BucketData<RoutingTable>, Serializable {
+ private static final long serialVersionUID = 1L;
- private final Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> table = new HashMap<>();
- private ActorRef router;
+ private final Set<RouteIdentifier<?, ?, ?>> rpcs;
+ private final ActorRef rpcInvoker;
- @Override
- public RoutingTable copy() {
- RoutingTable copy = new RoutingTable();
- copy.table.putAll(table);
- copy.setRouter(this.getRouter());
-
- return copy;
+ 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;
}
- public boolean isEmpty(){
- return table.isEmpty();
+ RoutingTable addRpcs(final Collection<RouteIdentifier<?, ?, ?>> toAdd) {
+ final Set<RouteIdentifier<?, ?, ?>> newRpcs = new HashSet<>(rpcs);
+ newRpcs.addAll(toAdd);
+ return new RoutingTable(rpcInvoker, newRpcs);
}
- public int size() {
- return table.size();
+ 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 + '}';
}
}