b4cdb81c5aee7e9088e94d31cd62d35ed1260cb1
[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 javax.annotation.concurrent.NotThreadSafe;
15 import org.opendaylight.controller.cluster.common.actor.Dispatchers;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Abstract base class for a ShardDataTreeNotificationPublisher that offloads the generation and publication
22  * of data tree notifications to an actor.
23  *
24  * @author Thomas Pantelis
25  */
26 @NotThreadSafe
27 abstract class AbstractShardDataTreeNotificationPublisherActorProxy implements ShardDataTreeNotificationPublisher {
28     @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_PRIVATE")
29     protected final Logger log = LoggerFactory.getLogger(getClass());
30
31     private final ActorContext actorContext;
32     private final String actorName;
33     private final String logContext;
34     private ActorRef publisherActor;
35
36     protected AbstractShardDataTreeNotificationPublisherActorProxy(final ActorContext actorContext,
37             final String actorName, final String logContext) {
38         this.actorContext = actorContext;
39         this.actorName = actorName;
40         this.logContext = logContext;
41     }
42
43     protected abstract Props props();
44
45     protected final String actorName() {
46         return actorName;
47     }
48
49     protected final String logContext() {
50         return logContext;
51     }
52
53     @Override
54     public void publishChanges(final DataTreeCandidate candidate) {
55         publisherActor().tell(new ShardDataTreeNotificationPublisherActor.PublishNotifications(candidate),
56                 ActorRef.noSender());
57     }
58
59     protected final ActorRef publisherActor() {
60         if (publisherActor == null) {
61             String dispatcher = new Dispatchers(actorContext.system().dispatchers()).getDispatcherPath(
62                     Dispatchers.DispatcherType.Notification);
63             publisherActor = actorContext.actorOf(props().withDispatcher(dispatcher), actorName);
64
65             log.debug("{}: Created publisher actor {} with name {}", logContext, publisherActor, actorName);
66         }
67
68         return publisherActor;
69     }
70 }