Fixup "Leader should always apply modifications as local" regression
[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
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             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());
56                 }
57             }
58         }
59
60         @Override
61         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
62             opsInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
63
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()));
69             }
70         }
71
72         private Object readResolve() {
73             return new RoutingTable(opsInvoker, rpcs);
74         }
75     }
76
77     private static final long serialVersionUID = 1L;
78
79     RoutingTable(final ActorRef invoker, final Collection<DOMRpcIdentifier> table) {
80         super(invoker, table);
81     }
82
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);
87     }
88
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);
93     }
94
95     @Override
96     Object writeReplace() {
97         return new Proxy(this);
98     }
99 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.