/*
 * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.controller.cluster.access.client;

import static java.util.Objects.requireNonNull;

import akka.persistence.RecoveryCompleted;
import akka.persistence.SnapshotOffer;
import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Transient behavior handling messages during initial actor recovery.
 *
 * @author Robert Varga
 */
final class RecoveringClientActorBehavior extends AbstractClientActorBehavior<InitialClientActorContext> {
    private static final Logger LOG = LoggerFactory.getLogger(RecoveringClientActorBehavior.class);
    private final FrontendIdentifier currentFrontend;
    private ClientIdentifier lastId = null;

    RecoveringClientActorBehavior(final InitialClientActorContext context, final FrontendIdentifier frontendId) {
        super(context);
        currentFrontend = requireNonNull(frontendId);
    }

    @Override
    AbstractClientActorBehavior<?> onReceiveCommand(final Object command) {
        throw new IllegalStateException("Frontend is recovering");
    }

    @Override
    AbstractClientActorBehavior<?> onReceiveRecover(final Object recover) {
        if (recover instanceof RecoveryCompleted) {
            final ClientIdentifier nextId;
            if (lastId != null) {
                if (!currentFrontend.equals(lastId.getFrontendId())) {
                    LOG.error("{}: Mismatched frontend identifier, shutting down. Current: {} Saved: {}",
                        persistenceId(), currentFrontend, lastId.getFrontendId());
                    return null;
                }

                nextId = ClientIdentifier.create(currentFrontend, lastId.getGeneration() + 1);
            } else {
                nextId = ClientIdentifier.create(currentFrontend, 0);
            }

            LOG.debug("{}: persisting new identifier {}", persistenceId(), nextId);
            context().saveSnapshot(nextId);
            return new SavingClientActorBehavior(context(), nextId);
        } else if (recover instanceof SnapshotOffer) {
            lastId = (ClientIdentifier) ((SnapshotOffer)recover).snapshot();
            LOG.debug("{}: recovered identifier {}", persistenceId(), lastId);
        } else {
            LOG.warn("{}: ignoring recovery message {}", persistenceId(), recover);
        }

        return this;
    }
}
@@ -1019,16 +1019,372 @@
 class);%0A
+%0A    /*%0A     * Base for the property name which overrides the initial generation when we fail to find anything from persistence.%0A     * The actual property name has the frontend type name appended.%0A     */%0A    private static final String GENERATION_OVERRIDE_PROP_BASE =%0A            %22org.opendaylight.controller.cluster.access.client.initial.generation.%22;%0A%0A
     priv
@@ -2550,17 +2550,35 @@
 ontend, 
-0
+initialGeneration()
 );%0A     
@@ -3134,9 +3134,793 @@
 ;%0A    %7D%0A
-%7D
+%0A    private long initialGeneration() %7B%0A        final String propName = GENERATION_OVERRIDE_PROP_BASE + currentFrontend.getClientType().getName();%0A        final String propValue = System.getProperty(propName);%0A        if (propValue == null) %7B%0A            LOG.debug(%22%7B%7D: no initial generation override, starting from 0%22, persistenceId());%0A            return 0;%0A        %7D%0A%0A        final long ret;%0A        try %7B%0A            ret = Long.parseUnsignedLong(propValue);%0A        %7D catch (NumberFormatException e) %7B%0A            LOG.warn(%22%7B%7D: failed to parse initial generation override '%7B%7D', starting from 0%22, persistenceId(),%0A                propValue, e);%0A            return 0;%0A        %7D%0A%0A        LOG.info(%22%7B%7D: initial generation set to %7B%7D%22, persistenceId(), ret);%0A        return ret;%0A    %7D%0A%7D%0A

Allow AbstractClientActor generation to start from non-zero