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