8d67b3bdd311af7747657274d16717695de0eca1
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / RoutingTable.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.remote.rpc.registry;
9
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;
21 import java.util.Set;
22 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
23 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
24 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
25 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
26
27 public final class RoutingTable extends AbstractRoutingTable<RoutingTable, DOMRpcIdentifier> {
28     private static final class Proxy implements Externalizable {
29         private static final long serialVersionUID = 1L;
30
31         @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
32         private Collection<DOMRpcIdentifier> rpcs;
33         private ActorRef opsInvoker;
34
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")
38         public Proxy() {
39             // For Externalizable
40         }
41
42         Proxy(final RoutingTable table) {
43             rpcs = table.getItems();
44             opsInvoker = table.getInvoker();
45         }
46
47         @Override
48         public void writeExternal(final ObjectOutput out) throws IOException {
49             out.writeObject(Serialization.serializedActorPath(opsInvoker));
50
51             final NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(out);
52             nnout.writeInt(rpcs.size());
53             for (DOMRpcIdentifier id : rpcs) {
54                 nnout.writeSchemaPath(id.getType());
55                 nnout.writeYangInstanceIdentifier(id.getContextReference());
56             }
57         }
58
59         @Override
60         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
61             opsInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
62
63             final NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(in);
64             final int size = nnin.readInt();
65             rpcs = new ArrayList<>(size);
66             for (int i = 0; i < size; ++i) {
67                 rpcs.add(DOMRpcIdentifier.create(nnin.readSchemaPath(), nnin.readYangInstanceIdentifier()));
68             }
69         }
70
71         private Object readResolve() {
72             return new RoutingTable(opsInvoker, rpcs);
73         }
74     }
75
76     private static final long serialVersionUID = 1L;
77
78     RoutingTable(final ActorRef invoker, final Collection<DOMRpcIdentifier> table) {
79         super(invoker, table);
80     }
81
82     RoutingTable addRpcs(final Collection<DOMRpcIdentifier> toAdd) {
83         final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(getItems());
84         newRpcs.addAll(toAdd);
85         return new RoutingTable(getInvoker(), newRpcs);
86     }
87
88     RoutingTable removeRpcs(final Collection<DOMRpcIdentifier> toRemove) {
89         final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(getItems());
90         newRpcs.removeAll(toRemove);
91         return new RoutingTable(getInvoker(), newRpcs);
92     }
93
94     @Override
95     Object writeReplace() {
96         return new Proxy(this);
97     }
98 }