Bump upstream SNAPSHOTS
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import org.opendaylight.controller.cluster.common.actor.Dispatchers;
15 import org.opendaylight.yangtools.yang.data.tree.api.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. This class is NOT thread-safe.
22  *
23  * @author Thomas Pantelis
24  */
25 abstract class AbstractShardDataTreeNotificationPublisherActorProxy implements ShardDataTreeNotificationPublisher {
26     @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_PRIVATE")
27     protected final Logger log = LoggerFactory.getLogger(getClass());
28
29     private final ActorContext actorContext;
30     private final String actorName;
31     private final String logContext;
32     private ActorRef publisherActor;
33
34     protected AbstractShardDataTreeNotificationPublisherActorProxy(final ActorContext actorContext,
35             final String actorName, final String logContext) {
36         this.actorContext = actorContext;
37         this.actorName = actorName;
38         this.logContext = logContext;
39     }
40
41     protected abstract Props props();
42
43     protected final String actorName() {
44         return actorName;
45     }
46
47     protected final String logContext() {
48         return logContext;
49     }
50
51     @Override
52     public void publishChanges(final DataTreeCandidate candidate) {
53         publisherActor().tell(new ShardDataTreeNotificationPublisherActor.PublishNotifications(candidate),
54                 ActorRef.noSender());
55     }
56
57     protected final ActorRef publisherActor() {
58         if (publisherActor == null) {
59             String dispatcher = new Dispatchers(actorContext.system().dispatchers()).getDispatcherPath(
60                     Dispatchers.DispatcherType.Notification);
61             publisherActor = actorContext.actorOf(props().withDispatcher(dispatcher), actorName);
62
63             log.debug("{}: Created publisher actor {} with name {}", logContext, publisherActor, actorName);
64         }
65
66         return publisherActor;
67     }
68 }