X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-remoterpc-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fremote%2Frpc%2Fregistry%2FRoutingTable.java;h=2f9513175776cd72b879437832ffa6c34d494f06;hp=d99faabfe45365fdc69dd7c45fa566f09e43f6e4;hb=5b66dd8f5e3467a07e77b20fe696b29993ce5565;hpb=aed240ecf3096404bce48fb61a13e528a5ee96ce diff --git a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RoutingTable.java b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RoutingTable.java index d99faabfe4..2f95131757 100644 --- a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RoutingTable.java +++ b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RoutingTable.java @@ -8,79 +8,129 @@ 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 akka.serialization.JavaSerializer; +import akka.serialization.Serialization; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; +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.io.Serializable; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class RoutingTable implements Copier, Serializable { +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; +import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput; +import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput; +import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier; +import org.opendaylight.controller.remote.rpc.registry.gossip.BucketData; + +public final class RoutingTable implements BucketData, Serializable { + 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 rpcs; + private ActorRef rpcInvoker; + + // 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.getRoutes(); + rpcInvoker = table.getRpcInvoker(); + } + + @Override + public void writeExternal(final ObjectOutput out) throws IOException { + out.writeObject(Serialization.serializedActorPath(rpcInvoker)); + + final NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.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 { + rpcInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject()); + + final NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.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(rpcInvoker, rpcs); + } + } - private Map, Long> table = new HashMap<>(); - private ActorRef router; + private static final long serialVersionUID = 1L; - @Override - public RoutingTable copy() { - RoutingTable copy = new RoutingTable(); - copy.setTable(Collections.unmodifiableMap(table)); - copy.setRouter(this.getRouter()); + @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.") + private final Set rpcs; + private final ActorRef rpcInvoker; - return copy; + RoutingTable(final ActorRef rpcInvoker, final Collection table) { + this.rpcInvoker = Preconditions.checkNotNull(rpcInvoker); + this.rpcs = ImmutableSet.copyOf(table); } - public Option> 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)); + @Override + public Optional getWatchActor() { + return Optional.of(rpcInvoker); } - public void addRoute(RpcRouter.RouteIdentifier routeId){ - table.put(routeId, System.currentTimeMillis()); + public Set getRoutes() { + return rpcs; } - public void removeRoute(RpcRouter.RouteIdentifier routeId){ - table.remove(routeId); + ActorRef getRpcInvoker() { + return rpcInvoker; } - public Boolean contains(RpcRouter.RouteIdentifier routeId){ - return table.containsKey(routeId); + RoutingTable addRpcs(final Collection toAdd) { + final Set newRpcs = new HashSet<>(rpcs); + newRpcs.addAll(toAdd); + return new RoutingTable(rpcInvoker, newRpcs); } - public Boolean isEmpty(){ - return table.isEmpty(); - } - /// - /// Getter, Setters - /// - //TODO: Remove public - public Map, Long> getTable() { - return table; + RoutingTable removeRpcs(final Collection toRemove) { + final Set newRpcs = new HashSet<>(rpcs); + newRpcs.removeAll(toRemove); + return new RoutingTable(rpcInvoker, newRpcs); } - void setTable(Map, Long> table) { - this.table = table; + private Object writeReplace() { + return new Proxy(this); } - public ActorRef getRouter() { - return router; + @VisibleForTesting + boolean contains(final DOMRpcIdentifier 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 + '}'; } }