79da59f9645949c91383abf860cccfa93393cfdd
[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             if (isValidSender(getSender())) {
36                 getSender().tell(CloseDataTreeChangeListenerRegistrationReply.getInstance(), getSelf());
37             }
38
39             getSelf().tell(PoisonPill.getInstance(), getSelf());
40         } else {
41             unknownMessage(message);
42         }
43     }
44
45     public static Props props(final ListenerRegistration<DOMDataTreeChangeListener> registration) {
46         return Props.create(new DataTreeChangeListenerRegistrationCreator(registration));
47     }
48
49     private static final class DataTreeChangeListenerRegistrationCreator
50             implements Creator<DataTreeChangeListenerRegistrationActor> {
51         private static final long serialVersionUID = 1L;
52         final ListenerRegistration<DOMDataTreeChangeListener> registration;
53
54         DataTreeChangeListenerRegistrationCreator(ListenerRegistration<DOMDataTreeChangeListener> registration) {
55             this.registration = Preconditions.checkNotNull(registration);
56         }
57
58         @Override
59         public DataTreeChangeListenerRegistrationActor create() {
60             return new DataTreeChangeListenerRegistrationActor(registration);
61         }
62     }
63 }