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;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
28 public final class RoutingTable extends AbstractRoutingTable<RoutingTable, DOMRpcIdentifier> {
29 private static final class Proxy implements Externalizable {
30 private static final long serialVersionUID = 1L;
32 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
33 private Collection<DOMRpcIdentifier> rpcs;
34 private ActorRef opsInvoker;
36 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
37 // be able to create instances via reflection.
38 @SuppressWarnings("checkstyle:RedundantModifier")
43 Proxy(final RoutingTable table) {
44 rpcs = table.getItems();
45 opsInvoker = table.getInvoker();
49 public void writeExternal(final ObjectOutput out) throws IOException {
50 out.writeObject(Serialization.serializedActorPath(opsInvoker));
52 try (NormalizedNodeDataOutput nnout = NormalizedNodeStreamVersion.current().newDataOutput(out)) {
53 nnout.writeInt(rpcs.size());
54 for (DOMRpcIdentifier id : rpcs) {
55 // TODO: we should be able to get by with just a QName
56 nnout.writeSchemaNodeIdentifier(Absolute.of(id.getType()));
57 nnout.writeYangInstanceIdentifier(id.getContextReference());
63 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
64 opsInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
66 final NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(in);
67 final int size = nnin.readInt();
68 rpcs = new ArrayList<>(size);
69 for (int i = 0; i < size; ++i) {
70 // TODO: we should be able to get by with just a QName
71 rpcs.add(DOMRpcIdentifier.create(nnin.readSchemaNodeIdentifier().firstNodeIdentifier(),
72 nnin.readYangInstanceIdentifier()));
76 private Object readResolve() {
77 return new RoutingTable(opsInvoker, rpcs);
81 private static final long serialVersionUID = 1L;
83 RoutingTable(final ActorRef invoker, final Collection<DOMRpcIdentifier> table) {
84 super(invoker, table);
87 RoutingTable addRpcs(final Collection<DOMRpcIdentifier> toAdd) {
88 final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(getItems());
89 newRpcs.addAll(toAdd);
90 return new RoutingTable(getInvoker(), newRpcs);
93 RoutingTable removeRpcs(final Collection<DOMRpcIdentifier> toRemove) {
94 final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(getItems());
95 newRpcs.removeAll(toRemove);
96 return new RoutingTable(getInvoker(), newRpcs);
100 Object writeReplace() {
101 return new Proxy(this);