Merge "Fixed Karaf Distribution Archetype add dependent bundles"
[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
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import org.opendaylight.controller.remote.rpc.messages.UpdateSchemaContext;
15 import org.opendaylight.controller.sal.core.api.Broker;
16 import org.opendaylight.controller.sal.core.api.Provider;
17 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
18 import org.opendaylight.controller.sal.core.api.model.SchemaService;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.util.Collection;
25
26 /**
27  * This is the base class which initialize all the actors, listeners and
28  * default RPc implementation so remote invocation of rpcs.
29  */
30 public class RemoteRpcProvider implements AutoCloseable, Provider, SchemaContextListener {
31
32   private static final Logger LOG = LoggerFactory.getLogger(RemoteRpcProvider.class);
33
34   private final RpcProvisionRegistry rpcProvisionRegistry;
35
36   private ActorSystem actorSystem;
37   private Broker.ProviderSession brokerSession;
38   private SchemaContext schemaContext;
39   private ActorRef rpcManager;
40   private RemoteRpcProviderConfig config;
41
42
43   public RemoteRpcProvider(ActorSystem actorSystem, RpcProvisionRegistry rpcProvisionRegistry) {
44     this.actorSystem = actorSystem;
45     this.rpcProvisionRegistry = rpcProvisionRegistry;
46     this.config = new RemoteRpcProviderConfig(actorSystem.settings().config());
47   }
48
49   @Override
50   public void close() throws Exception {
51     if (this.actorSystem != null)
52       this.actorSystem.shutdown();
53   }
54
55   @Override
56   public void onSessionInitiated(Broker.ProviderSession session) {
57     this.brokerSession = session;
58     start();
59   }
60
61   @Override
62   public Collection<ProviderFunctionality> getProviderFunctionality() {
63     return null;
64   }
65
66   private void start() {
67     LOG.info("Starting remote rpc service...");
68
69     SchemaService schemaService = brokerSession.getService(SchemaService.class);
70     schemaContext = schemaService.getGlobalContext();
71
72     rpcManager = actorSystem.actorOf(RpcManager.props(schemaContext, brokerSession, rpcProvisionRegistry),
73                                      config.getRpcManagerName());
74
75     LOG.debug("rpc manager started");
76   }
77
78   @Override
79   public void onGlobalContextUpdated(SchemaContext schemaContext) {
80     this.schemaContext = schemaContext;
81     rpcManager.tell(new UpdateSchemaContext(schemaContext), null);
82
83   }
84 }