95c37f56ad66f51023a581f704999fd89d240a80
[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.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;
32
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<>();
39
40     public ActorSystemProviderImpl(final BundleContext bundleContext) {
41         LOG.info("Creating new ActorSystem");
42
43         final Bundle bundle = bundleContext.getBundle();
44
45         final BundleDelegatingClassLoader classLoader = AccessController.doPrivileged(
46             (PrivilegedAction<BundleDelegatingClassLoader>) () ->
47                 new BundleDelegatingClassLoader(bundle, Thread.currentThread().getContextClassLoader()));
48
49         final AkkaConfigurationReader configurationReader = new FileAkkaConfigurationReader();
50         final Config akkaConfig = ConfigFactory.load(configurationReader.read()).getConfig(CONFIGURATION_NAME);
51
52         actorSystem = ActorSystem.create(ACTOR_SYSTEM_NAME, akkaConfig, classLoader);
53
54         actorSystem.actorOf(Props.create(TerminationMonitor.class), TerminationMonitor.ADDRESS);
55
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);
62
63     }
64
65     @Override
66     public ActorSystem getActorSystem() {
67         return actorSystem;
68     }
69
70     @Override
71     public ListenerRegistration<ActorSystemProviderListener> registerActorSystemProviderListener(
72             final ActorSystemProviderListener listener) {
73         return listeners.register(listener);
74     }
75
76     @Override
77     @SuppressWarnings("checkstyle:IllegalCatch")
78     public void close() {
79         LOG.info("Shutting down ActorSystem");
80
81         try {
82             Await.result(actorSystem.terminate(), Duration.create(10, TimeUnit.SECONDS));
83         } catch (Exception e) {
84             LOG.warn("Error awaiting actor termination", e);
85         }
86     }
87 }