2 * Copyright (c) 2019 Nordix Foundation. 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 static java.util.Objects.requireNonNull;
12 import akka.actor.ActorRef;
13 import akka.actor.Address;
14 import akka.actor.Props;
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.collect.ImmutableSet;
19 import java.util.Collection;
20 import java.util.HashMap;
22 import java.util.Optional;
24 import org.opendaylight.controller.remote.rpc.RemoteOpsProviderConfig;
25 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
26 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess;
27 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor;
28 import org.opendaylight.controller.remote.rpc.registry.mbeans.RemoteActionRegistryMXBeanImpl;
29 import org.opendaylight.mdsal.dom.api.DOMActionInstance;
32 * Registry to look up cluster nodes that have registered for a given Action.
35 * It uses {@link org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor} to maintain this
36 * cluster wide information.
38 public class ActionRegistry extends BucketStoreActor<ActionRoutingTable> {
39 private final ActorRef rpcRegistrar;
40 private final RemoteActionRegistryMXBeanImpl mxBean;
42 public ActionRegistry(final RemoteOpsProviderConfig config, final ActorRef rpcInvoker,
43 final ActorRef rpcRegistrar) {
44 super(config, config.getRpcRegistryPersistenceId(), new ActionRoutingTable(rpcInvoker, ImmutableSet.of()));
45 this.rpcRegistrar = requireNonNull(rpcRegistrar);
46 this.mxBean = new RemoteActionRegistryMXBeanImpl(new BucketStoreAccess(self(), getContext().dispatcher(),
47 config.getAskDuration()), config.getAskDuration());
51 * Create a new props instance for instantiating an ActionRegistry actor.
53 * @param config Provider configuration
54 * @param opsRegistrar Local RPC provider interface, used to register routers to remote nodes
55 * @param opsInvoker Actor handling RPC invocation requests from remote nodes
56 * @return A new {@link Props} instance
58 public static Props props(final RemoteOpsProviderConfig config, final ActorRef opsInvoker,
59 final ActorRef opsRegistrar) {
60 return Props.create(ActionRegistry.class, config, opsInvoker, opsRegistrar);
64 public void postStop() {
66 this.mxBean.unregister();
70 protected void handleCommand(final Object message) throws Exception {
71 if (message instanceof ActionRegistry.Messages.UpdateActions) {
72 LOG.debug("handling updatesActionRoutes message");
73 updatesActionRoutes((Messages.UpdateActions) message);
75 super.handleCommand(message);
79 private void updatesActionRoutes(final Messages.UpdateActions msg) {
80 LOG.debug("addedActions: {}", msg.getAddedActions());
81 LOG.debug("removedActions: {}", msg.getRemovedActions());
82 updateLocalBucket(getLocalData().updateActions(msg.getAddedActions(), msg.getRemovedActions()));
86 protected void onBucketRemoved(final Address address, final Bucket<ActionRoutingTable> bucket) {
87 rpcRegistrar.tell(new Messages.UpdateRemoteActionEndpoints(ImmutableMap.of(address, Optional.empty())),
92 protected void onBucketsUpdated(final Map<Address, Bucket<ActionRoutingTable>> buckets) {
93 LOG.debug("Updating buckets for action registry");
94 final Map<Address, Optional<RemoteActionEndpoint>> endpoints = new HashMap<>(buckets.size());
96 for (Map.Entry<Address, Bucket<ActionRoutingTable>> e : buckets.entrySet()) {
97 final ActionRoutingTable table = e.getValue().getData();
99 final Collection<DOMActionInstance> actions = table.getItems();
100 endpoints.put(e.getKey(), actions.isEmpty() ? Optional.empty()
101 : Optional.of(new RemoteActionEndpoint(table.getInvoker(), actions)));
104 if (!endpoints.isEmpty()) {
105 rpcRegistrar.tell(new Messages.UpdateRemoteActionEndpoints(endpoints), ActorRef.noSender());
109 public static final class RemoteActionEndpoint {
110 private final Set<DOMActionInstance> actions;
111 private final ActorRef router;
114 public RemoteActionEndpoint(final ActorRef router, final Collection<DOMActionInstance> actions) {
115 this.router = requireNonNull(router);
116 this.actions = ImmutableSet.copyOf(actions);
119 public ActorRef getRouter() {
123 public Set<DOMActionInstance> getActions() {
129 * All messages used by the ActionRegistry.
131 public static class Messages {
132 abstract static class AbstractActionRouteMessage {
133 final Collection<DOMActionInstance> addedActions;
134 final Collection<DOMActionInstance> removedActions;
136 AbstractActionRouteMessage(final Collection<DOMActionInstance> addedActions,
137 final Collection<DOMActionInstance> removedActions) {
138 this.addedActions = ImmutableList.copyOf(addedActions);
139 this.removedActions = ImmutableList.copyOf(removedActions);
142 Collection<DOMActionInstance> getAddedActions() {
143 return this.addedActions;
146 Collection<DOMActionInstance> getRemovedActions() {
147 return this.removedActions;
152 public String toString() {
153 return "ContainsRoute{" + "addedActions=" + addedActions + " removedActions=" + removedActions + '}';
158 public static final class UpdateActions extends AbstractActionRouteMessage {
159 public UpdateActions(final Collection<DOMActionInstance> addedActions,
160 final Collection<DOMActionInstance> removedActions) {
161 super(addedActions, removedActions);
166 public static final class UpdateRemoteActionEndpoints {
167 private final Map<Address, Optional<RemoteActionEndpoint>> actionEndpoints;
170 public UpdateRemoteActionEndpoints(final Map<Address, Optional<RemoteActionEndpoint>>
172 this.actionEndpoints = ImmutableMap.copyOf(actionEndpoints);
175 public Map<Address, Optional<RemoteActionEndpoint>> getActionEndpoints() {
176 return actionEndpoints;