2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.remote.rpc.registry;
10 import akka.actor.ActorRef;
11 import akka.serialization.JavaSerializer;
12 import akka.serialization.Serialization;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableSet;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.io.Externalizable;
18 import java.io.IOException;
19 import java.io.ObjectInput;
20 import java.io.ObjectOutput;
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.Optional;
27 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
28 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
29 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
30 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketData;
31 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
33 public final class RoutingTable implements BucketData<RoutingTable>, Serializable {
34 private static final class Proxy implements Externalizable {
35 private static final long serialVersionUID = 1L;
37 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
38 private Collection<DOMRpcIdentifier> rpcs;
39 private ActorRef rpcInvoker;
41 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
42 // be able to create instances via reflection.
43 @SuppressWarnings("checkstyle:RedundantModifier")
48 Proxy(final RoutingTable table) {
49 rpcs = table.getRoutes();
50 rpcInvoker = table.getRpcInvoker();
54 public void writeExternal(final ObjectOutput out) throws IOException {
55 out.writeObject(Serialization.serializedActorPath(rpcInvoker));
57 final NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(out);
58 nnout.writeInt(rpcs.size());
59 for (DOMRpcIdentifier id : rpcs) {
60 nnout.writeSchemaPath(id.getType());
61 nnout.writeYangInstanceIdentifier(id.getContextReference());
66 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
67 rpcInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
69 final NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(in);
70 final int size = nnin.readInt();
71 rpcs = new ArrayList<>(size);
72 for (int i = 0; i < size; ++i) {
73 rpcs.add(DOMRpcIdentifier.create(nnin.readSchemaPath(), nnin.readYangInstanceIdentifier()));
77 private Object readResolve() {
78 return new RoutingTable(rpcInvoker, rpcs);
82 private static final long serialVersionUID = 1L;
84 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
85 private final Set<DOMRpcIdentifier> rpcs;
86 private final ActorRef rpcInvoker;
88 RoutingTable(final ActorRef rpcInvoker, final Collection<DOMRpcIdentifier> table) {
89 this.rpcInvoker = Preconditions.checkNotNull(rpcInvoker);
90 this.rpcs = ImmutableSet.copyOf(table);
94 public Optional<ActorRef> getWatchActor() {
95 return Optional.of(rpcInvoker);
98 public Set<DOMRpcIdentifier> getRoutes() {
102 ActorRef getRpcInvoker() {
106 RoutingTable addRpcs(final Collection<DOMRpcIdentifier> toAdd) {
107 final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(rpcs);
108 newRpcs.addAll(toAdd);
109 return new RoutingTable(rpcInvoker, newRpcs);
112 RoutingTable removeRpcs(final Collection<DOMRpcIdentifier> toRemove) {
113 final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(rpcs);
114 newRpcs.removeAll(toRemove);
115 return new RoutingTable(rpcInvoker, newRpcs);
118 private Object writeReplace() {
119 return new Proxy(this);
123 boolean contains(final DOMRpcIdentifier routeId) {
124 return rpcs.contains(routeId);
133 public String toString() {
134 return "RoutingTable{" + "rpcs=" + rpcs + ", rpcInvoker=" + rpcInvoker + '}';