5ba97a306f7b68625253eaff0f258660139a4a27
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / RpcRegistry.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.actor.Address;
12 import akka.actor.Props;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.ImmutableSet;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Optional;
24 import java.util.Set;
25 import org.opendaylight.controller.remote.rpc.RemoteRpcProviderConfig;
26 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.AddOrUpdateRoutes;
27 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.RemoveRoutes;
28 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.UpdateRemoteEndpoints;
29 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
30 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess;
31 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor;
32 import org.opendaylight.controller.remote.rpc.registry.mbeans.RemoteRpcRegistryMXBeanImpl;
33 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
34
35 /**
36  * Registry to look up cluster nodes that have registered for a given RPC.
37  *
38  * <p>
39  * It uses {@link org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor} to maintain this
40  * cluster wide information.
41  */
42 public class RpcRegistry extends BucketStoreActor<RoutingTable> {
43     private final ActorRef rpcRegistrar;
44     private final RemoteRpcRegistryMXBeanImpl mxBean;
45
46     public RpcRegistry(final RemoteRpcProviderConfig config, final ActorRef rpcInvoker, final ActorRef rpcRegistrar) {
47         super(config, config.getRpcRegistryPersistenceId(), new RoutingTable(rpcInvoker, ImmutableSet.of()));
48         this.rpcRegistrar = Preconditions.checkNotNull(rpcRegistrar);
49         this.mxBean = new RemoteRpcRegistryMXBeanImpl(new BucketStoreAccess(self(), getContext().dispatcher(),
50                 config.getAskDuration()), config.getAskDuration());
51     }
52
53     /**
54      * Create a new props instance for instantiating an RpcRegistry actor.
55      *
56      * @param config Provider configuration
57      * @param rpcRegistrar Local RPC provider interface, used to register routers to remote nodes
58      * @param rpcInvoker Actor handling RPC invocation requests from remote nodes
59      * @return A new {@link Props} instance
60      */
61     public static Props props(final RemoteRpcProviderConfig config, final ActorRef rpcInvoker,
62             final ActorRef rpcRegistrar) {
63         return Props.create(RpcRegistry.class, config, rpcInvoker, rpcRegistrar);
64     }
65
66     @Override
67     public void postStop() {
68         super.postStop();
69         this.mxBean.unregister();
70     }
71
72     @Override
73     protected void handleCommand(final Object message) throws Exception {
74         if (message instanceof AddOrUpdateRoutes) {
75             receiveAddRoutes((AddOrUpdateRoutes) message);
76         } else if (message instanceof RemoveRoutes) {
77             receiveRemoveRoutes((RemoveRoutes) message);
78         } else {
79             super.handleCommand(message);
80         }
81     }
82
83     private void receiveAddRoutes(final AddOrUpdateRoutes msg) {
84         LOG.debug("AddOrUpdateRoutes: {}", msg.getRouteIdentifiers());
85         updateLocalBucket(getLocalData().addRpcs(msg.getRouteIdentifiers()));
86     }
87
88     /**
89      * Processes a RemoveRoutes message.
90      *
91      * @param msg contains list of route ids to remove
92      */
93     private void receiveRemoveRoutes(final RemoveRoutes msg) {
94         LOG.debug("RemoveRoutes: {}", msg.getRouteIdentifiers());
95         updateLocalBucket(getLocalData().removeRpcs(msg.getRouteIdentifiers()));
96     }
97
98     @Override
99     protected void onBucketRemoved(final Address address, final Bucket<RoutingTable> bucket) {
100         rpcRegistrar.tell(new UpdateRemoteEndpoints(ImmutableMap.of(address, Optional.empty())), ActorRef.noSender());
101     }
102
103     @Override
104     protected void onBucketsUpdated(final Map<Address, Bucket<RoutingTable>> buckets) {
105         final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = new HashMap<>(buckets.size());
106
107         for (Entry<Address, Bucket<RoutingTable>> e : buckets.entrySet()) {
108             final RoutingTable table = e.getValue().getData();
109
110             final Collection<DOMRpcIdentifier> rpcs = table.getRoutes();
111             endpoints.put(e.getKey(), rpcs.isEmpty() ? Optional.empty()
112                     : Optional.of(new RemoteRpcEndpoint(table.getRpcInvoker(), rpcs)));
113         }
114
115         if (!endpoints.isEmpty()) {
116             rpcRegistrar.tell(new UpdateRemoteEndpoints(endpoints), ActorRef.noSender());
117         }
118     }
119
120     public static final class RemoteRpcEndpoint {
121         private final Set<DOMRpcIdentifier> rpcs;
122         private final ActorRef router;
123
124         @VisibleForTesting
125         public RemoteRpcEndpoint(final ActorRef router, final Collection<DOMRpcIdentifier> rpcs) {
126             this.router = Preconditions.checkNotNull(router);
127             this.rpcs = ImmutableSet.copyOf(rpcs);
128         }
129
130         public ActorRef getRouter() {
131             return router;
132         }
133
134         public Set<DOMRpcIdentifier> getRpcs() {
135             return rpcs;
136         }
137     }
138
139     /**
140      * All messages used by the RpcRegistry.
141      */
142     public static class Messages {
143         abstract static class AbstractRouteMessage {
144             final List<DOMRpcIdentifier> routeIdentifiers;
145
146             AbstractRouteMessage(final Collection<DOMRpcIdentifier> routeIdentifiers) {
147                 Preconditions.checkArgument(routeIdentifiers != null && !routeIdentifiers.isEmpty(),
148                         "Route Identifiers must be supplied");
149                 this.routeIdentifiers = ImmutableList.copyOf(routeIdentifiers);
150             }
151
152             List<DOMRpcIdentifier> getRouteIdentifiers() {
153                 return this.routeIdentifiers;
154             }
155
156             @Override
157             public String toString() {
158                 return "ContainsRoute{" + "routeIdentifiers=" + routeIdentifiers + '}';
159             }
160         }
161
162         public static final class AddOrUpdateRoutes extends AbstractRouteMessage {
163             public AddOrUpdateRoutes(final Collection<DOMRpcIdentifier> routeIdentifiers) {
164                 super(routeIdentifiers);
165             }
166         }
167
168         public static final class RemoveRoutes extends AbstractRouteMessage {
169             public RemoveRoutes(final Collection<DOMRpcIdentifier> routeIdentifiers) {
170                 super(routeIdentifiers);
171             }
172         }
173
174         public static final class UpdateRemoteEndpoints {
175             private final Map<Address, Optional<RemoteRpcEndpoint>> endpoints;
176
177             @VisibleForTesting
178             public UpdateRemoteEndpoints(final Map<Address, Optional<RemoteRpcEndpoint>> endpoints) {
179                 this.endpoints = ImmutableMap.copyOf(endpoints);
180             }
181
182             public Map<Address, Optional<RemoteRpcEndpoint>> getEndpoints() {
183                 return endpoints;
184             }
185         }
186     }
187 }