Fix modernization issues
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / ActionRegistry.java
1 /*
2  * Copyright (c) 2019 Nordix Foundation.  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 static java.util.Objects.requireNonNull;
11
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;
21 import java.util.Map;
22 import java.util.Optional;
23 import java.util.Set;
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;
30
31 /**
32  * Registry to look up cluster nodes that have registered for a given Action.
33  *
34  * <p>
35  * It uses {@link org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor} to maintain this
36  * cluster wide information.
37  */
38 public class ActionRegistry extends BucketStoreActor<ActionRoutingTable> {
39     private final ActorRef rpcRegistrar;
40     private final RemoteActionRegistryMXBeanImpl mxBean;
41
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());
48     }
49
50     /**
51      * Create a new props instance for instantiating an ActionRegistry actor.
52      *
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
57      */
58     public static Props props(final RemoteOpsProviderConfig config, final ActorRef opsInvoker,
59                               final ActorRef opsRegistrar) {
60         return Props.create(ActionRegistry.class, config, opsInvoker, opsRegistrar);
61     }
62
63     @Override
64     public void postStop() {
65         super.postStop();
66         this.mxBean.unregister();
67     }
68
69     @Override
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);
74         } else {
75             super.handleCommand(message);
76         }
77     }
78
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()));
83     }
84
85     @Override
86     protected void onBucketRemoved(final Address address, final Bucket<ActionRoutingTable> bucket) {
87         rpcRegistrar.tell(new Messages.UpdateRemoteActionEndpoints(ImmutableMap.of(address, Optional.empty())),
88             ActorRef.noSender());
89     }
90
91     @Override
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());
95
96         for (Map.Entry<Address, Bucket<ActionRoutingTable>> e : buckets.entrySet()) {
97             final ActionRoutingTable table = e.getValue().getData();
98
99             final Collection<DOMActionInstance> actions = table.getItems();
100             endpoints.put(e.getKey(), actions.isEmpty() ? Optional.empty()
101                 : Optional.of(new RemoteActionEndpoint(table.getInvoker(), actions)));
102         }
103
104         if (!endpoints.isEmpty()) {
105             rpcRegistrar.tell(new Messages.UpdateRemoteActionEndpoints(endpoints), ActorRef.noSender());
106         }
107     }
108
109     public static final class RemoteActionEndpoint {
110         private final Set<DOMActionInstance> actions;
111         private final ActorRef router;
112
113         @VisibleForTesting
114         public RemoteActionEndpoint(final ActorRef router, final Collection<DOMActionInstance> actions) {
115             this.router = requireNonNull(router);
116             this.actions = ImmutableSet.copyOf(actions);
117         }
118
119         public ActorRef getRouter() {
120             return router;
121         }
122
123         public Set<DOMActionInstance> getActions() {
124             return actions;
125         }
126     }
127
128         /**
129          * All messages used by the ActionRegistry.
130          */
131     public static class Messages {
132         abstract static class AbstractActionRouteMessage {
133             final Collection<DOMActionInstance> addedActions;
134             final Collection<DOMActionInstance> removedActions;
135
136             AbstractActionRouteMessage(final Collection<DOMActionInstance> addedActions,
137                                        final Collection<DOMActionInstance> removedActions) {
138                 this.addedActions = ImmutableList.copyOf(addedActions);
139                 this.removedActions = ImmutableList.copyOf(removedActions);
140             }
141
142             Collection<DOMActionInstance> getAddedActions() {
143                 return this.addedActions;
144             }
145
146             Collection<DOMActionInstance> getRemovedActions() {
147                 return this.removedActions;
148             }
149
150
151             @Override
152             public String toString() {
153                 return "ContainsRoute{" + "addedActions=" + addedActions + " removedActions=" + removedActions + '}';
154             }
155         }
156
157
158         public static final class UpdateActions extends AbstractActionRouteMessage {
159             public UpdateActions(final Collection<DOMActionInstance> addedActions,
160                                  final Collection<DOMActionInstance> removedActions) {
161                 super(addedActions, removedActions);
162             }
163
164         }
165
166         public static final class UpdateRemoteActionEndpoints {
167             private final Map<Address, Optional<RemoteActionEndpoint>> actionEndpoints;
168
169             @VisibleForTesting
170             public UpdateRemoteActionEndpoints(final Map<Address, Optional<RemoteActionEndpoint>>
171                                                                    actionEndpoints) {
172                 this.actionEndpoints = ImmutableMap.copyOf(actionEndpoints);
173             }
174
175             public Map<Address, Optional<RemoteActionEndpoint>> getActionEndpoints() {
176                 return actionEndpoints;
177             }
178         }
179     }
180 }