dab699e394d5fc65bca8ab681d1cefdc9b045136
[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.SupervisorStrategy;
13 import akka.actor.typed.javadsl.AbstractBehavior;
14 import akka.actor.typed.javadsl.ActorContext;
15 import akka.actor.typed.javadsl.Behaviors;
16 import akka.actor.typed.javadsl.Receive;
17 import akka.cluster.typed.Cluster;
18 import akka.cluster.typed.ClusterSingleton;
19 import akka.cluster.typed.SingletonActor;
20 import org.opendaylight.controller.eos.akka.bootstrap.command.BootstrapCommand;
21 import org.opendaylight.controller.eos.akka.bootstrap.command.GetRunningContext;
22 import org.opendaylight.controller.eos.akka.bootstrap.command.RunningContext;
23 import org.opendaylight.controller.eos.akka.bootstrap.command.Terminate;
24 import org.opendaylight.controller.eos.akka.owner.checker.OwnerStateChecker;
25 import org.opendaylight.controller.eos.akka.owner.checker.command.StateCheckerCommand;
26 import org.opendaylight.controller.eos.akka.owner.supervisor.IdleSupervisor;
27 import org.opendaylight.controller.eos.akka.owner.supervisor.command.OwnerSupervisorCommand;
28 import org.opendaylight.controller.eos.akka.registry.candidate.CandidateRegistryInit;
29 import org.opendaylight.controller.eos.akka.registry.candidate.command.CandidateRegistryCommand;
30 import org.opendaylight.controller.eos.akka.registry.listener.type.EntityTypeListenerRegistry;
31 import org.opendaylight.controller.eos.akka.registry.listener.type.command.TypeListenerRegistryCommand;
32 import org.opendaylight.mdsal.binding.dom.codec.api.BindingInstanceIdentifierCodec;
33 import org.opendaylight.yangtools.yang.common.Empty;
34
35 public final class EOSMain extends AbstractBehavior<BootstrapCommand> {
36     private final ActorRef<TypeListenerRegistryCommand> listenerRegistry;
37     private final ActorRef<CandidateRegistryCommand> candidateRegistry;
38     private final ActorRef<OwnerSupervisorCommand> ownerSupervisor;
39     private final ActorRef<StateCheckerCommand> ownerStateChecker;
40
41     private EOSMain(final ActorContext<BootstrapCommand> context, final BindingInstanceIdentifierCodec iidCodec) {
42         super(context);
43
44         final String role = Cluster.get(context.getSystem()).selfMember().getRoles().iterator().next();
45
46         listenerRegistry = context.spawn(EntityTypeListenerRegistry.create(role), "ListenerRegistry");
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(
51                 SingletonActor.of(Behaviors.supervise(IdleSupervisor.create(iidCodec))
52                         .onFailure(SupervisorStrategy.restart()), "OwnerSupervisor"));
53         candidateRegistry = context.spawn(CandidateRegistryInit.create(ownerSupervisor), "CandidateRegistry");
54
55         ownerStateChecker = context.spawn(OwnerStateChecker.create(role, ownerSupervisor, iidCodec),
56                 "OwnerStateChecker");
57     }
58
59     public static Behavior<BootstrapCommand> create(final BindingInstanceIdentifierCodec iidCodec) {
60         return Behaviors.setup(context -> new EOSMain(context, iidCodec));
61     }
62
63     @Override
64     public Receive<BootstrapCommand> createReceive() {
65         return newReceiveBuilder()
66                 .onMessage(GetRunningContext.class, this::onGetRunningContext)
67                 .onMessage(Terminate.class, this::onTerminate)
68                 .build();
69     }
70
71     private Behavior<BootstrapCommand> onGetRunningContext(final GetRunningContext request) {
72         request.getReplyTo().tell(
73                 new RunningContext(listenerRegistry, candidateRegistry, ownerStateChecker, ownerSupervisor));
74         return this;
75     }
76
77     private Behavior<BootstrapCommand> onTerminate(final Terminate request) {
78         request.getReplyTo().tell(Empty.value());
79         return Behaviors.stopped();
80     }
81 }