Factor out ActorSystemProvider from blueprint
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / akka / osgi / impl / OSGiActorSystemProvider.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.cluster.akka.osgi.impl;
9
10 import akka.actor.ActorSystem;
11 import com.typesafe.config.Config;
12 import java.util.concurrent.TimeoutException;
13 import org.opendaylight.controller.cluster.ActorSystemProvider;
14 import org.opendaylight.controller.cluster.ActorSystemProviderListener;
15 import org.opendaylight.controller.cluster.akka.impl.ActorSystemProviderImpl;
16 import org.opendaylight.controller.cluster.akka.impl.AkkaConfigFactory;
17 import org.opendaylight.controller.cluster.common.actor.AkkaConfigurationReader;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.service.component.annotations.Activate;
21 import org.osgi.service.component.annotations.Component;
22 import org.osgi.service.component.annotations.Deactivate;
23 import org.osgi.service.component.annotations.Reference;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import scala.concurrent.Await;
27 import scala.concurrent.duration.Duration;
28
29 @Component(immediate = true)
30 public final class OSGiActorSystemProvider implements ActorSystemProvider {
31     private static final Logger LOG = LoggerFactory.getLogger(OSGiActorSystemProvider.class);
32
33     @Reference
34     AkkaConfigurationReader reader = null;
35
36     private ActorSystemProviderImpl delegate;
37
38     @Override
39     public ActorSystem getActorSystem() {
40         return delegate.getActorSystem();
41     }
42
43     @Override
44     public ListenerRegistration<ActorSystemProviderListener> registerActorSystemProviderListener(
45             final ActorSystemProviderListener listener) {
46         return delegate.registerActorSystemProviderListener(listener);
47     }
48
49     @Activate
50     void activate(final BundleContext bundleContext) {
51         LOG.info("Actor System provider starting");
52         final Config akkaConfig = AkkaConfigFactory.createAkkaConfig(reader);
53         delegate = new ActorSystemProviderImpl(BundleClassLoaderFactory.createClassLoader(bundleContext),
54             QuarantinedMonitorActorPropsFactory.createProps(bundleContext, akkaConfig), akkaConfig);
55         LOG.info("Actor System provider started");
56     }
57
58     @Deactivate
59     void deactivate() throws TimeoutException, InterruptedException {
60         LOG.info("Actor System provider stopping");
61         Await.result(delegate.asyncClose(), Duration.Inf());
62         delegate = null;
63         LOG.info("Actor System provider stopped");
64     }
65 }
66

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.