237b570320543939df3692d720e55aefd59d61dc
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / actors / client / ClientActorBehavior.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.datastore.actors.client;
9
10 import com.google.common.annotations.Beta;
11 import javax.annotation.Nonnull;
12 import javax.annotation.Nullable;
13 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
14 import org.opendaylight.controller.cluster.access.concepts.RequestException;
15 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
16 import org.opendaylight.controller.cluster.access.concepts.RetiredGenerationException;
17 import org.opendaylight.yangtools.concepts.Identifiable;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * A behavior, which handles messages sent to a {@link AbstractClientActor}.
23  *
24  * @param <T> Frontend type
25  *
26  * @author Robert Varga
27  */
28 @Beta
29 public abstract class ClientActorBehavior extends RecoveredClientActorBehavior<ClientActorContext>
30         implements Identifiable<ClientIdentifier> {
31     private static final Logger LOG = LoggerFactory.getLogger(ClientActorBehavior.class);
32
33     protected ClientActorBehavior(final @Nonnull ClientActorContext context) {
34         super(context);
35     }
36
37     @Override
38     final ClientActorBehavior onReceiveCommand(final Object command) {
39         if (command instanceof InternalCommand) {
40             return ((InternalCommand) command).execute();
41         } else if (command instanceof RequestFailure) {
42             final RequestFailure<?, ?> failure = (RequestFailure<?, ?>) command;
43             final RequestException cause = failure.getCause();
44             if (cause instanceof RetiredGenerationException) {
45                 LOG.error("{}: current generation {} has been superseded", persistenceId(), getIdentifier(), cause);
46                 haltClient(cause);
47                 return null;
48             }
49         }
50
51         // TODO: any client-common logic (such as validation and common dispatch) needs to go here
52         return onCommand(command);
53     }
54
55     @Override
56     public final @Nonnull ClientIdentifier getIdentifier() {
57         return context().getIdentifier();
58     }
59
60     /**
61      * Halt And Catch Fire.
62      *
63      * Halt processing on this client. Implementations need to ensure they initiate state flush procedures. No attempt
64      * to use this instance should be made after this method returns. Any such use may result in undefined behavior.
65      *
66      * @param cause Failure cause
67      */
68     protected abstract void haltClient(@Nonnull Throwable cause);
69
70     /**
71      * Override this method to handle any command which is not handled by the base behavior.
72      *
73      * @param command
74      * @return Next behavior to use, null if this actor should shut down.
75      */
76     protected abstract @Nullable ClientActorBehavior onCommand(@Nonnull Object command);
77
78     /**
79      * Override this method to provide a backend resolver instance.
80      *
81      * @return
82      */
83     protected abstract @Nonnull BackendInfoResolver<?> resolver();
84 }