Use ActorSystem.terminate()
[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.japi.Effect;
13 import akka.osgi.BundleDelegatingClassLoader;
14 import com.typesafe.config.Config;
15 import com.typesafe.config.ConfigFactory;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.controller.cluster.ActorSystemProvider;
18 import org.opendaylight.controller.cluster.ActorSystemProviderListener;
19 import org.opendaylight.controller.cluster.common.actor.AkkaConfigurationReader;
20 import org.opendaylight.controller.cluster.common.actor.FileAkkaConfigurationReader;
21 import org.opendaylight.controller.cluster.common.actor.QuarantinedMonitorActor;
22 import org.opendaylight.controller.cluster.datastore.TerminationMonitor;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.util.ListenerRegistry;
25 import org.osgi.framework.Bundle;
26 import org.osgi.framework.BundleContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import scala.concurrent.Await;
30 import scala.concurrent.duration.Duration;
31
32 public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseable {
33     private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
34     private static final String CONFIGURATION_NAME = "odl-cluster-data";
35     static final Logger LOG = LoggerFactory.getLogger(ActorSystemProviderImpl.class);
36     private final ActorSystem actorSystem;
37     private final ListenerRegistry<ActorSystemProviderListener> listeners = new ListenerRegistry<>();
38
39     public ActorSystemProviderImpl(final BundleContext bundleContext) {
40         LOG.info("Creating new ActorSystem");
41
42         final Bundle bundle = bundleContext.getBundle();
43
44         final BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundle, Thread.currentThread()
45                 .getContextClassLoader());
46
47         final AkkaConfigurationReader configurationReader = new FileAkkaConfigurationReader();
48         final Config akkaConfig = ConfigFactory.load(configurationReader.read()).getConfig(CONFIGURATION_NAME);
49
50         actorSystem = ActorSystem.create(ACTOR_SYSTEM_NAME, akkaConfig, classLoader);
51
52         actorSystem.actorOf(Props.create(TerminationMonitor.class), TerminationMonitor.ADDRESS);
53
54         actorSystem.actorOf(QuarantinedMonitorActor.props(new Effect() {
55
56             @Override
57             public void apply() throws Exception {
58                 // restart the entire karaf container
59                 LOG.warn("Restarting karaf container");
60                 System.setProperty("karaf.restart", "true");
61                 bundleContext.getBundle(0).stop();
62             }
63         }), QuarantinedMonitorActor.ADDRESS);
64
65     }
66
67     @Override
68     public ActorSystem getActorSystem() {
69         return actorSystem;
70     }
71
72     @Override
73     public ListenerRegistration<ActorSystemProviderListener> registerActorSystemProviderListener(
74             final ActorSystemProviderListener listener) {
75         return listeners.register(listener);
76     }
77
78     @Override
79     public void close() {
80         LOG.info("Shutting down ActorSystem");
81
82         try {
83             Await.result(actorSystem.terminate(), Duration.create(10, TimeUnit.SECONDS));
84         } catch (Exception e) {
85             LOG.warn("Error awaiting actor termination", e);
86         }
87     }
88 }