5fc3ef38841d51c811ea792d55677a145590b1b0
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / 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.access.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  * Transient behavior handling messages while the new generation is being persisted.
22  *
23  * @author Robert Varga
24  */
25 final class SavingClientActorBehavior extends RecoveredClientActorBehavior<InitialClientActorContext> {
26     private static final Logger LOG = LoggerFactory.getLogger(SavingClientActorBehavior.class);
27     private final ClientIdentifier myId;
28
29     SavingClientActorBehavior(final InitialClientActorContext context, final ClientIdentifier nextId) {
30         super(context);
31         this.myId = Preconditions.checkNotNull(nextId);
32     }
33
34     @Override
35     AbstractClientActorBehavior<?> onReceiveCommand(final Object command) {
36         if (command instanceof SaveSnapshotFailure) {
37             LOG.error("{}: failed to persist state", persistenceId(), ((SaveSnapshotFailure) command).cause());
38             return null;
39         } else if (command instanceof SaveSnapshotSuccess) {
40             LOG.debug("{}: got command: {}", persistenceId(), command);
41             SaveSnapshotSuccess saved = (SaveSnapshotSuccess)command;
42             context().deleteSnapshots(new SnapshotSelectionCriteria(saved.metadata().sequenceNr(),
43                     saved.metadata().timestamp() - 1, 0L, 0L));
44             return this;
45         } else if (command instanceof DeleteSnapshotsSuccess) {
46             LOG.debug("{}: got command: {}", persistenceId(), command);
47         } else if (command instanceof DeleteSnapshotsFailure) {
48             // Not treating this as a fatal error.
49             LOG.warn("{}: failed to delete prior snapshots", persistenceId(), ((DeleteSnapshotsFailure) command).cause());
50         } else {
51             LOG.debug("{}: stashing command {}", persistenceId(), command);
52             context().stash();
53             return this;
54         }
55
56         context().unstash();
57         return context().createBehavior(myId);
58     }
59 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.