6b348fb76f904e6de35e105b0bc46cf796aabe32
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / AbstractShardDataTreeNotificationPublisherActorProxy.java
1 /*
2  * Copyright (c) 2016 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.cluster.datastore;
9
10 import akka.actor.ActorContext;
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import javax.annotation.concurrent.NotThreadSafe;
14 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Abstract base class for a ShardDataTreeNotificationPublisher that offloads the generation and publication
21  * of data tree notifications to an actor.
22  *
23  * @author Thomas Pantelis
24  */
25 @NotThreadSafe
26 abstract class AbstractShardDataTreeNotificationPublisherActorProxy implements ShardDataTreeNotificationPublisher {
27     private static final Logger LOG = LoggerFactory.getLogger(
28             AbstractShardDataTreeNotificationPublisherActorProxy.class);
29
30     private final ActorContext actorContext;
31     private final String actorName;
32     private final String logContext;
33     private ActorRef notifierActor;
34
35     protected AbstractShardDataTreeNotificationPublisherActorProxy(ActorContext actorContext, String actorName,
36             String logContext) {
37         this.actorContext = actorContext;
38         this.actorName = actorName;
39         this.logContext = logContext;
40     }
41
42     protected abstract Props props();
43
44     protected final String actorName() {
45         return actorName;
46     }
47
48     protected final String logContext() {
49         return logContext;
50     }
51
52     @Override
53     public void publishChanges(DataTreeCandidate candidate, String logContext) {
54         notifierActor().tell(new ShardDataTreeNotificationPublisherActor.PublishNotifications(candidate),
55                 ActorRef.noSender());
56     }
57
58     protected final ActorRef notifierActor() {
59         if (notifierActor == null) {
60             LOG.debug("Creating actor {}", actorName);
61
62             String dispatcher = new Dispatchers(actorContext.system().dispatchers()).getDispatcherPath(
63                     Dispatchers.DispatcherType.Notification);
64             notifierActor = actorContext.actorOf(props().withDispatcher(dispatcher).withMailbox(
65                     org.opendaylight.controller.cluster.datastore.utils.ActorContext.BOUNDED_MAILBOX), actorName);
66         }
67
68         return notifierActor;
69     }
70 }