Convert sal-remoterpc to mdsal APIs
[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 com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableSet;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.io.Externalizable;
18 import java.io.IOException;
19 import java.io.ObjectInput;
20 import java.io.ObjectOutput;
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.Optional;
26 import java.util.Set;
27 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
28 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
29 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
30 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketData;
31 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
32
33 public final class RoutingTable implements BucketData<RoutingTable>, Serializable {
34     private static final class Proxy implements Externalizable {
35         private static final long serialVersionUID = 1L;
36
37         @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
38         private Collection<DOMRpcIdentifier> rpcs;
39         private ActorRef rpcInvoker;
40
41         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
42         // be able to create instances via reflection.
43         @SuppressWarnings("checkstyle:RedundantModifier")
44         public Proxy() {
45             // For Externalizable
46         }
47
48         Proxy(final RoutingTable table) {
49             rpcs = table.getRoutes();
50             rpcInvoker = table.getRpcInvoker();
51         }
52
53         @Override
54         public void writeExternal(final ObjectOutput out) throws IOException {
55             out.writeObject(Serialization.serializedActorPath(rpcInvoker));
56
57             final NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(out);
58             nnout.writeInt(rpcs.size());
59             for (DOMRpcIdentifier id : rpcs) {
60                 nnout.writeSchemaPath(id.getType());
61                 nnout.writeYangInstanceIdentifier(id.getContextReference());
62             }
63         }
64
65         @Override
66         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
67             rpcInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
68
69             final NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(in);
70             final int size = nnin.readInt();
71             rpcs = new ArrayList<>(size);
72             for (int i = 0; i < size; ++i) {
73                 rpcs.add(DOMRpcIdentifier.create(nnin.readSchemaPath(), nnin.readYangInstanceIdentifier()));
74             }
75         }
76
77         private Object readResolve() {
78             return new RoutingTable(rpcInvoker, rpcs);
79         }
80     }
81
82     private static final long serialVersionUID = 1L;
83
84     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
85     private final Set<DOMRpcIdentifier> rpcs;
86     private final ActorRef rpcInvoker;
87
88     RoutingTable(final ActorRef rpcInvoker, final Collection<DOMRpcIdentifier> table) {
89         this.rpcInvoker = Preconditions.checkNotNull(rpcInvoker);
90         this.rpcs = ImmutableSet.copyOf(table);
91     }
92
93     @Override
94     public Optional<ActorRef> getWatchActor() {
95         return Optional.of(rpcInvoker);
96     }
97
98     public Set<DOMRpcIdentifier> getRoutes() {
99         return rpcs;
100     }
101
102     ActorRef getRpcInvoker() {
103         return rpcInvoker;
104     }
105
106     RoutingTable addRpcs(final Collection<DOMRpcIdentifier> toAdd) {
107         final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(rpcs);
108         newRpcs.addAll(toAdd);
109         return new RoutingTable(rpcInvoker, newRpcs);
110     }
111
112     RoutingTable removeRpcs(final Collection<DOMRpcIdentifier> toRemove) {
113         final Set<DOMRpcIdentifier> newRpcs = new HashSet<>(rpcs);
114         newRpcs.removeAll(toRemove);
115         return new RoutingTable(rpcInvoker, newRpcs);
116     }
117
118     private Object writeReplace() {
119         return new Proxy(this);
120     }
121
122     @VisibleForTesting
123     boolean contains(final DOMRpcIdentifier routeId) {
124         return rpcs.contains(routeId);
125     }
126
127     @VisibleForTesting
128     int size() {
129         return rpcs.size();
130     }
131
132     @Override
133     public String toString() {
134         return "RoutingTable{" + "rpcs=" + rpcs + ", rpcInvoker=" + rpcInvoker + '}';
135     }
136 }