Introduce DOMEntityOwnershipService replacement
[controller.git] / opendaylight / md-sal / eos-dom-akka / src / main / java / org / opendaylight / controller / eos / akka / bootstrap / EOSMain.java
1 /*
2  * Copyright (c) 2021 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.bootstrap;
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.actor.typed.javadsl.Behaviors;
15 import akka.actor.typed.javadsl.Receive;
16 import akka.cluster.typed.Cluster;
17 import akka.cluster.typed.ClusterSingleton;
18 import akka.cluster.typed.SingletonActor;
19 import org.opendaylight.controller.eos.akka.bootstrap.command.BootstrapCommand;
20 import org.opendaylight.controller.eos.akka.bootstrap.command.GetRunningContext;
21 import org.opendaylight.controller.eos.akka.bootstrap.command.RunningContext;
22 import org.opendaylight.controller.eos.akka.bootstrap.command.Terminate;
23 import org.opendaylight.controller.eos.akka.owner.checker.OwnerStateChecker;
24 import org.opendaylight.controller.eos.akka.owner.checker.command.StateCheckerCommand;
25 import org.opendaylight.controller.eos.akka.owner.supervisor.OwnerSyncer;
26 import org.opendaylight.controller.eos.akka.owner.supervisor.command.OwnerSupervisorCommand;
27 import org.opendaylight.controller.eos.akka.registry.candidate.CandidateRegistry;
28 import org.opendaylight.controller.eos.akka.registry.candidate.command.CandidateRegistryCommand;
29 import org.opendaylight.controller.eos.akka.registry.listener.type.EntityTypeListenerRegistry;
30 import org.opendaylight.controller.eos.akka.registry.listener.type.command.TypeListenerRegistryCommand;
31 import org.opendaylight.yangtools.yang.common.Empty;
32
33 public final class EOSMain extends AbstractBehavior<BootstrapCommand> {
34     private final ActorRef<TypeListenerRegistryCommand> listenerRegistry;
35     private final ActorRef<CandidateRegistryCommand> candidateRegistry;
36     private final ActorRef<OwnerSupervisorCommand> ownerSupervisor;
37     private final ActorRef<StateCheckerCommand> ownerStateChecker;
38
39     private EOSMain(final ActorContext<BootstrapCommand> context) {
40         super(context);
41
42         final String role = Cluster.get(context.getSystem()).selfMember().getRoles().iterator().next();
43
44         listenerRegistry = context.spawn(EntityTypeListenerRegistry.create(role), "ListenerRegistry");
45         candidateRegistry = context.spawn(CandidateRegistry.create(), "CandidateRegistry");
46         ownerStateChecker = context.spawn(OwnerStateChecker.create(role), "OwnerStateChecker");
47
48         final ClusterSingleton clusterSingleton = ClusterSingleton.get(context.getSystem());
49         // start the initial sync behavior that switches to the regular one after syncing
50         ownerSupervisor = clusterSingleton.init(SingletonActor.of(OwnerSyncer.create(), "OwnerSupervisor"));
51     }
52
53     public static Behavior<BootstrapCommand> create() {
54         return Behaviors.setup(EOSMain::new);
55     }
56
57     @Override
58     public Receive<BootstrapCommand> createReceive() {
59         return newReceiveBuilder()
60                 .onMessage(GetRunningContext.class, this::onGetRunningContext)
61                 .onMessage(Terminate.class, this::onTerminate)
62                 .build();
63     }
64
65     private Behavior<BootstrapCommand> onGetRunningContext(final GetRunningContext request) {
66         request.getReplyTo().tell(
67                 new RunningContext(listenerRegistry, candidateRegistry, ownerStateChecker, ownerSupervisor));
68         return this;
69     }
70
71     private Behavior<BootstrapCommand> onTerminate(final Terminate request) {
72         request.getReplyTo().tell(Empty.getInstance());
73         return Behaviors.stopped();
74     }
75 }