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
9 package org.opendaylight.controller.remote.rpc;
11 import akka.actor.ActorRef;
12 import akka.actor.OneForOneStrategy;
13 import akka.actor.Props;
14 import akka.actor.SupervisorStrategy;
15 import com.google.common.base.Preconditions;
16 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
19 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import scala.concurrent.duration.Duration;
24 * This class acts as a supervisor, creates all the actors, resumes them, if an exception is thrown. It also registers
25 * {@link RpcListener} with the local {@link DOMRpcService}.
27 public class RpcManager extends AbstractUntypedActor {
28 private final DOMRpcProviderService rpcProvisionRegistry;
29 private final RemoteRpcProviderConfig config;
30 private final DOMRpcService rpcServices;
32 private ListenerRegistration<RpcListener> listenerReg;
33 private ActorRef rpcInvoker;
34 private ActorRef rpcRegistry;
35 private ActorRef rpcRegistrar;
37 RpcManager(final DOMRpcProviderService rpcProvisionRegistry, final DOMRpcService rpcServices,
38 final RemoteRpcProviderConfig config) {
39 this.rpcProvisionRegistry = Preconditions.checkNotNull(rpcProvisionRegistry);
40 this.rpcServices = Preconditions.checkNotNull(rpcServices);
41 this.config = Preconditions.checkNotNull(config);
44 public static Props props(final DOMRpcProviderService rpcProvisionRegistry, final DOMRpcService rpcServices,
45 final RemoteRpcProviderConfig config) {
46 Preconditions.checkNotNull(rpcProvisionRegistry, "RpcProviderService can not be null!");
47 Preconditions.checkNotNull(rpcServices, "RpcService can not be null!");
48 Preconditions.checkNotNull(config, "RemoteRpcProviderConfig can not be null!");
49 return Props.create(RpcManager.class, rpcProvisionRegistry, rpcServices, config);
53 public void preStart() throws Exception {
56 rpcInvoker = getContext().actorOf(RpcInvoker.props(rpcServices)
57 .withMailbox(config.getMailBoxName()), config.getRpcBrokerName());
58 LOG.debug("Listening for RPC invocation requests with {}", rpcInvoker);
60 rpcRegistrar = getContext().actorOf(RpcRegistrar.props(config, rpcProvisionRegistry)
61 .withMailbox(config.getMailBoxName()), config.getRpcRegistrarName());
62 LOG.debug("Registering remote RPCs with {}", rpcRegistrar);
64 rpcRegistry = getContext().actorOf(RpcRegistry.props(config, rpcInvoker, rpcRegistrar)
65 .withMailbox(config.getMailBoxName()), config.getRpcRegistryName());
66 LOG.debug("Propagating RPC information with {}", rpcRegistry);
68 final RpcListener rpcListener = new RpcListener(rpcRegistry);
69 LOG.debug("Registering local availabitility listener {}", rpcListener);
70 listenerReg = rpcServices.registerRpcListener(rpcListener);
74 public void postStop() throws Exception {
75 if (listenerReg != null) {
84 protected void handleReceive(final Object message) {
85 unknownMessage(message);
89 public SupervisorStrategy supervisorStrategy() {
90 return new OneForOneStrategy(10, Duration.create("1 minute"), t -> {
91 LOG.error("An exception happened actor will be resumed", t);
92 return SupervisorStrategy.resume();