1d812b7590b0715468e9e53793a6529320785f0b
[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 RequestFailure) {
40             final RequestFailure<?, ?> failure = (RequestFailure<?, ?>) command;
41             final RequestException cause = failure.getCause();
42             if (cause instanceof RetiredGenerationException) {
43                 LOG.error("{}: current generation {} has been superseded", persistenceId(), getIdentifier(), cause);
44                 haltClient(cause);
45                 return null;
46             }
47         }
48
49         // TODO: any client-common logic (such as validation and common dispatch) needs to go here
50         return onCommand(command);
51     }
52
53     @Override
54     public final @Nonnull ClientIdentifier getIdentifier() {
55         return context().getIdentifier();
56     }
57
58     /**
59      * Halt And Catch Fire.
60      *
61      * Halt processing on this client. Implementations need to ensure they initiate state flush procedures. No attempt
62      * to use this instance should be made after this method returns. Any such use may result in undefined behavior.
63      *
64      * @param cause Failure cause
65      */
66     protected abstract void haltClient(@Nonnull Throwable cause);
67
68     /**
69      * Override this method to handle any command which is not handled by the base behavior.
70      *
71      * @param command
72      * @return Next behavior to use, null if this actor should shut down.
73      */
74     protected abstract @Nullable ClientActorBehavior onCommand(@Nonnull Object command);
75 }