Add sender actor to the ForwardingDataTreeChangeListener
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ForwardingDataTreeChangeListener.java
1 /*
2  * Copyright (c) 2015 Cisco 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 static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSelection;
14 import java.util.Collection;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChanged;
17 import org.opendaylight.controller.cluster.datastore.messages.OnInitialData;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Internal implementation of a {@link DOMDataTreeChangeListener} which
25  * encapsulates received notifications into a {@link DataTreeChanged}
26  * message and forwards them towards the client's {@link DataTreeChangeListenerActor}.
27  */
28 final class ForwardingDataTreeChangeListener implements DOMDataTreeChangeListener {
29     private static final Logger LOG = LoggerFactory.getLogger(ForwardingDataTreeChangeListener.class);
30
31     private final ActorSelection actor;
32     private final ActorRef sendingActor;
33
34     ForwardingDataTreeChangeListener(final ActorSelection actor, @Nullable final ActorRef sendingActor) {
35         this.actor = requireNonNull(actor, "actor should not be null");
36         this.sendingActor = sendingActor;
37     }
38
39     @Override
40     public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
41         LOG.debug("Sending DataTreeChanged to {}", actor);
42         actor.tell(new DataTreeChanged(changes), sendingActor);
43     }
44
45     @Override
46     public void onInitialData() {
47         LOG.debug("Sending OnInitialData to {}", actor);
48         actor.tell(OnInitialData.INSTANCE, sendingActor);
49     }
50
51     @Override
52     public String toString() {
53         return "ForwardingDataTreeChangeListener [actor=" + actor
54             + ", sending actor=" + (sendingActor != null ? sendingActor : "NO_SENDER") + "]";
55     }
56 }

©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.