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.registry.listener.type;
10 import static java.util.Objects.requireNonNull;
12 import akka.actor.typed.ActorRef;
13 import akka.actor.typed.Behavior;
14 import akka.actor.typed.javadsl.AbstractBehavior;
15 import akka.actor.typed.javadsl.ActorContext;
16 import akka.actor.typed.javadsl.Behaviors;
17 import akka.actor.typed.javadsl.Receive;
18 import java.util.HashMap;
20 import java.util.UUID;
21 import org.opendaylight.controller.eos.akka.registry.listener.type.command.RegisterListener;
22 import org.opendaylight.controller.eos.akka.registry.listener.type.command.TerminateListener;
23 import org.opendaylight.controller.eos.akka.registry.listener.type.command.TypeListenerCommand;
24 import org.opendaylight.controller.eos.akka.registry.listener.type.command.TypeListenerRegistryCommand;
25 import org.opendaylight.controller.eos.akka.registry.listener.type.command.UnregisterListener;
26 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class EntityTypeListenerRegistry extends AbstractBehavior<TypeListenerRegistryCommand> {
31 private static final Logger LOG = LoggerFactory.getLogger(EntityTypeListenerRegistry.class);
33 private final Map<DOMEntityOwnershipListener, ActorRef<TypeListenerCommand>> spawnedListenerActors =
35 private final String localMember;
37 public EntityTypeListenerRegistry(final ActorContext<TypeListenerRegistryCommand> context,
38 final String localMember) {
40 this.localMember = requireNonNull(localMember);
43 public static Behavior<TypeListenerRegistryCommand> create(final String role) {
44 return Behaviors.setup(ctx -> new EntityTypeListenerRegistry(ctx, role));
48 public Receive<TypeListenerRegistryCommand> createReceive() {
49 return newReceiveBuilder()
50 .onMessage(RegisterListener.class, this::onRegisterListener)
51 .onMessage(UnregisterListener.class, this::onUnregisterListener)
55 private Behavior<TypeListenerRegistryCommand> onRegisterListener(final RegisterListener command) {
56 LOG.debug("Spawning entity type listener actor for: {}", command.getEntityType());
58 final ActorRef<TypeListenerCommand> listenerActor =
59 getContext().spawn(EntityTypeListenerActor.create(localMember,
60 command.getEntityType(), command.getDelegateListener()),
61 "TypeListener:" + encodeEntityToActorName(command.getEntityType()));
62 spawnedListenerActors.put(command.getDelegateListener(), listenerActor);
66 private Behavior<TypeListenerRegistryCommand> onUnregisterListener(final UnregisterListener command) {
67 LOG.debug("Stopping entity type listener actor for: {}", command.getEntityType());
69 spawnedListenerActors.remove(command.getDelegateListener()).tell(TerminateListener.INSTANCE);
73 private static String encodeEntityToActorName(final String entityType) {
74 return "type=" + entityType + "-" + UUID.randomUUID();