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