BUG-5280: synchronize access to local histories
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
16 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeChangeListenerRegistration;
17 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeChangeListenerRegistrationReply;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20
21 /**
22  * Actor co-located with a shard. It exists only to terminate the registration when
23  * asked to do so via {@link CloseDataTreeChangeListenerRegistration}.
24  */
25 public final class DataTreeChangeListenerRegistrationActor extends AbstractUntypedActor {
26     private final ListenerRegistration<DOMDataTreeChangeListener> registration;
27
28     public DataTreeChangeListenerRegistrationActor(final ListenerRegistration<DOMDataTreeChangeListener> registration) {
29         this.registration = Preconditions.checkNotNull(registration);
30     }
31
32     @Override
33     protected void handleReceive(Object message) throws Exception {
34         if (message instanceof CloseDataTreeChangeListenerRegistration) {
35             registration.close();
36             if (isValidSender(getSender())) {
37                 getSender().tell(CloseDataTreeChangeListenerRegistrationReply.getInstance(), getSelf());
38             }
39
40             getSelf().tell(PoisonPill.getInstance(), getSelf());
41         } else {
42             unknownMessage(message);
43         }
44     }
45
46     public static Props props(final ListenerRegistration<DOMDataTreeChangeListener> registration) {
47         return Props.create(new DataTreeChangeListenerRegistrationCreator(registration));
48     }
49
50     private static final class DataTreeChangeListenerRegistrationCreator
51             implements Creator<DataTreeChangeListenerRegistrationActor> {
52         private static final long serialVersionUID = 1L;
53
54         @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but we don't "
55                 + "create remote instances of this actor and thus don't need it to be Serializable.")
56         final ListenerRegistration<DOMDataTreeChangeListener> registration;
57
58         DataTreeChangeListenerRegistrationCreator(ListenerRegistration<DOMDataTreeChangeListener> registration) {
59             this.registration = Preconditions.checkNotNull(registration);
60         }
61
62         @Override
63         public DataTreeChangeListenerRegistrationActor create() {
64             return new DataTreeChangeListenerRegistrationActor(registration);
65         }
66     }
67 }