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=62312c14cb90cc60fed0cdec0435a050f8ad16ef;hp=d21d05d7fe9b02ae9a604a2f6c0567706ec2f68a;hb=HEAD;hpb=58d9dc1c6df02ee0c5f1d72e9f0a2f6361b7d23f 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 d21d05d7fe..f0795a3a42 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,78 +8,96 @@ 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.HashMap; -import java.util.Map; - -public class RoutingTable implements Copier, Serializable { - - private Map, Long> table = new HashMap<>(); - private ActorRef router; - - @Override - public RoutingTable copy() { - RoutingTable copy = new RoutingTable(); - copy.setTable(new HashMap<>(table)); - copy.setRouter(this.getRouter()); - - return copy; - } - - 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)); +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; +import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute; + +public final class RoutingTable extends AbstractRoutingTable { + 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 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) { + // TODO: we should be able to get by with just a QName + nnout.writeSchemaNodeIdentifier(Absolute.of(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) { + // TODO: we should be able to get by with just a QName + rpcs.add(DOMRpcIdentifier.create(nnin.readSchemaNodeIdentifier().firstNodeIdentifier(), + nnin.readYangInstanceIdentifier())); + } + } + + private Object readResolve() { + return new RoutingTable(opsInvoker, rpcs); + } } - 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); - } - - public Boolean isEmpty(){ - return table.isEmpty(); - } - /// - /// Getter, Setters - /// - //TODO: Remove public - public Map, Long> getTable() { - return table; - } + private static final long serialVersionUID = 1L; - void setTable(Map, Long> table) { - this.table = table; + RoutingTable(final ActorRef invoker, final Collection table) { + super(invoker, table); } - public ActorRef getRouter() { - return router; + RoutingTable addRpcs(final Collection toAdd) { + final Set newRpcs = new HashSet<>(getItems()); + newRpcs.addAll(toAdd); + return new RoutingTable(getInvoker(), newRpcs); } - public void setRouter(ActorRef router) { - this.router = router; + RoutingTable removeRpcs(final Collection toRemove) { + final Set 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); } }