f8977344c3ab9190397964d03f25fe4fdbaaad71
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / actors / client / SavingClientActorBehavior.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 akka.persistence.SaveSnapshotFailure;
11 import akka.persistence.SaveSnapshotSuccess;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * @author Robert Varga
19  */
20 final class SavingClientActorBehavior extends RecoveredClientActorBehavior<InitialClientActorContext> {
21     private static final Logger LOG = LoggerFactory.getLogger(SavingClientActorBehavior.class);
22     private final ClientIdentifier myId;
23
24     SavingClientActorBehavior(final InitialClientActorContext context, final ClientIdentifier nextId) {
25         super(context);
26         this.myId = Preconditions.checkNotNull(nextId);
27     }
28
29     @Override
30     AbstractClientActorBehavior<?> onReceiveCommand(final Object command) {
31         if (command instanceof SaveSnapshotFailure) {
32             LOG.error("{}: failed to persist state", persistenceId(), ((SaveSnapshotFailure) command).cause());
33             return null;
34         } else if (command instanceof SaveSnapshotSuccess) {
35             context().unstash();
36             return context().createBehavior(myId);
37         } else {
38             LOG.debug("{}: stashing command {}", persistenceId(), command);
39             context().stash();
40             return this;
41         }
42     }
43 }