Implement DOMDataTreeChangeListener.onInitialData
[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 akka.actor.ActorRef;
11 import akka.actor.ActorSelection;
12 import com.google.common.base.Preconditions;
13 import java.util.Collection;
14 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChanged;
15 import org.opendaylight.controller.cluster.datastore.messages.OnInitialData;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Internal implementation of a {@link DOMDataTreeChangeListener} which
23  * encapsulates received notifications into a {@link DataTreeChanged}
24  * message and forwards them towards the client's {@link DataTreeChangeListenerActor}.
25  */
26 final class ForwardingDataTreeChangeListener implements DOMDataTreeChangeListener {
27     private static final Logger LOG = LoggerFactory.getLogger(ForwardingDataTreeChangeListener.class);
28
29     private final ActorSelection actor;
30
31     ForwardingDataTreeChangeListener(final ActorSelection actor) {
32         this.actor = Preconditions.checkNotNull(actor, "actor should not be null");
33     }
34
35     @Override
36     public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
37         LOG.debug("Sending DataTreeChanged to {}", actor);
38         actor.tell(new DataTreeChanged(changes), ActorRef.noSender());
39     }
40
41     @Override
42     public void onInitialData() {
43         LOG.debug("Sending OnInitialData to {}", actor);
44         actor.tell(OnInitialData.INSTANCE, ActorRef.noSender());
45     }
46
47     @Override
48     public String toString() {
49         return "ForwardingDataTreeChangeListener [actor=" + actor + "]";
50     }
51 }