2fb4c943079527f489cbaeb609b5c10ee9c6b8d3
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / AbstractClientActor.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.cluster.access.client;
9
10 import akka.actor.ActorRef;
11 import akka.actor.PoisonPill;
12 import akka.persistence.UntypedPersistentActor;
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;
17
18 /**
19  * Frontend actor which takes care of persisting generations and creates an appropriate ClientIdentifier.
20  *
21  * @author Robert Varga
22  */
23 @Beta
24 public abstract class AbstractClientActor extends UntypedPersistentActor {
25     private static final Logger LOG = LoggerFactory.getLogger(AbstractClientActor.class);
26     private AbstractClientActorBehavior<?> currentBehavior;
27
28     protected AbstractClientActor(final FrontendIdentifier frontendId) {
29         currentBehavior = new RecoveringClientActorBehavior(
30                 new InitialClientActorContext(this, frontendId.toPersistentId()), frontendId);
31     }
32
33     @Override
34     public final String persistenceId() {
35         return currentBehavior.persistenceId();
36     }
37
38     private void switchBehavior(final AbstractClientActorBehavior<?> nextBehavior) {
39         if (!currentBehavior.equals(nextBehavior)) {
40             if (nextBehavior == null) {
41                 LOG.debug("{}: shutting down", persistenceId());
42                 self().tell(PoisonPill.getInstance(), ActorRef.noSender());
43             } else {
44                 LOG.debug("{}: switched from {} to {}", persistenceId(), currentBehavior, nextBehavior);
45             }
46
47             currentBehavior = nextBehavior;
48         }
49     }
50
51     @Override
52     public final void onReceiveCommand(final Object command) {
53         if (command == null) {
54             LOG.debug("{}: ignoring null command", persistenceId());
55             return;
56         }
57
58         if (currentBehavior != null) {
59             switchBehavior(currentBehavior.onReceiveCommand(command));
60         } else {
61             LOG.debug("{}: shutting down, ignoring command {}", persistenceId(), command);
62         }
63     }
64
65     @Override
66     public final void onReceiveRecover(final Object recover) {
67         switchBehavior(currentBehavior.onReceiveRecover(recover));
68     }
69
70     protected abstract ClientActorBehavior<?> initialBehavior(ClientActorContext context);
71
72     protected abstract ClientActorConfig getClientActorConfig();
73 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.