BUG-2673: make CDS implement DOMDataTreeChangeListener
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerRegistrationActor.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.PoisonPill;
11 import akka.actor.Props;
12 import akka.japi.Creator;
13 import com.google.common.base.Preconditions;
14 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
15 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeChangeListenerRegistration;
16 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeChangeListenerRegistrationReply;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19
20 /**
21  * Actor co-located with a shard. It exists only to terminate the registration when
22  * asked to do so via {@link CloseDataTreeChangeListenerRegistration}.
23  */
24 public final class DataTreeChangeListenerRegistrationActor extends AbstractUntypedActor {
25     private final ListenerRegistration<DOMDataTreeChangeListener> registration;
26
27     public DataTreeChangeListenerRegistrationActor(final ListenerRegistration<DOMDataTreeChangeListener> registration) {
28         this.registration = Preconditions.checkNotNull(registration);
29     }
30
31     @Override
32     protected void handleReceive(Object message) throws Exception {
33         if (message instanceof CloseDataTreeChangeListenerRegistration) {
34             registration.close();
35             getSender().tell(CloseDataTreeChangeListenerRegistrationReply.getInstance(), getSelf());
36             getSelf().tell(PoisonPill.getInstance(), getSelf());
37         }
38     }
39
40     public static Props props(final ListenerRegistration<DOMDataTreeChangeListener> registration) {
41         return Props.create(new DataTreeChangeListenerRegistrationCreator(registration));
42     }
43
44     private static final class DataTreeChangeListenerRegistrationCreator implements Creator<DataTreeChangeListenerRegistrationActor> {
45         private static final long serialVersionUID = 1L;
46         final ListenerRegistration<DOMDataTreeChangeListener> registration;
47
48         DataTreeChangeListenerRegistrationCreator(ListenerRegistration<DOMDataTreeChangeListener> registration) {
49             this.registration = Preconditions.checkNotNull(registration);
50         }
51
52         @Override
53         public DataTreeChangeListenerRegistrationActor create() {
54             return new DataTreeChangeListenerRegistrationActor(registration);
55         }
56     }
57 }