Bump versions to 4.0.4-SNAPSHOT
[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.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;
29
30 public class EntityTypeListenerRegistry extends AbstractBehavior<TypeListenerRegistryCommand> {
31     private static final Logger LOG = LoggerFactory.getLogger(EntityTypeListenerRegistry.class);
32
33     private final Map<DOMEntityOwnershipListener, ActorRef<TypeListenerCommand>> spawnedListenerActors =
34             new HashMap<>();
35     private final String localMember;
36
37     public EntityTypeListenerRegistry(final ActorContext<TypeListenerRegistryCommand> context,
38                                       final String localMember) {
39         super(context);
40         this.localMember = requireNonNull(localMember);
41     }
42
43     public static Behavior<TypeListenerRegistryCommand> create(final String role) {
44         return Behaviors.setup(ctx -> new EntityTypeListenerRegistry(ctx, role));
45     }
46
47     @Override
48     public Receive<TypeListenerRegistryCommand> createReceive() {
49         return newReceiveBuilder()
50                 .onMessage(RegisterListener.class, this::onRegisterListener)
51                 .onMessage(UnregisterListener.class, this::onUnregisterListener)
52                 .build();
53     }
54
55     private Behavior<TypeListenerRegistryCommand> onRegisterListener(final RegisterListener command) {
56         LOG.debug("Spawning entity type listener actor for: {}", command.getEntityType());
57
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);
63         return this;
64     }
65
66     private Behavior<TypeListenerRegistryCommand> onUnregisterListener(final UnregisterListener command) {
67         LOG.debug("Stopping entity type listener actor for: {}", command.getEntityType());
68
69         spawnedListenerActors.remove(command.getDelegateListener()).tell(TerminateListener.INSTANCE);
70         return this;
71     }
72
73     private static String encodeEntityToActorName(final String entityType) {
74         return "type=" + entityType + "-" + UUID.randomUUID();
75     }
76 }