Add support for reusable streaming
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RemoteRpcProvider.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.ActorSystem;
13 import akka.actor.PoisonPill;
14 import com.google.common.base.Preconditions;
15 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
16 import org.opendaylight.mdsal.dom.api.DOMRpcService;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * This is the base class which initialize all the actors, listeners and
22  * default RPc implementation so remote invocation of rpcs.
23  */
24 public class RemoteRpcProvider implements AutoCloseable {
25
26     private static final Logger LOG = LoggerFactory.getLogger(RemoteRpcProvider.class);
27
28     private final DOMRpcProviderService rpcProvisionRegistry;
29     private final RemoteRpcProviderConfig config;
30     private final ActorSystem actorSystem;
31     private final DOMRpcService rpcService;
32
33     private ActorRef rpcManager;
34
35     public RemoteRpcProvider(final ActorSystem actorSystem, final DOMRpcProviderService rpcProvisionRegistry,
36             final DOMRpcService rpcService, final RemoteRpcProviderConfig config) {
37         this.actorSystem = Preconditions.checkNotNull(actorSystem);
38         this.rpcProvisionRegistry = Preconditions.checkNotNull(rpcProvisionRegistry);
39         this.rpcService = Preconditions.checkNotNull(rpcService);
40         this.config = Preconditions.checkNotNull(config);
41     }
42
43     @Override
44     public void close() {
45         if (rpcManager != null) {
46             LOG.info("Stopping RPC Manager at {}", rpcManager);
47             rpcManager.tell(PoisonPill.getInstance(), ActorRef.noSender());
48             rpcManager = null;
49         }
50     }
51
52     public void start() {
53         LOG.info("Starting Remote RPC service...");
54         rpcManager = actorSystem.actorOf(RpcManager.props(rpcProvisionRegistry, rpcService, config),
55                 config.getRpcManagerName());
56         LOG.debug("RPC Manager started at {}", rpcManager);
57     }
58 }