Convert sal-remoterpc to mdsal APIs
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RpcManager.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
9 package org.opendaylight.controller.remote.rpc;
10
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.remote.rpc.registry.RpcRegistry;
18 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
19 import org.opendaylight.mdsal.dom.api.DOMRpcService;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import scala.concurrent.duration.Duration;
22
23 /**
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}.
26  */
27 public class RpcManager extends AbstractUntypedActor {
28     private final DOMRpcProviderService rpcProvisionRegistry;
29     private final RemoteRpcProviderConfig config;
30     private final DOMRpcService rpcServices;
31
32     private ListenerRegistration<RpcListener> listenerReg;
33     private ActorRef rpcInvoker;
34     private ActorRef rpcRegistry;
35     private ActorRef rpcRegistrar;
36
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);
42     }
43
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);
50     }
51
52     @Override
53     public void preStart() throws Exception {
54         super.preStart();
55
56         rpcInvoker = getContext().actorOf(RpcInvoker.props(rpcServices)
57             .withMailbox(config.getMailBoxName()), config.getRpcBrokerName());
58         LOG.debug("Listening for RPC invocation requests with {}", rpcInvoker);
59
60         rpcRegistrar = getContext().actorOf(RpcRegistrar.props(config, rpcProvisionRegistry)
61             .withMailbox(config.getMailBoxName()), config.getRpcRegistrarName());
62         LOG.debug("Registering remote RPCs with {}", rpcRegistrar);
63
64         rpcRegistry = getContext().actorOf(RpcRegistry.props(config, rpcInvoker, rpcRegistrar)
65                 .withMailbox(config.getMailBoxName()), config.getRpcRegistryName());
66         LOG.debug("Propagating RPC information with {}", rpcRegistry);
67
68         final RpcListener rpcListener = new RpcListener(rpcRegistry);
69         LOG.debug("Registering local availabitility listener {}", rpcListener);
70         listenerReg = rpcServices.registerRpcListener(rpcListener);
71     }
72
73     @Override
74     public void postStop() throws Exception {
75         if (listenerReg != null) {
76             listenerReg.close();
77             listenerReg = null;
78         }
79
80         super.postStop();
81     }
82
83     @Override
84     protected void handleReceive(final Object message) {
85         unknownMessage(message);
86     }
87
88     @Override
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();
93         });
94     }
95 }