Fix modernization issues
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / RecoveringClientActorBehavior.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.RecoveryCompleted;
13 import akka.persistence.SnapshotOffer;
14 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
15 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Transient behavior handling messages during initial actor recovery.
21  *
22  * @author Robert Varga
23  */
24 final class RecoveringClientActorBehavior extends AbstractClientActorBehavior<InitialClientActorContext> {
25     private static final Logger LOG = LoggerFactory.getLogger(RecoveringClientActorBehavior.class);
26     private final FrontendIdentifier currentFrontend;
27     private ClientIdentifier lastId = null;
28
29     RecoveringClientActorBehavior(final InitialClientActorContext context, final FrontendIdentifier frontendId) {
30         super(context);
31         currentFrontend = requireNonNull(frontendId);
32     }
33
34     @Override
35     AbstractClientActorBehavior<?> onReceiveCommand(final Object command) {
36         throw new IllegalStateException("Frontend is recovering");
37     }
38
39     @Override
40     AbstractClientActorBehavior<?> onReceiveRecover(final Object recover) {
41         if (recover instanceof RecoveryCompleted) {
42             final ClientIdentifier nextId;
43             if (lastId != null) {
44                 if (!currentFrontend.equals(lastId.getFrontendId())) {
45                     LOG.error("{}: Mismatched frontend identifier, shutting down. Current: {} Saved: {}",
46                         persistenceId(), currentFrontend, lastId.getFrontendId());
47                     return null;
48                 }
49
50                 nextId = ClientIdentifier.create(currentFrontend, lastId.getGeneration() + 1);
51             } else {
52                 nextId = ClientIdentifier.create(currentFrontend, 0);
53             }
54
55             LOG.debug("{}: persisting new identifier {}", persistenceId(), nextId);
56             context().saveSnapshot(nextId);
57             return new SavingClientActorBehavior(context(), nextId);
58         } else if (recover instanceof SnapshotOffer) {
59             lastId = (ClientIdentifier) ((SnapshotOffer)recover).snapshot();
60             LOG.debug("{}: recovered identifier {}", persistenceId(), lastId);
61         } else {
62             LOG.warn("{}: ignoring recovery message {}", persistenceId(), recover);
63         }
64
65         return this;
66     }
67 }