5994f30ab007df13a347359972b1b9287e2bf59b
[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 implements Creator<DataTreeChangeListenerRegistrationActor> {
50         private static final long serialVersionUID = 1L;
51         final ListenerRegistration<DOMDataTreeChangeListener> registration;
52
53         DataTreeChangeListenerRegistrationCreator(ListenerRegistration<DOMDataTreeChangeListener> registration) {
54             this.registration = Preconditions.checkNotNull(registration);
55         }
56
57         @Override
58         public DataTreeChangeListenerRegistrationActor create() {
59             return new DataTreeChangeListenerRegistrationActor(registration);
60         }
61     }
62 }

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