2 * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.actors;
10 import static java.util.Objects.requireNonNull;
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.eclipse.jdt.annotation.NonNullByDefault;
19 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
20 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
21 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistrationReply;
22 import org.opendaylight.yangtools.concepts.Registration;
23 import scala.concurrent.duration.FiniteDuration;
26 * Actor co-located with a shard. It exists only to terminate the registration when
27 * asked to do so via {@link CloseDataTreeNotificationListenerRegistration}.
29 public final class DataTreeNotificationListenerRegistrationActor extends AbstractUntypedActor {
30 // FIXME: rework this constant to a duration and its injection
32 static long killDelay = TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS);
34 private SetRegistration registration = null;
35 private Cancellable killSchedule = null;
36 private boolean closed;
39 protected void handleReceive(final Object message) {
40 if (message instanceof CloseDataTreeNotificationListenerRegistration) {
41 closeListenerRegistration();
42 if (isValidSender(getSender())) {
43 getSender().tell(CloseDataTreeNotificationListenerRegistrationReply.getInstance(), getSelf());
45 } else if (message instanceof SetRegistration setRegistration) {
46 registration = setRegistration;
48 closeListenerRegistration();
51 unknownMessage(message);
55 private void closeListenerRegistration() {
58 final var reg = registration;
61 reg.registration.close();
64 if (killSchedule == null) {
65 killSchedule = getContext().system().scheduler().scheduleOnce(FiniteDuration.create(killDelay,
66 TimeUnit.MILLISECONDS), getSelf(), PoisonPill.getInstance(), getContext().dispatcher(),
72 public static Props props() {
73 return Props.create(DataTreeNotificationListenerRegistrationActor.class);
77 public record SetRegistration(Registration registration, Runnable onClose) {
78 public SetRegistration {
79 requireNonNull(registration);
80 requireNonNull(onClose);