Add an actor for entity rpc execution.
[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         candidateRegistry = context.spawn(CandidateRegistryInit.create(), "CandidateRegistry");
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(IdleSupervisor.create(iidCodec), "OwnerSupervisor"));
52
53         ownerStateChecker = context.spawn(OwnerStateChecker.create(role, ownerSupervisor, iidCodec),
54                 "OwnerStateChecker");
55     }
56
57     public static Behavior<BootstrapCommand> create(final BindingInstanceIdentifierCodec iidCodec) {
58         return Behaviors.setup(context -> new EOSMain(context, iidCodec));
59     }
60
61     @Override
62     public Receive<BootstrapCommand> createReceive() {
63         return newReceiveBuilder()
64                 .onMessage(GetRunningContext.class, this::onGetRunningContext)
65                 .onMessage(Terminate.class, this::onTerminate)
66                 .build();
67     }
68
69     private Behavior<BootstrapCommand> onGetRunningContext(final GetRunningContext request) {
70         request.getReplyTo().tell(
71                 new RunningContext(listenerRegistry, candidateRegistry, ownerStateChecker, ownerSupervisor));
72         return this;
73     }
74
75     private Behavior<BootstrapCommand> onTerminate(final Terminate request) {
76         request.getReplyTo().tell(Empty.getInstance());
77         return Behaviors.stopped();
78     }
79 }