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