package org.opendaylight.controller.remote.rpc.registry;
import akka.actor.ActorRef;
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableSet;
-import java.io.Serializable;
+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.Optional;
import java.util.Set;
-import org.opendaylight.controller.remote.rpc.registry.gossip.BucketData;
-import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier;
+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.mdsal.dom.api.DOMRpcIdentifier;
-public class RoutingTable implements BucketData<RoutingTable>, Serializable {
- private static final long serialVersionUID = 1L;
+public final class RoutingTable extends AbstractRoutingTable<RoutingTable, DOMRpcIdentifier> {
+ private static final class Proxy implements Externalizable {
+ private static final long serialVersionUID = 1L;
- private final Set<RouteIdentifier<?, ?, ?>> rpcs;
- private final ActorRef rpcInvoker;
+ @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
+ private Collection<DOMRpcIdentifier> rpcs;
+ private ActorRef opsInvoker;
- private RoutingTable(final ActorRef rpcInvoker, final Set<RouteIdentifier<?, ?, ?>> table) {
- this.rpcInvoker = Preconditions.checkNotNull(rpcInvoker);
- this.rpcs = ImmutableSet.copyOf(table);
- }
+ // 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
+ }
- RoutingTable(final ActorRef rpcInvoker) {
- this(rpcInvoker, ImmutableSet.of());
- }
+ Proxy(final RoutingTable table) {
+ rpcs = table.getItems();
+ opsInvoker = table.getInvoker();
+ }
- @Override
- public Optional<ActorRef> getWatchActor() {
- return Optional.of(rpcInvoker);
- }
+ @Override
+ public void writeExternal(final ObjectOutput out) throws IOException {
+ out.writeObject(Serialization.serializedActorPath(opsInvoker));
- public Set<RouteIdentifier<?, ?, ?>> getRoutes() {
- return rpcs;
- }
+ final NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(out);
+ nnout.writeInt(rpcs.size());
+ for (DOMRpcIdentifier id : rpcs) {
+ nnout.writeSchemaPath(id.getType());
+ nnout.writeYangInstanceIdentifier(id.getContextReference());
+ }
+ }
- ActorRef getRpcInvoker() {
- return rpcInvoker;
- }
+ @Override
+ public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
+ opsInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
- RoutingTable addRpcs(final Collection<RouteIdentifier<?, ?, ?>> toAdd) {
- final Set<RouteIdentifier<?, ?, ?>> newRpcs = new HashSet<>(rpcs);
- newRpcs.addAll(toAdd);
- return new RoutingTable(rpcInvoker, newRpcs);
+ 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(opsInvoker, rpcs);
+ }
}
- RoutingTable removeRpcs(final Collection<RouteIdentifier<?, ?, ?>> toRemove) {
- final Set<RouteIdentifier<?, ?, ?>> newRpcs = new HashSet<>(rpcs);
- newRpcs.removeAll(toRemove);
- return new RoutingTable(rpcInvoker, newRpcs);
+ private static final long serialVersionUID = 1L;
+
+ RoutingTable(final ActorRef invoker, final Collection<DOMRpcIdentifier> table) {
+ super(invoker, table);
}
- @VisibleForTesting
- boolean contains(final RouteIdentifier<?, ?, ?> routeId) {
- return rpcs.contains(routeId);
+ RoutingTable addRpcs(final Collection<DOMRpcIdentifier> toAdd) {
+ final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(getItems());
+ newRpcs.addAll(toAdd);
+ return new RoutingTable(getInvoker(), newRpcs);
}
- @VisibleForTesting
- int size() {
- return rpcs.size();
+ RoutingTable removeRpcs(final Collection<DOMRpcIdentifier> toRemove) {
+ final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(getItems());
+ newRpcs.removeAll(toRemove);
+ return new RoutingTable(getInvoker(), newRpcs);
}
@Override
- public String toString() {
- return "RoutingTable{" + "rpcs=" + rpcs + ", rpcInvoker=" + rpcInvoker + '}';
+ Object writeReplace() {
+ return new Proxy(this);
}
}