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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashSet;
22 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
23 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
24 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataOutput;
25 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
27 public final class RoutingTable extends AbstractRoutingTable<RoutingTable, DOMRpcIdentifier> {
28 private static final class Proxy implements Externalizable {
29 private static final long serialVersionUID = 1L;
31 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
32 private Collection<DOMRpcIdentifier> rpcs;
33 private ActorRef opsInvoker;
35 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
36 // be able to create instances via reflection.
37 @SuppressWarnings("checkstyle:RedundantModifier")
42 Proxy(final RoutingTable table) {
43 rpcs = table.getItems();
44 opsInvoker = table.getInvoker();
48 public void writeExternal(final ObjectOutput out) throws IOException {
49 out.writeObject(Serialization.serializedActorPath(opsInvoker));
51 try (NormalizedNodeDataOutput nnout = NormalizedNodeStreamVersion.current().newDataOutput(out)) {
52 nnout.writeInt(rpcs.size());
53 for (DOMRpcIdentifier id : rpcs) {
54 nnout.writeSchemaPath(id.getType());
55 nnout.writeYangInstanceIdentifier(id.getContextReference());
61 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
62 opsInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
64 final NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(in);
65 final int size = nnin.readInt();
66 rpcs = new ArrayList<>(size);
67 for (int i = 0; i < size; ++i) {
68 rpcs.add(DOMRpcIdentifier.create(nnin.readSchemaPath(), nnin.readYangInstanceIdentifier()));
72 private Object readResolve() {
73 return new RoutingTable(opsInvoker, rpcs);
77 private static final long serialVersionUID = 1L;
79 RoutingTable(final ActorRef invoker, final Collection<DOMRpcIdentifier> table) {
80 super(invoker, table);
83 RoutingTable addRpcs(final Collection<DOMRpcIdentifier> toAdd) {
84 final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(getItems());
85 newRpcs.addAll(toAdd);
86 return new RoutingTable(getInvoker(), newRpcs);
89 RoutingTable removeRpcs(final Collection<DOMRpcIdentifier> toRemove) {
90 final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(getItems());
91 newRpcs.removeAll(toRemove);
92 return new RoutingTable(getInvoker(), newRpcs);
96 Object writeReplace() {
97 return new Proxy(this);