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.base.Preconditions;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.ImmutableSet;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.List;
21 import java.util.Map.Entry;
22 import java.util.Optional;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
25 import org.opendaylight.controller.remote.rpc.RemoteRpcProviderConfig;
26 import org.opendaylight.controller.remote.rpc.RouteIdentifierImpl;
27 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.AddOrUpdateRoutes;
28 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.RemoveRoutes;
29 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.UpdateRemoteEndpoints;
30 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
31 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStore;
32 import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
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.BucketStore} to maintain this
40 * cluster wide information.
42 public class RpcRegistry extends BucketStore<RoutingTable> {
43 private final ActorRef rpcRegistrar;
45 public RpcRegistry(final RemoteRpcProviderConfig config, final ActorRef rpcInvoker, final ActorRef rpcRegistrar) {
46 super(config, new RoutingTable(rpcInvoker));
47 this.rpcRegistrar = Preconditions.checkNotNull(rpcRegistrar);
51 * Create a new props instance for instantiating an RpcRegistry actor.
53 * @param config Provider configuration
54 * @param rpcRegistrar Local RPC provider interface, used to register routers to remote nodes
55 * @param rpcInvoker Actor handling RPC invocation requests from remote nodes
56 * @return A new {@link Props} instance
58 public static Props props(final RemoteRpcProviderConfig config, final ActorRef rpcInvoker,
59 final ActorRef rpcRegistrar) {
60 return Props.create(RpcRegistry.class, config, rpcInvoker, rpcRegistrar);
64 protected void handleReceive(final Object message) throws Exception {
65 if (message instanceof AddOrUpdateRoutes) {
66 receiveAddRoutes((AddOrUpdateRoutes) message);
67 } else if (message instanceof RemoveRoutes) {
68 receiveRemoveRoutes((RemoveRoutes) message);
70 super.handleReceive(message);
74 private void receiveAddRoutes(final AddOrUpdateRoutes msg) {
75 LOG.debug("AddOrUpdateRoutes: {}", msg.getRouteIdentifiers());
76 updateLocalBucket(getLocalBucket().getData().addRpcs(msg.getRouteIdentifiers()));
80 * Processes a RemoveRoutes message.
82 * @param msg contains list of route ids to remove
84 private void receiveRemoveRoutes(final RemoveRoutes msg) {
85 LOG.debug("RemoveRoutes: {}", msg.getRouteIdentifiers());
86 updateLocalBucket(getLocalBucket().getData().removeRpcs(msg.getRouteIdentifiers()));
90 protected void onBucketRemoved(final Address address, final Bucket<RoutingTable> bucket) {
91 rpcRegistrar.tell(new UpdateRemoteEndpoints(ImmutableMap.of(address, Optional.empty())), ActorRef.noSender());
95 protected void onBucketsUpdated(final Map<Address, Bucket<RoutingTable>> buckets) {
96 final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = new HashMap<>(buckets.size());
98 for (Entry<Address, Bucket<RoutingTable>> e : buckets.entrySet()) {
99 final RoutingTable table = e.getValue().getData();
101 final List<DOMRpcIdentifier> rpcs = new ArrayList<>(table.getRoutes().size());
102 for (RouteIdentifier<?, ?, ?> ri : table.getRoutes()) {
103 if (ri instanceof RouteIdentifierImpl) {
104 final RouteIdentifierImpl id = (RouteIdentifierImpl) ri;
105 rpcs.add(DOMRpcIdentifier.create(SchemaPath.create(true, id.getType()), id.getRoute()));
107 LOG.warn("Skipping unsupported route {} from {}", ri, e.getKey());
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;
124 RemoteRpcEndpoint(final ActorRef router, final Collection<DOMRpcIdentifier> rpcs) {
125 this.router = Preconditions.checkNotNull(router);
126 this.rpcs = ImmutableSet.copyOf(rpcs);
129 public ActorRef getRouter() {
133 public Set<DOMRpcIdentifier> getRpcs() {
139 * All messages used by the RpcRegistry.
141 public static class Messages {
142 abstract static class AbstractRouteMessage {
143 final List<RouteIdentifier<?, ?, ?>> routeIdentifiers;
145 AbstractRouteMessage(final List<RouteIdentifier<?, ?, ?>> routeIdentifiers) {
146 Preconditions.checkArgument(routeIdentifiers != null && !routeIdentifiers.isEmpty(),
147 "Route Identifiers must be supplied");
148 this.routeIdentifiers = routeIdentifiers;
151 List<RouteIdentifier<?, ?, ?>> getRouteIdentifiers() {
152 return this.routeIdentifiers;
156 public String toString() {
157 return "ContainsRoute{" + "routeIdentifiers=" + routeIdentifiers + '}';
161 public static final class AddOrUpdateRoutes extends AbstractRouteMessage {
162 public AddOrUpdateRoutes(final List<RouteIdentifier<?, ?, ?>> routeIdentifiers) {
163 super(routeIdentifiers);
167 public static final class RemoveRoutes extends AbstractRouteMessage {
168 public RemoveRoutes(final List<RouteIdentifier<?, ?, ?>> routeIdentifiers) {
169 super(routeIdentifiers);
173 public static final class UpdateRemoteEndpoints {
174 private final Map<Address, Optional<RemoteRpcEndpoint>> endpoints;
176 UpdateRemoteEndpoints(final Map<Address, Optional<RemoteRpcEndpoint>> endpoints) {
177 this.endpoints = ImmutableMap.copyOf(endpoints);
180 public Map<Address, Optional<RemoteRpcEndpoint>> getEndpoints() {