Improve segmented journal actor metrics
[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.AbstractPersistentActor;
13 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * Frontend actor which takes care of persisting generations and creates an appropriate ClientIdentifier.
19  */
20 public abstract class AbstractClientActor extends AbstractPersistentActor {
21     private static final Logger LOG = LoggerFactory.getLogger(AbstractClientActor.class);
22     private AbstractClientActorBehavior<?> currentBehavior;
23
24     protected AbstractClientActor(final FrontendIdentifier frontendId) {
25         currentBehavior = new RecoveringClientActorBehavior(
26                 new InitialClientActorContext(this, frontendId.toPersistentId()), frontendId);
27     }
28
29     @Override
30     public final String persistenceId() {
31         return currentBehavior.persistenceId();
32     }
33
34     @Override
35     public void postStop() throws Exception {
36         if (currentBehavior != null) {
37             currentBehavior.close();
38         }
39
40         super.postStop();
41     }
42
43     private void switchBehavior(final AbstractClientActorBehavior<?> nextBehavior) {
44         if (!currentBehavior.equals(nextBehavior)) {
45             if (nextBehavior == null) {
46                 LOG.debug("{}: shutting down", persistenceId());
47                 self().tell(PoisonPill.getInstance(), ActorRef.noSender());
48             } else {
49                 LOG.debug("{}: switched from {} to {}", persistenceId(), currentBehavior, nextBehavior);
50             }
51
52             currentBehavior.close();
53             currentBehavior = nextBehavior;
54         }
55     }
56
57     @Override
58     public Receive createReceive() {
59         return receiveBuilder().matchAny(this::onReceiveCommand).build();
60     }
61
62     @Override
63     public Receive createReceiveRecover() {
64         return receiveBuilder().matchAny(this::onReceiveRecover).build();
65     }
66
67     private void onReceiveCommand(final Object command) {
68         if (command == null) {
69             LOG.debug("{}: ignoring null command", persistenceId());
70             return;
71         }
72
73         if (currentBehavior != null) {
74             switchBehavior(currentBehavior.onReceiveCommand(command));
75         } else {
76             LOG.debug("{}: shutting down, ignoring command {}", persistenceId(), command);
77         }
78     }
79
80     private void onReceiveRecover(final Object recover) {
81         switchBehavior(currentBehavior.onReceiveRecover(recover));
82     }
83
84     protected abstract ClientActorBehavior<?> initialBehavior(ClientActorContext context);
85
86     protected abstract ClientActorConfig getClientActorConfig();
87 }