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