Make Payload Serializable
[controller.git] / opendaylight / md-sal / eos-dom-akka / src / main / java / org / opendaylight / controller / eos / akka / owner / supervisor / AbstractSupervisor.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.eos.akka.owner.supervisor;
9
10 import akka.actor.typed.ActorRef;
11 import akka.actor.typed.Behavior;
12 import akka.actor.typed.javadsl.AbstractBehavior;
13 import akka.actor.typed.javadsl.ActorContext;
14 import akka.cluster.ddata.ORMap;
15 import akka.cluster.ddata.ORSet;
16 import akka.cluster.ddata.typed.javadsl.DistributedData;
17 import akka.cluster.ddata.typed.javadsl.Replicator;
18 import akka.cluster.ddata.typed.javadsl.ReplicatorMessageAdapter;
19 import java.time.Duration;
20 import org.opendaylight.controller.eos.akka.owner.supervisor.command.ClearCandidates;
21 import org.opendaylight.controller.eos.akka.owner.supervisor.command.ClearCandidatesForMember;
22 import org.opendaylight.controller.eos.akka.owner.supervisor.command.ClearCandidatesResponse;
23 import org.opendaylight.controller.eos.akka.owner.supervisor.command.OwnerSupervisorCommand;
24 import org.opendaylight.controller.eos.akka.registry.candidate.CandidateRegistry;
25 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
26 import org.slf4j.Logger;
27
28 abstract class AbstractSupervisor extends AbstractBehavior<OwnerSupervisorCommand> {
29
30     final ReplicatorMessageAdapter<OwnerSupervisorCommand, ORMap<DOMEntity, ORSet<String>>> candidateReplicator;
31
32     AbstractSupervisor(final ActorContext<OwnerSupervisorCommand> context) {
33         super(context);
34
35         final ActorRef<Replicator.Command> replicator = DistributedData.get(getContext().getSystem()).replicator();
36         candidateReplicator = new ReplicatorMessageAdapter<>(getContext(), replicator, Duration.ofSeconds(5));
37     }
38
39     Behavior<OwnerSupervisorCommand> onClearCandidatesForMember(final ClearCandidatesForMember command) {
40         getLogger().debug("Clearing candidates for member: {}", command.getCandidate());
41
42         candidateReplicator.askGet(
43                 askReplyTo -> new Replicator.Get<>(CandidateRegistry.KEY,
44                         new Replicator.ReadMajority(Duration.ofSeconds(15)), askReplyTo),
45                 response -> new ClearCandidates(response, command));
46
47         return this;
48     }
49
50     Behavior<OwnerSupervisorCommand> finishClearCandidates(final ClearCandidates command) {
51         if (command.getResponse() instanceof Replicator.GetSuccess) {
52             getLogger().debug("Retrieved candidate data, clearing candidates for {}",
53                     command.getOriginalMessage().getCandidate());
54
55             getContext().spawnAnonymous(CandidateCleaner.create()).tell(command);
56         } else {
57             getLogger().debug("Unable to retrieve candidate data for {}, no candidates present sending empty reply",
58                     command.getOriginalMessage().getCandidate());
59             command.getOriginalMessage().getReplyTo().tell(new ClearCandidatesResponse());
60         }
61
62         return this;
63     }
64
65     abstract Logger getLogger();
66 }