2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.remote.rpc.registry;
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;
22 import java.util.Map.Entry;
23 import java.util.Optional;
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;
36 * Registry to look up cluster nodes that have registered for a given RPC.
39 * It uses {@link org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor} to maintain this
40 * cluster wide information.
42 public class RpcRegistry extends BucketStoreActor<RoutingTable> {
43 private final ActorRef rpcRegistrar;
44 private final RemoteRpcRegistryMXBeanImpl mxBean;
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());
54 * Create a new props instance for instantiating an RpcRegistry actor.
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
61 public static Props props(final RemoteRpcProviderConfig config, final ActorRef rpcInvoker,
62 final ActorRef rpcRegistrar) {
63 return Props.create(RpcRegistry.class, config, rpcInvoker, rpcRegistrar);
67 public void postStop() {
69 this.mxBean.unregister();
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);
79 super.handleCommand(message);
83 private void receiveAddRoutes(final AddOrUpdateRoutes msg) {
84 LOG.debug("AddOrUpdateRoutes: {}", msg.getRouteIdentifiers());
85 updateLocalBucket(getLocalData().addRpcs(msg.getRouteIdentifiers()));
89 * Processes a RemoveRoutes message.
91 * @param msg contains list of route ids to remove
93 private void receiveRemoveRoutes(final RemoveRoutes msg) {
94 LOG.debug("RemoveRoutes: {}", msg.getRouteIdentifiers());
95 updateLocalBucket(getLocalData().removeRpcs(msg.getRouteIdentifiers()));
99 protected void onBucketRemoved(final Address address, final Bucket<RoutingTable> bucket) {
100 rpcRegistrar.tell(new UpdateRemoteEndpoints(ImmutableMap.of(address, Optional.empty())), ActorRef.noSender());
104 protected void onBucketsUpdated(final Map<Address, Bucket<RoutingTable>> buckets) {
105 final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = new HashMap<>(buckets.size());
107 for (Entry<Address, Bucket<RoutingTable>> e : buckets.entrySet()) {
108 final RoutingTable table = e.getValue().getData();
110 final Collection<DOMRpcIdentifier> rpcs = table.getRoutes();
111 endpoints.put(e.getKey(), rpcs.isEmpty() ? Optional.empty()
112 : Optional.of(new RemoteRpcEndpoint(table.getRpcInvoker(), rpcs)));
115 if (!endpoints.isEmpty()) {
116 rpcRegistrar.tell(new UpdateRemoteEndpoints(endpoints), ActorRef.noSender());
120 public static final class RemoteRpcEndpoint {
121 private final Set<DOMRpcIdentifier> rpcs;
122 private final ActorRef router;
125 public RemoteRpcEndpoint(final ActorRef router, final Collection<DOMRpcIdentifier> rpcs) {
126 this.router = Preconditions.checkNotNull(router);
127 this.rpcs = ImmutableSet.copyOf(rpcs);
130 public ActorRef getRouter() {
134 public Set<DOMRpcIdentifier> getRpcs() {
140 * All messages used by the RpcRegistry.
142 public static class Messages {
143 abstract static class AbstractRouteMessage {
144 final List<DOMRpcIdentifier> routeIdentifiers;
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);
152 List<DOMRpcIdentifier> getRouteIdentifiers() {
153 return this.routeIdentifiers;
157 public String toString() {
158 return "ContainsRoute{" + "routeIdentifiers=" + routeIdentifiers + '}';
162 public static final class AddOrUpdateRoutes extends AbstractRouteMessage {
163 public AddOrUpdateRoutes(final Collection<DOMRpcIdentifier> routeIdentifiers) {
164 super(routeIdentifiers);
168 public static final class RemoveRoutes extends AbstractRouteMessage {
169 public RemoveRoutes(final Collection<DOMRpcIdentifier> routeIdentifiers) {
170 super(routeIdentifiers);
174 public static final class UpdateRemoteEndpoints {
175 private final Map<Address, Optional<RemoteRpcEndpoint>> endpoints;
178 public UpdateRemoteEndpoints(final Map<Address, Optional<RemoteRpcEndpoint>> endpoints) {
179 this.endpoints = ImmutableMap.copyOf(endpoints);
182 public Map<Address, Optional<RemoteRpcEndpoint>> getEndpoints() {