2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yang.config.actor_system_provider.impl;
10 import akka.actor.ActorSystem;
11 import akka.actor.Props;
12 import akka.osgi.BundleDelegatingClassLoader;
13 import com.typesafe.config.Config;
14 import com.typesafe.config.ConfigFactory;
15 import java.security.AccessController;
16 import java.security.PrivilegedAction;
17 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.controller.cluster.ActorSystemProvider;
19 import org.opendaylight.controller.cluster.ActorSystemProviderListener;
20 import org.opendaylight.controller.cluster.common.actor.AkkaConfigurationReader;
21 import org.opendaylight.controller.cluster.common.actor.FileAkkaConfigurationReader;
22 import org.opendaylight.controller.cluster.common.actor.QuarantinedMonitorActor;
23 import org.opendaylight.controller.cluster.datastore.TerminationMonitor;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.util.ListenerRegistry;
26 import org.osgi.framework.Bundle;
27 import org.osgi.framework.BundleContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import scala.concurrent.Await;
31 import scala.concurrent.duration.Duration;
33 public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseable {
34 private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
35 private static final String CONFIGURATION_NAME = "odl-cluster-data";
36 static final Logger LOG = LoggerFactory.getLogger(ActorSystemProviderImpl.class);
37 private final ActorSystem actorSystem;
38 private final ListenerRegistry<ActorSystemProviderListener> listeners = new ListenerRegistry<>();
40 public ActorSystemProviderImpl(final BundleContext bundleContext) {
41 LOG.info("Creating new ActorSystem");
43 final Bundle bundle = bundleContext.getBundle();
45 final BundleDelegatingClassLoader classLoader = AccessController.doPrivileged(
46 (PrivilegedAction<BundleDelegatingClassLoader>) () ->
47 new BundleDelegatingClassLoader(bundle, Thread.currentThread().getContextClassLoader()));
49 final AkkaConfigurationReader configurationReader = new FileAkkaConfigurationReader();
50 final Config akkaConfig = ConfigFactory.load(configurationReader.read()).getConfig(CONFIGURATION_NAME);
52 actorSystem = ActorSystem.create(ACTOR_SYSTEM_NAME, akkaConfig, classLoader);
54 actorSystem.actorOf(Props.create(TerminationMonitor.class), TerminationMonitor.ADDRESS);
56 actorSystem.actorOf(QuarantinedMonitorActor.props(() -> {
57 // restart the entire karaf container
58 LOG.warn("Restarting karaf container");
59 System.setProperty("karaf.restart", "true");
60 bundleContext.getBundle(0).stop();
61 }), QuarantinedMonitorActor.ADDRESS);
66 public ActorSystem getActorSystem() {
71 public ListenerRegistration<ActorSystemProviderListener> registerActorSystemProviderListener(
72 final ActorSystemProviderListener listener) {
73 return listeners.register(listener);
77 @SuppressWarnings("checkstyle:IllegalCatch")
79 LOG.info("Shutting down ActorSystem");
82 Await.result(actorSystem.terminate(), Duration.create(10, TimeUnit.SECONDS));
83 } catch (Exception e) {
84 LOG.warn("Error awaiting actor termination", e);