2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.eos.akka.bootstrap;
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.IdleSupervisor;
26 import org.opendaylight.controller.eos.akka.owner.supervisor.command.OwnerSupervisorCommand;
27 import org.opendaylight.controller.eos.akka.registry.candidate.CandidateRegistryInit;
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.mdsal.binding.dom.codec.api.BindingInstanceIdentifierCodec;
32 import org.opendaylight.yangtools.yang.common.Empty;
34 public final class EOSMain extends AbstractBehavior<BootstrapCommand> {
35 private final ActorRef<TypeListenerRegistryCommand> listenerRegistry;
36 private final ActorRef<CandidateRegistryCommand> candidateRegistry;
37 private final ActorRef<OwnerSupervisorCommand> ownerSupervisor;
38 private final ActorRef<StateCheckerCommand> ownerStateChecker;
40 private EOSMain(final ActorContext<BootstrapCommand> context, final BindingInstanceIdentifierCodec iidCodec) {
43 final String role = Cluster.get(context.getSystem()).selfMember().getRoles().iterator().next();
45 listenerRegistry = context.spawn(EntityTypeListenerRegistry.create(role), "ListenerRegistry");
47 final ClusterSingleton clusterSingleton = ClusterSingleton.get(context.getSystem());
48 // start the initial sync behavior that switches to the regular one after syncing
49 ownerSupervisor = clusterSingleton.init(
50 SingletonActor.of(IdleSupervisor.create(iidCodec), "OwnerSupervisor"));
52 candidateRegistry = context.spawn(CandidateRegistryInit.create(ownerSupervisor), "CandidateRegistry");
54 ownerStateChecker = context.spawn(OwnerStateChecker.create(role, ownerSupervisor, iidCodec),
58 public static Behavior<BootstrapCommand> create(final BindingInstanceIdentifierCodec iidCodec) {
59 return Behaviors.setup(context -> new EOSMain(context, iidCodec));
63 public Receive<BootstrapCommand> createReceive() {
64 return newReceiveBuilder()
65 .onMessage(GetRunningContext.class, this::onGetRunningContext)
66 .onMessage(Terminate.class, this::onTerminate)
70 private Behavior<BootstrapCommand> onGetRunningContext(final GetRunningContext request) {
71 request.getReplyTo().tell(
72 new RunningContext(listenerRegistry, candidateRegistry, ownerStateChecker, ownerSupervisor));
76 private Behavior<BootstrapCommand> onTerminate(final Terminate request) {
77 request.getReplyTo().tell(Empty.getInstance());
78 return Behaviors.stopped();