Bug 8231: Fix testChangeListenerRegistration failure
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / actors / DataTreeNotificationListenerRegistrationActor.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies 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.actors;
9
10 import akka.actor.ActorRef;
11 import akka.actor.Cancellable;
12 import akka.actor.PoisonPill;
13 import akka.actor.Props;
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.base.Preconditions;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
18 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
19 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistrationReply;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import scala.concurrent.duration.Duration;
22
23 /**
24  * Actor co-located with a shard. It exists only to terminate the registration when
25  * asked to do so via {@link CloseDataTreeNotificationListenerRegistration}.
26  */
27 public final class DataTreeNotificationListenerRegistrationActor extends AbstractUntypedActor {
28     @VisibleForTesting
29     static long killDelay = TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS);
30
31     private ListenerRegistration<?> registration;
32     private Runnable onClose;
33     private boolean closed;
34     private Cancellable killSchedule;
35
36     @Override
37     protected void handleReceive(Object message) throws Exception {
38         if (message instanceof CloseDataTreeNotificationListenerRegistration) {
39             closeListenerRegistration();
40             if (isValidSender(getSender())) {
41                 getSender().tell(CloseDataTreeNotificationListenerRegistrationReply.getInstance(), getSelf());
42             }
43         } else if (message instanceof SetRegistration) {
44             registration = ((SetRegistration)message).registration;
45             onClose = ((SetRegistration)message).onClose;
46             if (closed) {
47                 closeListenerRegistration();
48             }
49         } else {
50             unknownMessage(message);
51         }
52     }
53
54     private void closeListenerRegistration() {
55         closed = true;
56         if (registration != null) {
57             registration.close();
58             onClose.run();
59             registration = null;
60
61             if (killSchedule == null) {
62                 killSchedule = getContext().system().scheduler().scheduleOnce(Duration.create(killDelay,
63                         TimeUnit.MILLISECONDS), getSelf(), PoisonPill.getInstance(), getContext().dispatcher(),
64                         ActorRef.noSender());
65             }
66         }
67     }
68
69     public static Props props() {
70         return Props.create(DataTreeNotificationListenerRegistrationActor.class);
71     }
72
73     public static class SetRegistration {
74         private final ListenerRegistration<?> registration;
75         private final Runnable onClose;
76
77         public SetRegistration(final ListenerRegistration<?> registration, final Runnable onClose) {
78             this.registration = Preconditions.checkNotNull(registration);
79             this.onClose = Preconditions.checkNotNull(onClose);
80         }
81     }
82 }