atomic-storage: remove type dependency at segment level I/O
[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.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;
27
28 public final class RoutingTable extends AbstractRoutingTable<RoutingTable, DOMRpcIdentifier> {
29     private static final class Proxy implements Externalizable {
30         private static final long serialVersionUID = 1L;
31
32         @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
33         private Collection<DOMRpcIdentifier> rpcs;
34         private ActorRef opsInvoker;
35
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")
39         public Proxy() {
40             // For Externalizable
41         }
42
43         Proxy(final RoutingTable table) {
44             rpcs = table.getItems();
45             opsInvoker = table.getInvoker();
46         }
47
48         @Override
49         public void writeExternal(final ObjectOutput out) throws IOException {
50             out.writeObject(Serialization.serializedActorPath(opsInvoker));
51
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());
58                 }
59             }
60         }
61
62         @Override
63         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
64             opsInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
65
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()));
73             }
74         }
75
76         private Object readResolve() {
77             return new RoutingTable(opsInvoker, rpcs);
78         }
79     }
80
81     private static final long serialVersionUID = 1L;
82
83     RoutingTable(final ActorRef invoker, final Collection<DOMRpcIdentifier> table) {
84         super(invoker, table);
85     }
86
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);
91     }
92
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);
97     }
98
99     @Override
100     Object writeReplace() {
101         return new Proxy(this);
102     }
103 }