X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-remoterpc-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fremote%2Frpc%2Fregistry%2FRpcRegistry.java;h=805e47a0dd05e7b52082f705f523b8870cd5bb02;hp=1dcc4e140595250a0414eba706eada3e776f4ece;hb=5b66dd8f5e3467a07e77b20fe696b29993ce5565;hpb=a54ec60368110d22794602343c934902f6833c65 diff --git a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java index 1dcc4e1405..805e47a0dd 100644 --- a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java +++ b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java @@ -8,235 +8,166 @@ package org.opendaylight.controller.remote.rpc.registry; import akka.actor.ActorRef; +import akka.actor.Address; import akka.actor.Props; -import akka.japi.Creator; -import akka.japi.Option; -import akka.japi.Pair; import com.google.common.base.Preconditions; -import java.util.ArrayList; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import java.util.Collection; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.Set; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier; +import org.opendaylight.controller.remote.rpc.RemoteRpcProviderConfig; import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.AddOrUpdateRoutes; -import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.FindRouters; import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.RemoveRoutes; -import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.SetLocalRouter; +import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.UpdateRemoteEndpoints; import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket; -import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStore; -import org.opendaylight.controller.remote.rpc.registry.mbeans.RemoteRpcRegistryMXBean; -import org.opendaylight.controller.remote.rpc.registry.mbeans.RemoteRpcRegistryMXBeanImpl; -import org.opendaylight.controller.sal.connector.api.RpcRouter; -import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier; +import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor; /** - * Registry to look up cluster nodes that have registered for a given rpc. - *

- * It uses {@link org.opendaylight.controller.remote.rpc.registry.gossip.BucketStore} to maintain this + * Registry to look up cluster nodes that have registered for a given RPC. + * + *

+ * It uses {@link org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor} to maintain this * cluster wide information. */ -public class RpcRegistry extends BucketStore { +public class RpcRegistry extends BucketStoreActor { + private final ActorRef rpcRegistrar; - public RpcRegistry() { - getLocalBucket().setData(new RoutingTable()); + public RpcRegistry(final RemoteRpcProviderConfig config, final ActorRef rpcInvoker, final ActorRef rpcRegistrar) { + super(config, config.getRpcRegistryPersistenceId(), new RoutingTable(rpcInvoker, ImmutableSet.of())); + this.rpcRegistrar = Preconditions.checkNotNull(rpcRegistrar); } - public static Props props() { - return Props.create(new RpcRegistryCreator()); + /** + * Create a new props instance for instantiating an RpcRegistry actor. + * + * @param config Provider configuration + * @param rpcRegistrar Local RPC provider interface, used to register routers to remote nodes + * @param rpcInvoker Actor handling RPC invocation requests from remote nodes + * @return A new {@link Props} instance + */ + public static Props props(final RemoteRpcProviderConfig config, final ActorRef rpcInvoker, + final ActorRef rpcRegistrar) { + return Props.create(RpcRegistry.class, config, rpcInvoker, rpcRegistrar); } @Override - protected void handleReceive(Object message) throws Exception { - //TODO: if sender is remote, reject message - - if (message instanceof SetLocalRouter) { - receiveSetLocalRouter((SetLocalRouter) message); - } else if (message instanceof AddOrUpdateRoutes) { + protected void handleCommand(final Object message) throws Exception { + if (message instanceof AddOrUpdateRoutes) { receiveAddRoutes((AddOrUpdateRoutes) message); } else if (message instanceof RemoveRoutes) { receiveRemoveRoutes((RemoveRoutes) message); - } else if (message instanceof Messages.FindRouters) { - receiveGetRouter((FindRouters) message); } else { - super.handleReceive(message); + super.handleCommand(message); } } - /** - * Register's rpc broker - * - * @param message contains {@link akka.actor.ActorRef} for rpc broker - */ - private void receiveSetLocalRouter(SetLocalRouter message) { - getLocalBucket().getData().setRouter(message.getRouter()); + private void receiveAddRoutes(final AddOrUpdateRoutes msg) { + LOG.debug("AddOrUpdateRoutes: {}", msg.getRouteIdentifiers()); + updateLocalBucket(getLocalData().addRpcs(msg.getRouteIdentifiers())); } /** - * @param msg + * Processes a RemoveRoutes message. + * + * @param msg contains list of route ids to remove */ - private void receiveAddRoutes(AddOrUpdateRoutes msg) { - - log.debug("AddOrUpdateRoutes: {}", msg.getRouteIdentifiers()); - - RoutingTable table = getLocalBucket().getData().copy(); - for(RpcRouter.RouteIdentifier routeId : msg.getRouteIdentifiers()) { - table.addRoute(routeId); - } + private void receiveRemoveRoutes(final RemoveRoutes msg) { + LOG.debug("RemoveRoutes: {}", msg.getRouteIdentifiers()); + updateLocalBucket(getLocalData().removeRpcs(msg.getRouteIdentifiers())); + } - updateLocalBucket(table); + @Override + protected void onBucketRemoved(final Address address, final Bucket bucket) { + rpcRegistrar.tell(new UpdateRemoteEndpoints(ImmutableMap.of(address, Optional.empty())), ActorRef.noSender()); } - /** - * @param msg contains list of route ids to remove - */ - private void receiveRemoveRoutes(RemoveRoutes msg) { + @Override + protected void onBucketsUpdated(final Map> buckets) { + final Map> endpoints = new HashMap<>(buckets.size()); - RoutingTable table = getLocalBucket().getData().copy(); - for (RpcRouter.RouteIdentifier routeId : msg.getRouteIdentifiers()) { - table.removeRoute(routeId); + for (Entry> e : buckets.entrySet()) { + final RoutingTable table = e.getValue().getData(); + + final Collection rpcs = table.getRoutes(); + endpoints.put(e.getKey(), rpcs.isEmpty() ? Optional.empty() + : Optional.of(new RemoteRpcEndpoint(table.getRpcInvoker(), rpcs))); } - updateLocalBucket(table); + if (!endpoints.isEmpty()) { + rpcRegistrar.tell(new UpdateRemoteEndpoints(endpoints), ActorRef.noSender()); + } } - /** - * Finds routers for the given rpc. - * - * @param msg - */ - private void receiveGetRouter(FindRouters msg) { - List> routers = new ArrayList<>(); - - RouteIdentifier routeId = msg.getRouteIdentifier(); - findRoutes(getLocalBucket().getData(), routeId, routers); + public static final class RemoteRpcEndpoint { + private final Set rpcs; + private final ActorRef router; - for(Bucket bucket : getRemoteBuckets().values()) { - findRoutes(bucket.getData(), routeId, routers); + RemoteRpcEndpoint(final ActorRef router, final Collection rpcs) { + this.router = Preconditions.checkNotNull(router); + this.rpcs = ImmutableSet.copyOf(rpcs); } - getSender().tell(new Messages.FindRoutersReply(routers), getSelf()); - } - - private void findRoutes(RoutingTable table, RpcRouter.RouteIdentifier routeId, - List> routers) { - if (table == null) { - return; + public ActorRef getRouter() { + return router; } - Option> routerWithUpdateTime = table.getRouterFor(routeId); - if(!routerWithUpdateTime.isEmpty()) { - routers.add(routerWithUpdateTime.get()); + public Set getRpcs() { + return rpcs; } } /** - * All messages used by the RpcRegistry + * All messages used by the RpcRegistry. */ public static class Messages { + abstract static class AbstractRouteMessage { + final List routeIdentifiers; - - public static class ContainsRoute { - final List> routeIdentifiers; - - public ContainsRoute(List> routeIdentifiers) { - Preconditions.checkArgument(routeIdentifiers != null && - !routeIdentifiers.isEmpty(), - "Route Identifiers must be supplied"); - this.routeIdentifiers = routeIdentifiers; + AbstractRouteMessage(final Collection routeIdentifiers) { + Preconditions.checkArgument(routeIdentifiers != null && !routeIdentifiers.isEmpty(), + "Route Identifiers must be supplied"); + this.routeIdentifiers = ImmutableList.copyOf(routeIdentifiers); } - public List> getRouteIdentifiers() { + List getRouteIdentifiers() { return this.routeIdentifiers; } @Override public String toString() { - return "ContainsRoute{" + - "routeIdentifiers=" + routeIdentifiers + - '}'; + return "ContainsRoute{" + "routeIdentifiers=" + routeIdentifiers + '}'; } } - public static class AddOrUpdateRoutes extends ContainsRoute { - - public AddOrUpdateRoutes(List> routeIdentifiers) { + public static final class AddOrUpdateRoutes extends AbstractRouteMessage { + public AddOrUpdateRoutes(final Collection routeIdentifiers) { super(routeIdentifiers); } } - public static class RemoveRoutes extends ContainsRoute { - - public RemoveRoutes(List> routeIdentifiers) { + public static final class RemoveRoutes extends AbstractRouteMessage { + public RemoveRoutes(final Collection routeIdentifiers) { super(routeIdentifiers); } } - public static class SetLocalRouter { - private final ActorRef router; - - public SetLocalRouter(ActorRef router) { - Preconditions.checkArgument(router != null, "Router must not be null"); - this.router = router; - } - - public ActorRef getRouter() { - return this.router; - } + public static final class UpdateRemoteEndpoints { + private final Map> endpoints; - @Override - public String toString() { - return "SetLocalRouter{" + - "router=" + router + - '}'; + UpdateRemoteEndpoints(final Map> endpoints) { + this.endpoints = ImmutableMap.copyOf(endpoints); } - } - public static class FindRouters { - private final RpcRouter.RouteIdentifier routeIdentifier; - - public FindRouters(RpcRouter.RouteIdentifier routeIdentifier) { - Preconditions.checkArgument(routeIdentifier != null, "Route must not be null"); - this.routeIdentifier = routeIdentifier; - } - - public RpcRouter.RouteIdentifier getRouteIdentifier() { - return routeIdentifier; - } - - @Override - public String toString() { - return "FindRouters{" + - "routeIdentifier=" + routeIdentifier + - '}'; + public Map> getEndpoints() { + return endpoints; } } - - public static class FindRoutersReply { - final List> routerWithUpdateTime; - - public FindRoutersReply(List> routerWithUpdateTime) { - Preconditions.checkArgument(routerWithUpdateTime != null, "List of routers found must not be null"); - this.routerWithUpdateTime = routerWithUpdateTime; - } - - public List> getRouterWithUpdateTime() { - return routerWithUpdateTime; - } - - @Override - public String toString() { - return "FindRoutersReply{" + - "routerWithUpdateTime=" + routerWithUpdateTime + - '}'; - } - } - } - - private static class RpcRegistryCreator implements Creator { - private static final long serialVersionUID = 1L; - - @Override - public RpcRegistry create() throws Exception { - RpcRegistry registry = new RpcRegistry(); - RemoteRpcRegistryMXBean mxBean = new RemoteRpcRegistryMXBeanImpl(registry); - return registry; - } } }