abccc273cd8842d66b8a2e7d66d028e211f8fa83
[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.DeleteSnapshotsFailure;
11 import akka.persistence.DeleteSnapshotsSuccess;
12 import akka.persistence.SaveSnapshotFailure;
13 import akka.persistence.SaveSnapshotSuccess;
14 import akka.persistence.SnapshotSelectionCriteria;
15 import com.google.common.base.Preconditions;
16 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * @author Robert Varga
22  */
23 final class SavingClientActorBehavior extends RecoveredClientActorBehavior<InitialClientActorContext> {
24     private static final Logger LOG = LoggerFactory.getLogger(SavingClientActorBehavior.class);
25     private final ClientIdentifier myId;
26
27     SavingClientActorBehavior(final InitialClientActorContext context, final ClientIdentifier nextId) {
28         super(context);
29         this.myId = Preconditions.checkNotNull(nextId);
30     }
31
32     @Override
33     AbstractClientActorBehavior<?> onReceiveCommand(final Object command) {
34         if (command instanceof SaveSnapshotFailure) {
35             LOG.error("{}: failed to persist state", persistenceId(), ((SaveSnapshotFailure) command).cause());
36             return null;
37         } else if (command instanceof SaveSnapshotSuccess) {
38             LOG.debug("{}: got command: {}", persistenceId(), command);
39             SaveSnapshotSuccess saved = (SaveSnapshotSuccess)command;
40             context().deleteSnapshots(new SnapshotSelectionCriteria(saved.metadata().sequenceNr(),
41                     saved.metadata().timestamp() - 1, 0L, 0L));
42             return this;
43         } else if (command instanceof DeleteSnapshotsSuccess) {
44             LOG.debug("{}: got command: {}", persistenceId(), command);
45         } else if (command instanceof DeleteSnapshotsFailure) {
46             // Not treating this as a fatal error.
47             LOG.warn("{}: failed to delete prior snapshots", persistenceId(), ((DeleteSnapshotsFailure) command).cause());
48         } else {
49             LOG.debug("{}: stashing command {}", persistenceId(), command);
50             context().stash();
51             return this;
52         }
53
54         context().unstash();
55         return context().createBehavior(myId);
56     }
57 }