Merge "Bug 1819 - Moved bundle up in features.xml to avoid exception in log Change...
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RemoteRpcProviderFactory.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.ActorSystem;
12 import akka.osgi.BundleDelegatingClassLoader;
13 import com.typesafe.config.Config;
14 import org.opendaylight.controller.sal.core.api.Broker;
15 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
16 import org.osgi.framework.BundleContext;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class RemoteRpcProviderFactory {
21     private static final Logger LOG = LoggerFactory.getLogger(RemoteRpcProviderFactory.class);
22
23     public static RemoteRpcProvider createInstance(
24             final Broker broker, final BundleContext bundleContext, final RemoteRpcProviderConfig config){
25
26       RemoteRpcProvider rpcProvider =
27           new RemoteRpcProvider(createActorSystem(bundleContext, config), (RpcProvisionRegistry) broker);
28
29       broker.registerProvider(rpcProvider);
30       return rpcProvider;
31     }
32
33     private static ActorSystem createActorSystem(BundleContext bundleContext, RemoteRpcProviderConfig config){
34
35         // Create an OSGi bundle classloader for actor system
36         BundleDelegatingClassLoader classLoader =
37                 new BundleDelegatingClassLoader(bundleContext.getBundle(),
38                         Thread.currentThread().getContextClassLoader());
39
40         Config actorSystemConfig = config.get();
41         if(LOG.isDebugEnabled()) {
42             LOG.debug("Actor system configuration\n{}", actorSystemConfig.root().render());
43         }
44         if (config.isMetricCaptureEnabled()) {
45             LOG.info("Instrumentation is enabled in actor system {}. Metrics can be viewed in JMX console.",
46                     config.getActorSystemName());
47         }
48
49         return ActorSystem.create(config.getActorSystemName(), actorSystemConfig, classLoader);
50     }
51 }