c853124d9a2af30a7e7459ba570fdc625f42c754
[controller.git] / opendaylight / md-sal / eos-dom-akka / src / main / java / org / opendaylight / controller / eos / akka / registry / listener / type / EntityTypeListenerRegistry.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.registry.listener.type;
9
10 import static java.util.Objects.requireNonNull;
11
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;
19 import java.util.Map;
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;
28
29 public class EntityTypeListenerRegistry extends AbstractBehavior<TypeListenerRegistryCommand> {
30     private static final Logger LOG = LoggerFactory.getLogger(EntityTypeListenerRegistry.class);
31
32     private final Map<DOMEntityOwnershipListener, ActorRef<TypeListenerCommand>> spawnedListenerActors =
33             new HashMap<>();
34     private final String localMember;
35
36     public EntityTypeListenerRegistry(final ActorContext<TypeListenerRegistryCommand> context,
37                                       final String localMember) {
38         super(context);
39         this.localMember = requireNonNull(localMember);
40     }
41
42     public static Behavior<TypeListenerRegistryCommand> create(final String role) {
43         return Behaviors.setup(ctx -> new EntityTypeListenerRegistry(ctx, role));
44     }
45
46     @Override
47     public Receive<TypeListenerRegistryCommand> createReceive() {
48         return newReceiveBuilder()
49                 .onMessage(RegisterListener.class, this::onRegisterListener)
50                 .onMessage(UnregisterListener.class, this::onUnregisterListener)
51                 .build();
52     }
53
54     private Behavior<TypeListenerRegistryCommand> onRegisterListener(final RegisterListener command) {
55         LOG.debug("Spawning entity type listener actor for: {}", command.getEntityType());
56
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);
62         return this;
63     }
64
65     private Behavior<TypeListenerRegistryCommand> onUnregisterListener(final UnregisterListener command) {
66         LOG.debug("Stopping entity type listener actor for: {}", command.getEntityType());
67
68         getContext().stop(spawnedListenerActors.remove(command.getDelegateListener()));
69         return this;
70     }
71
72     private static String encodeEntityToActorName(final String entityType) {
73         return "type=" + entityType + "-" + UUID.randomUUID();
74     }
75 }