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.TypeListenerCommand;
23 import org.opendaylight.controller.eos.akka.registry.listener.type.command.TypeListenerRegistryCommand;
24 import org.opendaylight.controller.eos.akka.registry.listener.type.command.UnregisterListener;
25 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class EntityTypeListenerRegistry extends AbstractBehavior<TypeListenerRegistryCommand> {
30 private static final Logger LOG = LoggerFactory.getLogger(EntityTypeListenerRegistry.class);
32 private final Map<DOMEntityOwnershipListener, ActorRef<TypeListenerCommand>> spawnedListenerActors =
34 private final String localMember;
36 public EntityTypeListenerRegistry(final ActorContext<TypeListenerRegistryCommand> context,
37 final String localMember) {
39 this.localMember = requireNonNull(localMember);
42 public static Behavior<TypeListenerRegistryCommand> create(final String role) {
43 return Behaviors.setup(ctx -> new EntityTypeListenerRegistry(ctx, role));
47 public Receive<TypeListenerRegistryCommand> createReceive() {
48 return newReceiveBuilder()
49 .onMessage(RegisterListener.class, this::onRegisterListener)
50 .onMessage(UnregisterListener.class, this::onUnregisterListener)
54 private Behavior<TypeListenerRegistryCommand> onRegisterListener(final RegisterListener command) {
55 LOG.debug("Spawning entity type listener actor for: {}", command.getEntityType());
57 final ActorRef<TypeListenerCommand> listenerActor =
58 getContext().spawn(EntityTypeListenerActor.create(localMember,
59 command.getEntityType(), command.getDelegateListener()),
60 "TypeListener:" + encodeEntityToActorName(command.getEntityType()));
61 spawnedListenerActors.put(command.getDelegateListener(), listenerActor);
65 private Behavior<TypeListenerRegistryCommand> onUnregisterListener(final UnregisterListener command) {
66 LOG.debug("Stopping entity type listener actor for: {}", command.getEntityType());
68 getContext().stop(spawnedListenerActors.remove(command.getDelegateListener()));
72 private static String encodeEntityToActorName(final String entityType) {
73 return "type=" + entityType + "-" + UUID.randomUUID();