BUG-3128: rework sal-remoterpc-connector
[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.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;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Optional;
23 import java.util.Set;
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;
33 import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35
36 /**
37  * Registry to look up cluster nodes that have registered for a given RPC.
38  *
39  * <p>
40  * It uses {@link org.opendaylight.controller.remote.rpc.registry.gossip.BucketStore} to maintain this
41  * cluster wide information.
42  */
43 public class RpcRegistry extends BucketStore<RoutingTable> {
44     private final ActorRef rpcRegistrar;
45
46     public RpcRegistry(final RemoteRpcProviderConfig config, final ActorRef rpcInvoker, final ActorRef rpcRegistrar) {
47         super(config, new RoutingTable(rpcInvoker));
48         this.rpcRegistrar = Preconditions.checkNotNull(rpcRegistrar);
49     }
50
51     /**
52      * Create a new props instance for instantiating an RpcRegistry actor.
53      *
54      * @param config Provider configuration
55      * @param rpcRegistrar Local RPC provider interface, used to register routers to remote nodes
56      * @param rpcInvoker Actor handling RPC invocation requests from remote nodes
57      * @return A new {@link Props} instance
58      */
59     public static Props props(final RemoteRpcProviderConfig config, final ActorRef rpcInvoker,
60             final ActorRef rpcRegistrar) {
61         return Props.create(RpcRegistry.class, config, rpcInvoker, rpcRegistrar);
62     }
63
64     @Override
65     protected void handleReceive(final Object message) throws Exception {
66         if (message instanceof AddOrUpdateRoutes) {
67             receiveAddRoutes((AddOrUpdateRoutes) message);
68         } else if (message instanceof RemoveRoutes) {
69             receiveRemoveRoutes((RemoveRoutes) message);
70         } else {
71             super.handleReceive(message);
72         }
73     }
74
75     private void receiveAddRoutes(final AddOrUpdateRoutes msg) {
76         LOG.debug("AddOrUpdateRoutes: {}", msg.getRouteIdentifiers());
77
78         RoutingTable table = getLocalBucket().getData().copy();
79         for(RpcRouter.RouteIdentifier<?, ?, ?> routeId : msg.getRouteIdentifiers()) {
80             table.addRoute(routeId);
81         }
82
83         updateLocalBucket(table);
84     }
85
86     /**
87      * @param msg contains list of route ids to remove
88      */
89     private void receiveRemoveRoutes(final RemoveRoutes msg) {
90         RoutingTable table = getLocalBucket().getData().copy();
91         for (RpcRouter.RouteIdentifier<?, ?, ?> routeId : msg.getRouteIdentifiers()) {
92             table.removeRoute(routeId);
93         }
94
95         updateLocalBucket(table);
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 List<DOMRpcIdentifier> rpcs = new ArrayList<>(table.getRoutes().size());
111             for (RouteIdentifier<?, ?, ?> ri : table.getRoutes()) {
112                 if (ri instanceof RouteIdentifierImpl) {
113                     final RouteIdentifierImpl id = (RouteIdentifierImpl) ri;
114                     rpcs.add(DOMRpcIdentifier.create(SchemaPath.create(true, id.getType()), id.getRoute()));
115                 } else {
116                     LOG.warn("Skipping unsupported route {} from {}", ri, e.getKey());
117                 }
118             }
119
120             endpoints.put(e.getKey(), rpcs.isEmpty() ? Optional.empty()
121                     : Optional.of(new RemoteRpcEndpoint(table.getRouter(), rpcs)));
122         }
123
124         if (!endpoints.isEmpty()) {
125             rpcRegistrar.tell(new UpdateRemoteEndpoints(endpoints), ActorRef.noSender());
126         }
127     }
128
129     public static final class RemoteRpcEndpoint {
130         private final Set<DOMRpcIdentifier> rpcs;
131         private final ActorRef router;
132
133         RemoteRpcEndpoint(final ActorRef router, final Collection<DOMRpcIdentifier> rpcs) {
134             this.router = Preconditions.checkNotNull(router);
135             this.rpcs = ImmutableSet.copyOf(rpcs);
136         }
137
138         public ActorRef getRouter() {
139             return router;
140         }
141
142         public Set<DOMRpcIdentifier> getRpcs() {
143             return rpcs;
144         }
145     }
146
147     /**
148      * All messages used by the RpcRegistry
149      */
150     public static class Messages {
151         abstract static class AbstractRouteMessage {
152             final List<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdentifiers;
153
154             AbstractRouteMessage(final List<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdentifiers) {
155                 Preconditions.checkArgument(routeIdentifiers != null && !routeIdentifiers.isEmpty(),
156                         "Route Identifiers must be supplied");
157                 this.routeIdentifiers = routeIdentifiers;
158             }
159
160             List<RpcRouter.RouteIdentifier<?, ?, ?>> getRouteIdentifiers() {
161                 return this.routeIdentifiers;
162             }
163
164             @Override
165             public String toString() {
166                 return "ContainsRoute{" +
167                         "routeIdentifiers=" + routeIdentifiers +
168                         '}';
169             }
170         }
171
172         public static final class AddOrUpdateRoutes extends AbstractRouteMessage {
173             public AddOrUpdateRoutes(final List<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdentifiers) {
174                 super(routeIdentifiers);
175             }
176         }
177
178         public static final class RemoveRoutes extends AbstractRouteMessage {
179             public RemoveRoutes(final List<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdentifiers) {
180                 super(routeIdentifiers);
181             }
182         }
183
184         public static final class UpdateRemoteEndpoints {
185             private final Map<Address, Optional<RemoteRpcEndpoint>> endpoints;
186
187             UpdateRemoteEndpoints(final Map<Address, Optional<RemoteRpcEndpoint>> endpoints) {
188                 this.endpoints = ImmutableMap.copyOf(endpoints);
189             }
190
191             public Map<Address, Optional<RemoteRpcEndpoint>> getEndpoints() {
192                 return endpoints;
193             }
194         }
195     }
196 }