/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.runtime.executiongraph.utils;

import org.apache.flink.api.common.JobID;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.runtime.checkpoint.CheckpointOptions;
import org.apache.flink.runtime.clusterframework.types.AllocationID;
import org.apache.flink.runtime.concurrent.FutureUtils;
import org.apache.flink.runtime.deployment.TaskDeploymentDescriptor;
import org.apache.flink.runtime.executiongraph.ExecutionAttemptID;
import org.apache.flink.runtime.executiongraph.PartitionInfo;
import org.apache.flink.runtime.jobmanager.slots.TaskManagerGateway;
import org.apache.flink.runtime.messages.Acknowledge;
import org.apache.flink.runtime.messages.StackTraceSampleResponse;

import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Consumer;

/**
 * A TaskManagerGateway that simply acks the basic operations (deploy, cancel, update) and does not
 * support any more advanced operations.
 */
public class SimpleAckingTaskManagerGateway implements TaskManagerGateway {

	private final String address = UUID.randomUUID().toString();

	private Optional<Consumer<ExecutionAttemptID>> optSubmitConsumer;

	private Optional<Consumer<ExecutionAttemptID>> optCancelConsumer;

	private volatile BiFunction<AllocationID, Throwable, CompletableFuture<Acknowledge>> freeSlotFunction;

	public SimpleAckingTaskManagerGateway() {
		optSubmitConsumer = Optional.empty();
		optCancelConsumer = Optional.empty();
	}

	public void setSubmitConsumer(Consumer<ExecutionAttemptID> predicate) {
		optSubmitConsumer = Optional.of(predicate);
	}

	public void setCancelConsumer(Consumer<ExecutionAttemptID> predicate) {
		optCancelConsumer = Optional.of(predicate);
	}

	public void setFreeSlotFunction(BiFunction<AllocationID, Throwable, CompletableFuture<Acknowledge>> freeSlotFunction) {
		this.freeSlotFunction = freeSlotFunction;
	}

	@Override
	public String getAddress() {
		return address;
	}

	@Override
	public CompletableFuture<StackTraceSampleResponse> requestStackTraceSample(
			ExecutionAttemptID executionAttemptID,
			int sampleId,
			int numSamples,
			Time delayBetweenSamples,
			int maxStackTraceDepth,
			Time timeout) {
		return FutureUtils.completedExceptionally(new UnsupportedOperationException());
	}

	@Override
	public CompletableFuture<Acknowledge> submitTask(TaskDeploymentDescriptor tdd, Time timeout) {
		optSubmitConsumer.ifPresent(condition -> condition.accept(tdd.getExecutionAttemptId()));
		return CompletableFuture.completedFuture(Acknowledge.get());
	}

	@Override
	public CompletableFuture<Acknowledge> stopTask(ExecutionAttemptID executionAttemptID, Time timeout) {
		return CompletableFuture.completedFuture(Acknowledge.get());
	}

	@Override
	public CompletableFuture<Acknowledge> cancelTask(ExecutionAttemptID executionAttemptID, Time timeout) {
		optCancelConsumer.ifPresent(condition -> condition.accept(executionAttemptID));
		return CompletableFuture.completedFuture(Acknowledge.get());
	}

	@Override
	public CompletableFuture<Acknowledge> updatePartitions(ExecutionAttemptID executionAttemptID, Iterable<PartitionInfo> partitionInfos, Time timeout) {
		return CompletableFuture.completedFuture(Acknowledge.get());
	}

	@Override
	public void failPartition(ExecutionAttemptID executionAttemptID) {}

	@Override
	public void notifyCheckpointComplete(
			ExecutionAttemptID executionAttemptID,
			JobID jobId,
			long checkpointId,
			long timestamp) {}

	@Override
	public void triggerCheckpoint(
			ExecutionAttemptID executionAttemptID,
			JobID jobId,
			long checkpointId,
			long timestamp,
			CheckpointOptions checkpointOptions) {}

	@Override
	public CompletableFuture<Acknowledge> freeSlot(AllocationID allocationId, Throwable cause, Time timeout) {
		final BiFunction<AllocationID, Throwable, CompletableFuture<Acknowledge>> currentFreeSlotFunction = freeSlotFunction;

		if (currentFreeSlotFunction != null) {
			return currentFreeSlotFunction.apply(allocationId, cause);
		} else {
			return CompletableFuture.completedFuture(Acknowledge.get());
		}
	}
}

@@ -1524,35 +1524,8 @@
 e;%0A%0A
-import java.util.Optional;%0A
 impo
@@ -1967,64 +1967,73 @@
 ate 
-Optional%3CConsumer%3CExecutionAttemptID%3E%3E optSubmitConsumer
+Consumer%3CTaskDeploymentDescriptor%3E submitConsumer = ignore -%3E %7B %7D
 ;%0A%0A%09
@@ -2044,17 +2044,8 @@
 ate 
-Optional%3C
 Cons
@@ -2072,14 +2072,10 @@
 tID%3E
-%3E optC
+ c
 ance
@@ -2083,16 +2083,32 @@
 Consumer
+ = ignore -%3E %7B %7D
 ;%0A%0A%09priv
@@ -2219,185 +2219,64 @@
 lic 
-SimpleAckingTaskManagerGateway() %7B%0A%09%09optSubmitConsumer = Optional.empty();%0A%09%09optCancelConsumer = Optional.empty();%0A%09%7D%0A%0A%09public void setSubmitConsumer(Consumer%3CExecutionAttemptID
+void setSubmitConsumer(Consumer%3CTaskDeploymentDescriptor
 %3E pr
@@ -2284,28 +2284,25 @@
 dicate) %7B%0A%09%09
-optS
+s
 ubmitConsume
@@ -2301,36 +2301,24 @@
 tConsumer = 
-Optional.of(
 predicate);%0A
@@ -2306,33 +2306,32 @@
 umer = predicate
-)
 ;%0A%09%7D%0A%0A%09public vo
@@ -2391,28 +2391,25 @@
 dicate) %7B%0A%09%09
-optC
+c
 ancelConsume
@@ -2416,20 +2416,8 @@
 r = 
-Optional.of(
 pred
@@ -2421,17 +2421,16 @@
 redicate
-)
 ;%0A%09%7D%0A%0A%09p
@@ -3099,12 +3099,9 @@
 %7B%0A%09%09
-optS
+s
 ubmi
@@ -3114,76 +3114,18 @@
 mer.
-ifPresent(condition -%3E condition.accept(tdd.getExecutionAttemptId())
+accept(tdd
 );%0A%09
@@ -3493,12 +3493,9 @@
 %7B%0A%09%09
-optC
+c
 ance
@@ -3508,41 +3508,8 @@
 mer.
-ifPresent(condition -%3E condition.
 acce
@@ -3530,17 +3530,16 @@
 temptID)
-)
 ;%0A%09%09retu

Refactor SimpleAckingTaskManagerGateway to not use optional