BUG-3128: rework sal-remoterpc-connector
[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 com.google.common.collect.ImmutableSet;
16 import java.util.Collection;
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.sal.core.api.Broker;
20 import org.opendaylight.controller.sal.core.api.Provider;
21 import org.opendaylight.controller.sal.core.api.model.SchemaService;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * This is the base class which initialize all the actors, listeners and
27  * default RPc implementation so remote invocation of rpcs.
28  */
29 public class RemoteRpcProvider implements AutoCloseable, Provider {
30
31     private static final Logger LOG = LoggerFactory.getLogger(RemoteRpcProvider.class);
32
33     private final DOMRpcProviderService rpcProvisionRegistry;
34     private final RemoteRpcProviderConfig config;
35     private final ActorSystem actorSystem;
36
37     private DOMRpcService rpcService;
38     private SchemaService schemaService;
39     private ActorRef rpcManager;
40
41     public RemoteRpcProvider(final ActorSystem actorSystem, final DOMRpcProviderService rpcProvisionRegistry,
42             final RemoteRpcProviderConfig config) {
43         this.actorSystem = actorSystem;
44         this.rpcProvisionRegistry = rpcProvisionRegistry;
45         this.config = Preconditions.checkNotNull(config);
46     }
47
48     public void setRpcService(final DOMRpcService rpcService) {
49         this.rpcService = rpcService;
50     }
51
52     public void setSchemaService(final SchemaService schemaService) {
53         this.schemaService = schemaService;
54     }
55
56     @Override
57     public void close() {
58         if (rpcManager != null) {
59             LOG.info("Stopping RPC Manager at {}", rpcManager);
60             rpcManager.tell(PoisonPill.getInstance(), ActorRef.noSender());
61             rpcManager = null;
62         }
63     }
64
65     @Override
66     public void onSessionInitiated(final Broker.ProviderSession session) {
67         rpcService = session.getService(DOMRpcService.class);
68         start();
69     }
70
71     @Override
72     public Collection<ProviderFunctionality> getProviderFunctionality() {
73         return ImmutableSet.of();
74     }
75
76     public void start() {
77         LOG.info("Starting Remote RPC service...");
78         rpcManager = actorSystem.actorOf(RpcManager.props(rpcProvisionRegistry, rpcService, config),
79                 config.getRpcManagerName());
80         LOG.debug("RPC Manager started at {}", rpcManager);
81     }
82 }