Add cluster-admin api for datacenter activation
[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 akka.actor.typed.Behavior;
11 import akka.actor.typed.javadsl.AbstractBehavior;
12 import akka.actor.typed.javadsl.ActorContext;
13 import akka.actor.typed.javadsl.Behaviors;
14 import akka.actor.typed.javadsl.Receive;
15 import akka.cluster.Member;
16 import akka.cluster.typed.Cluster;
17 import org.opendaylight.controller.eos.akka.owner.supervisor.command.ActivateDataCenter;
18 import org.opendaylight.controller.eos.akka.owner.supervisor.command.OwnerSupervisorCommand;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Initial Supervisor behavior that stays idle and only switches itself to the active behavior when its running
24  * in the primary datacenter, or is activated on demand. Once the supervisor instance is no longer needed in the
25  * secondary datacenter it needs to be deactivated manually.
26  */
27 public final class IdleSupervisor extends AbstractBehavior<OwnerSupervisorCommand> {
28     private static final Logger LOG = LoggerFactory.getLogger(IdleSupervisor.class);
29
30     private static final String DATACENTER_PREFIX = "dc-";
31     private static final String DEFAULT_DATACENTER = "dc-default";
32
33     private IdleSupervisor(final ActorContext<OwnerSupervisorCommand> context) {
34         super(context);
35         final Cluster cluster = Cluster.get(context.getSystem());
36
37         final String datacenterRole = extractDatacenterRole(cluster.selfMember());
38         if (datacenterRole.equals(DEFAULT_DATACENTER)) {
39             LOG.debug("No datacenter configured, activating default data center");
40             context.getSelf().tell(new ActivateDataCenter(null));
41         }
42
43         LOG.debug("Idle supervisor started on {}.", cluster.selfMember());
44     }
45
46     public static Behavior<OwnerSupervisorCommand> create() {
47
48         return Behaviors.setup(IdleSupervisor::new);
49     }
50
51     @Override
52     public Receive<OwnerSupervisorCommand> createReceive() {
53         return newReceiveBuilder()
54                 .onMessage(ActivateDataCenter.class, this::onActivateDataCenter)
55                 .build();
56     }
57
58     private Behavior<OwnerSupervisorCommand> onActivateDataCenter(final ActivateDataCenter message) {
59         LOG.debug("Received ActivateDataCenter command switching to syncer behavior,");
60         return OwnerSyncer.create(message.getReplyTo());
61     }
62
63     private String extractDatacenterRole(final Member selfMember) {
64         return selfMember.getRoles().stream()
65                 .filter(role -> role.startsWith(DATACENTER_PREFIX))
66                 .findFirst()
67                 .orElseThrow(() -> new IllegalArgumentException(selfMember + " does not have a valid role"));
68     }
69 }