Fix CS warnings in cds-access-client and enable enforcement
[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(),
50                     ((DeleteSnapshotsFailure) command).cause());
51         } else {
52             LOG.debug("{}: stashing command {}", persistenceId(), command);
53             context().stash();
54             return this;
55         }
56
57         context().unstash();
58         return context().createBehavior(myId);
59     }
60 }