BUG 4151 : Create a shared actor system
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / config / yang / config / actor_system_provider / impl / ActorSystemProviderImpl.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 package org.opendaylight.controller.config.yang.config.actor_system_provider.impl;
9
10 import akka.actor.ActorSystem;
11 import akka.osgi.BundleDelegatingClassLoader;
12 import com.typesafe.config.ConfigFactory;
13 import java.util.concurrent.TimeUnit;
14 import org.opendaylight.controller.cluster.ActorSystemProvider;
15 import org.opendaylight.controller.cluster.ActorSystemProviderListener;
16 import org.opendaylight.controller.cluster.common.actor.AkkaConfigurationReader;
17 import org.opendaylight.controller.cluster.common.actor.FileAkkaConfigurationReader;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.util.ListenerRegistry;
20 import org.osgi.framework.BundleContext;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import scala.concurrent.duration.Duration;
24
25 public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseable {
26     private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
27     private static final String CONFIGURATION_NAME = "odl-cluster-data";
28     static final Logger LOG = LoggerFactory.getLogger(ActorSystemProviderImpl.class);
29
30     private ActorSystem actorSystem;
31     private final BundleDelegatingClassLoader classLoader;
32     private final ListenerRegistry<ActorSystemProviderListener> listeners = new ListenerRegistry<>();
33
34     public ActorSystemProviderImpl(BundleContext bundleContext) {
35         LOG.info("Creating new ActorSystem");
36
37         classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
38                 Thread.currentThread().getContextClassLoader());
39
40         createActorSystem();
41     }
42
43     private void createActorSystem() {
44         AkkaConfigurationReader configurationReader = new FileAkkaConfigurationReader();
45         actorSystem = ActorSystem.create(ACTOR_SYSTEM_NAME,
46                 ConfigFactory.load(configurationReader.read()).getConfig(CONFIGURATION_NAME), classLoader);
47     }
48
49     @Override
50     public ActorSystem getActorSystem() {
51         return actorSystem;
52     }
53
54     @Override
55     public ListenerRegistration<ActorSystemProviderListener> registerActorSystemProviderListener(
56             ActorSystemProviderListener listener) {
57         return listeners.register(listener);
58     }
59
60     @Override
61     public void close() {
62         LOG.info("Shutting down ActorSystem");
63
64         actorSystem.shutdown();
65         try {
66             actorSystem.awaitTermination(Duration.create(10, TimeUnit.SECONDS));
67         } catch (Exception e) {
68             LOG.warn("Error awaiting actor termination", e);
69         }
70     }
71 }