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