2 * Copyright (c) 2016 Cisco Systems, Inc. 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.cluster.access.client;
10 import akka.actor.ActorRef;
11 import akka.actor.PoisonPill;
12 import akka.persistence.AbstractPersistentActor;
13 import com.google.common.annotations.Beta;
14 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
19 * Frontend actor which takes care of persisting generations and creates an appropriate ClientIdentifier.
21 * @author Robert Varga
24 public abstract class AbstractClientActor extends AbstractPersistentActor {
25 private static final Logger LOG = LoggerFactory.getLogger(AbstractClientActor.class);
26 private AbstractClientActorBehavior<?> currentBehavior;
28 protected AbstractClientActor(final FrontendIdentifier frontendId) {
29 currentBehavior = new RecoveringClientActorBehavior(
30 new InitialClientActorContext(this, frontendId.toPersistentId()), frontendId);
34 public final String persistenceId() {
35 return currentBehavior.persistenceId();
39 public void postStop() throws Exception {
40 if (currentBehavior != null) {
41 currentBehavior.close();
47 private void switchBehavior(final AbstractClientActorBehavior<?> nextBehavior) {
48 if (!currentBehavior.equals(nextBehavior)) {
49 if (nextBehavior == null) {
50 LOG.debug("{}: shutting down", persistenceId());
51 self().tell(PoisonPill.getInstance(), ActorRef.noSender());
53 LOG.debug("{}: switched from {} to {}", persistenceId(), currentBehavior, nextBehavior);
56 currentBehavior.close();
57 currentBehavior = nextBehavior;
62 public Receive createReceive() {
63 return receiveBuilder().matchAny(this::onReceiveCommand).build();
67 public Receive createReceiveRecover() {
68 return receiveBuilder().matchAny(this::onReceiveRecover).build();
71 private void onReceiveCommand(final Object command) {
72 if (command == null) {
73 LOG.debug("{}: ignoring null command", persistenceId());
77 if (currentBehavior != null) {
78 switchBehavior(currentBehavior.onReceiveCommand(command));
80 LOG.debug("{}: shutting down, ignoring command {}", persistenceId(), command);
84 private void onReceiveRecover(final Object recover) {
85 switchBehavior(currentBehavior.onReceiveRecover(recover));
88 protected abstract ClientActorBehavior<?> initialBehavior(ClientActorContext context);
90 protected abstract ClientActorConfig getClientActorConfig();