79b876f626800106bb8d3885710a0a8bb60fde48
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / AbstractClientActorBehavior.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 com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15
16 /**
17  * Base behavior attached to {@link AbstractClientActor}. Exposes
18  * @author user
19  *
20  * @param <C> Type of associated context
21  *
22  * @author Robert Varga
23  */
24 @Beta
25 public abstract class AbstractClientActorBehavior<C extends AbstractClientActorContext> {
26     private final C context;
27
28     AbstractClientActorBehavior(final @Nonnull C context) {
29         // Hidden to prevent outside subclasses. Users instantiated this via ClientActorBehavior
30         this.context = Preconditions.checkNotNull(context);
31     }
32
33     /**
34      * Return an {@link AbstractClientActorContext} associated with this {@link AbstractClientActor}.
35      *
36      * @return A client actor context instance.
37      */
38     protected final @Nonnull C context() {
39         return context;
40     }
41
42     /**
43      * Return the persistence identifier associated with this {@link AbstractClientActor}. This identifier should be
44      * used in logging to identify this actor.
45      *
46      * @return Persistence identifier
47      */
48     protected final @Nonnull String persistenceId() {
49         return context.persistenceId();
50     }
51
52     /**
53      * Return an {@link ActorRef} of this ClientActor.
54      *
55      * @return
56      */
57     public final @Nonnull ActorRef self() {
58         return context.self();
59     }
60
61     /**
62      * Implementation-internal method for handling an incoming command message.
63      *
64      * @param command Command message
65      * @return Behavior which should be used with the next message. Return null if this actor should shut down.
66      */
67     abstract @Nullable AbstractClientActorBehavior<?> onReceiveCommand(@Nonnull Object command);
68
69     /**
70      * Implementation-internal method for handling an incoming recovery message coming from persistence.
71      *
72      * @param recover Recover message
73      * @return Behavior which should be used with the next message. Return null if this actor should shut down.
74      */
75     abstract @Nullable AbstractClientActorBehavior<?> onReceiveRecover(@Nonnull Object recover);
76 }