Move {Identifiable,Persistent,}Payload
[controller.git] / opendaylight / md-sal / eos-dom-akka / src / main / java / org / opendaylight / controller / eos / akka / owner / supervisor / IdleSupervisor.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.owner.supervisor;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.typed.Behavior;
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.Member;
17 import akka.cluster.typed.Cluster;
18 import akka.pattern.StatusReply;
19 import org.opendaylight.controller.eos.akka.owner.supervisor.command.ActivateDataCenter;
20 import org.opendaylight.controller.eos.akka.owner.supervisor.command.ClearCandidates;
21 import org.opendaylight.controller.eos.akka.owner.supervisor.command.ClearCandidatesForMember;
22 import org.opendaylight.controller.eos.akka.owner.supervisor.command.GetEntitiesBackendRequest;
23 import org.opendaylight.controller.eos.akka.owner.supervisor.command.GetEntityBackendRequest;
24 import org.opendaylight.controller.eos.akka.owner.supervisor.command.GetEntityOwnerBackendRequest;
25 import org.opendaylight.controller.eos.akka.owner.supervisor.command.OwnerSupervisorCommand;
26 import org.opendaylight.controller.eos.akka.owner.supervisor.command.OwnerSupervisorRequest;
27 import org.opendaylight.mdsal.binding.dom.codec.api.BindingInstanceIdentifierCodec;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Initial Supervisor behavior that stays idle and only switches itself to the active behavior when its running
33  * in the primary datacenter, or is activated on demand. Once the supervisor instance is no longer needed in the
34  * secondary datacenter it needs to be deactivated manually.
35  */
36 public final class IdleSupervisor extends AbstractSupervisor {
37     private static final Logger LOG = LoggerFactory.getLogger(IdleSupervisor.class);
38
39     private static final String DATACENTER_PREFIX = "dc-";
40     private static final String DEFAULT_DATACENTER = "dc-default";
41
42     private final BindingInstanceIdentifierCodec iidCodec;
43
44     private IdleSupervisor(final ActorContext<OwnerSupervisorCommand> context,
45                            final BindingInstanceIdentifierCodec iidCodec) {
46         super(context);
47         this.iidCodec = requireNonNull(iidCodec);
48         final Cluster cluster = Cluster.get(context.getSystem());
49
50         final String datacenterRole = extractDatacenterRole(cluster.selfMember());
51         if (datacenterRole.equals(DEFAULT_DATACENTER)) {
52             LOG.debug("No datacenter configured, activating default data center");
53             context.getSelf().tell(new ActivateDataCenter(null));
54         }
55
56         LOG.debug("Idle supervisor started on {}.", cluster.selfMember());
57     }
58
59     public static Behavior<OwnerSupervisorCommand> create(final BindingInstanceIdentifierCodec iidCodec) {
60         return Behaviors.setup(context -> new IdleSupervisor(context, iidCodec));
61     }
62
63     @Override
64     public Receive<OwnerSupervisorCommand> createReceive() {
65         return newReceiveBuilder()
66                 .onMessage(ActivateDataCenter.class, this::onActivateDataCenter)
67                 .onMessage(GetEntitiesBackendRequest.class, this::onFailEntityRpc)
68                 .onMessage(GetEntityBackendRequest.class, this::onFailEntityRpc)
69                 .onMessage(GetEntityOwnerBackendRequest.class, this::onFailEntityRpc)
70                 .onMessage(ClearCandidatesForMember.class, this::onClearCandidatesForMember)
71                 .onMessage(ClearCandidates.class, this::finishClearCandidates)
72                 .build();
73     }
74
75     private Behavior<OwnerSupervisorCommand> onFailEntityRpc(final OwnerSupervisorRequest message) {
76         LOG.debug("Failing rpc request. {}", message);
77         message.getReplyTo().tell(StatusReply.error("OwnerSupervisor is inactive so it"
78                 + " cannot handle entity rpc requests."));
79         return this;
80     }
81
82     private Behavior<OwnerSupervisorCommand> onActivateDataCenter(final ActivateDataCenter message) {
83         LOG.debug("Received ActivateDataCenter command switching to syncer behavior,");
84         return OwnerSyncer.create(message.getReplyTo(), iidCodec);
85     }
86
87     private static String extractDatacenterRole(final Member selfMember) {
88         return selfMember.getRoles().stream()
89                 .filter(role -> role.startsWith(DATACENTER_PREFIX))
90                 .findFirst()
91                 .orElseThrow(() -> new IllegalArgumentException(selfMember + " does not have a valid role"));
92     }
93
94     @Override
95     Logger getLogger() {
96         return LOG;
97     }
98 }