Make Payload Serializable
[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.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;
33
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;
39
40     private EOSMain(final ActorContext<BootstrapCommand> context, final BindingInstanceIdentifierCodec iidCodec) {
41         super(context);
42
43         final String role = Cluster.get(context.getSystem()).selfMember().getRoles().iterator().next();
44
45         listenerRegistry = context.spawn(EntityTypeListenerRegistry.create(role), "ListenerRegistry");
46
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"));
51
52         candidateRegistry = context.spawn(CandidateRegistryInit.create(ownerSupervisor), "CandidateRegistry");
53
54         ownerStateChecker = context.spawn(OwnerStateChecker.create(role, ownerSupervisor, iidCodec),
55                 "OwnerStateChecker");
56     }
57
58     public static Behavior<BootstrapCommand> create(final BindingInstanceIdentifierCodec iidCodec) {
59         return Behaviors.setup(context -> new EOSMain(context, iidCodec));
60     }
61
62     @Override
63     public Receive<BootstrapCommand> createReceive() {
64         return newReceiveBuilder()
65                 .onMessage(GetRunningContext.class, this::onGetRunningContext)
66                 .onMessage(Terminate.class, this::onTerminate)
67                 .build();
68     }
69
70     private Behavior<BootstrapCommand> onGetRunningContext(final GetRunningContext request) {
71         request.getReplyTo().tell(
72                 new RunningContext(listenerRegistry, candidateRegistry, ownerStateChecker, ownerSupervisor));
73         return this;
74     }
75
76     private Behavior<BootstrapCommand> onTerminate(final Terminate request) {
77         request.getReplyTo().tell(Empty.getInstance());
78         return Behaviors.stopped();
79     }
80 }