/*
 * Copyright (c) 2015 Brocade Communications 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.raft;

import akka.actor.ActorRef;
import akka.persistence.SaveSnapshotFailure;
import akka.persistence.SaveSnapshotSuccess;
import akka.util.Timeout;
import com.google.common.annotations.VisibleForTesting;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot;
import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot;
import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
import org.opendaylight.controller.cluster.raft.persisted.EmptyState;
import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
import org.slf4j.Logger;
import scala.concurrent.duration.FiniteDuration;

/**
 * Handles snapshot related messages for a RaftActor.
 *
 * @author Thomas Pantelis
 */
class RaftActorSnapshotMessageSupport {
    static final Object COMMIT_SNAPSHOT = new Object() {
        @Override
        public String toString() {
            return "commit_snapshot";
        }
    };

    private final RaftActorContext context;
    private final RaftActorSnapshotCohort cohort;
    private final Logger log;

    private FiniteDuration snapshotReplyActorTimeout = FiniteDuration.create(30, TimeUnit.SECONDS);

    RaftActorSnapshotMessageSupport(final RaftActorContext context, final RaftActorSnapshotCohort cohort) {
        this.context = context;
        this.cohort = cohort;
        this.log = context.getLogger();

        context.getSnapshotManager().setCreateSnapshotConsumer(
            outputStream -> cohort.createSnapshot(context.getActor(), outputStream));
        context.getSnapshotManager().setSnapshotCohort(cohort);
    }

    RaftActorSnapshotCohort getSnapshotCohort() {
        return cohort;
    }

    boolean handleSnapshotMessage(Object message, ActorRef sender) {
        if (message instanceof ApplySnapshot) {
            onApplySnapshot((ApplySnapshot) message);
        } else if (message instanceof SaveSnapshotSuccess) {
            onSaveSnapshotSuccess((SaveSnapshotSuccess) message);
        } else if (message instanceof SaveSnapshotFailure) {
            onSaveSnapshotFailure((SaveSnapshotFailure) message);
        } else if (message instanceof CaptureSnapshotReply) {
            onCaptureSnapshotReply((CaptureSnapshotReply) message);
        } else if (COMMIT_SNAPSHOT.equals(message)) {
            context.getSnapshotManager().commit(-1, -1);
        } else if (message instanceof GetSnapshot) {
            onGetSnapshot(sender, (GetSnapshot) message);
        } else {
            return false;
        }

        return true;
    }

    private void onCaptureSnapshotReply(CaptureSnapshotReply reply) {
        log.debug("{}: CaptureSnapshotReply received by actor", context.getId());

        context.getSnapshotManager().persist(reply.getSnapshotState(), reply.getInstallSnapshotStream(),
                context.getTotalMemory());
    }

    private void onSaveSnapshotFailure(SaveSnapshotFailure saveSnapshotFailure) {
        log.error("{}: SaveSnapshotFailure received for snapshot Cause:",
                context.getId(), saveSnapshotFailure.cause());

        context.getSnapshotManager().rollback();
    }

    private void onSaveSnapshotSuccess(SaveSnapshotSuccess success) {
        long sequenceNumber = success.metadata().sequenceNr();

        log.info("{}: SaveSnapshotSuccess received for snapshot, sequenceNr: {}", context.getId(), sequenceNumber);

        context.getSnapshotManager().commit(sequenceNumber, success.metadata().timestamp());
    }

    private void onApplySnapshot(ApplySnapshot message) {
        log.info("{}: Applying snapshot on follower:  {}", context.getId(), message.getSnapshot());

        context.getSnapshotManager().apply(message);
    }

    private void onGetSnapshot(ActorRef sender, GetSnapshot getSnapshot) {
        log.debug("{}: onGetSnapshot", context.getId());


        if (context.getPersistenceProvider().isRecoveryApplicable()) {
            CaptureSnapshot captureSnapshot = context.getSnapshotManager().newCaptureSnapshot(
                    context.getReplicatedLog().last(), -1, true);

            final FiniteDuration timeout =
                    getSnapshot.getTimeout().map(Timeout::duration).orElse(snapshotReplyActorTimeout);

            ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot,
                    ImmutableElectionTerm.copyOf(context.getTermInformation()), sender, timeout, context.getId(),
                    context.getPeerServerInfo(true)));

            cohort.createSnapshot(snapshotReplyActor, Optional.empty());
        } else {
            Snapshot snapshot = Snapshot.create(
                    EmptyState.INSTANCE, Collections.<ReplicatedLogEntry>emptyList(),
                    -1, -1, -1, -1,
                    context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(),
                    context.getPeerServerInfo(true));

            sender.tell(new GetSnapshotReply(context.getId(), snapshot), context.getActor());
        }
    }

    @VisibleForTesting
    void setSnapshotReplyActorTimeout(FiniteDuration snapshotReplyActorTimeout) {
        this.snapshotReplyActorTimeout = snapshotReplyActorTimeout;
    }
}

@@ -896,32 +896,112 @@
 eSnapshotReply;%0A
+import org.opendaylight.controller.cluster.raft.base.messages.SnapshotComplete;%0A
 import org.opend
@@ -1053,32 +1053,32 @@
 es.GetSnapshot;%0A
-
 import org.opend
@@ -2058,21 +2058,16 @@
         
-this.
 log = co
@@ -2421,16 +2421,22 @@
 Message(
+final 
 Object m
@@ -2442,16 +2442,22 @@
 message,
+ final
  ActorRe
@@ -3168,32 +3168,163 @@
 shot) message);%0A
+        %7D else if (message instanceof SnapshotComplete) %7B%0A            log.debug(%22%7B%7D: SnapshotComplete received%22, context.getId());%0A
         %7D else %7B
@@ -3429,16 +3429,22 @@
 otReply(
+final 
 CaptureS
@@ -3742,16 +3742,22 @@
 Failure(
+final 
 SaveSnap
@@ -4024,16 +4024,22 @@
 Success(
+final 
 SaveSnap
@@ -4375,16 +4375,22 @@
 napshot(
+final 
 ApplySna
@@ -4598,16 +4598,22 @@
 napshot(
+final 
 ActorRef
@@ -4620,16 +4620,22 @@
  sender,
+ final
  GetSnap
@@ -5927,16 +5927,16 @@
 Testing%0A
-
     void
@@ -5965,16 +5965,22 @@
 Timeout(
+final 
 FiniteDu

Handle SnapshotComplete message