package com.sequenceiq.cloudbreak.core.flow2.cluster.start;
import java.util.Map;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.action.Action;
import com.sequenceiq.cloudbreak.cloud.event.Selectable;
import com.sequenceiq.cloudbreak.core.flow2.cluster.AbstractClusterAction;
import com.sequenceiq.cloudbreak.core.flow2.cluster.ClusterContext;
import com.sequenceiq.cloudbreak.core.flow2.stack.AbstractStackFailureAction;
import com.sequenceiq.cloudbreak.core.flow2.stack.StackFailureContext;
import com.sequenceiq.cloudbreak.reactor.api.event.StackEvent;
import com.sequenceiq.cloudbreak.reactor.api.event.StackFailureEvent;
import com.sequenceiq.cloudbreak.reactor.api.event.cluster.ClusterStartPollingRequest;
import com.sequenceiq.cloudbreak.reactor.api.event.cluster.ClusterStartPollingResult;
import com.sequenceiq.cloudbreak.reactor.api.event.cluster.ClusterStartRequest;
import com.sequenceiq.cloudbreak.reactor.api.event.cluster.ClusterStartResult;
import com.sequenceiq.cloudbreak.service.cluster.ClusterService;
@Configuration
public class ClusterStartActions {
private static final Logger LOGGER = LoggerFactory.getLogger(ClusterStartActions.class);
@Inject
private ClusterService clusterService;
@Inject
private ClusterStartService clusterStartService;
@Bean(name = "CLUSTER_STARTING_STATE")
public Action startingCluster() {
return new AbstractClusterAction<StackEvent>(StackEvent.class) {
@Override
protected void doExecute(ClusterContext context, StackEvent payload, Map<Object, Object> variables) throws Exception {
clusterStartService.startingCluster(context.getStack(), context.getCluster());
sendEvent(context);
}
@Override
protected Selectable createRequest(ClusterContext context) {
return new ClusterStartRequest(context.getStack().getId());
}
};
}
@Bean(name = "CLUSTER_START_POLLING_STATE")
public Action clusterStartPolling() {
return new AbstractClusterAction<ClusterStartResult>(ClusterStartResult.class) {
@Override
protected void doExecute(ClusterContext context, ClusterStartResult payload, Map<Object, Object> variables) throws Exception {
sendEvent(context.getFlowId(), new ClusterStartPollingRequest(context.getStack().getId(), payload.getRequestId()));
}
};
}
@Bean(name = "CLUSTER_START_FINISHED_STATE")
public Action clusterStartFinished() {
return new AbstractClusterAction<ClusterStartPollingResult>(ClusterStartPollingResult.class) {
@Override
protected void doExecute(ClusterContext context, ClusterStartPollingResult payload, Map<Object, Object> variables) throws Exception {
clusterStartService.clusterStartFinished(context.getStack());
sendEvent(context);
}
@Override
protected Selectable createRequest(ClusterContext context) {
return new StackEvent(ClusterStartEvent.FINALIZED_EVENT.event(), context.getStack().getId());
}
};
}
@Bean(name = "CLUSTER_START_FAILED_STATE")
public Action clusterStartFailedAction() {
return new AbstractStackFailureAction<ClusterStartState, ClusterStartEvent>() {
@Override
protected void doExecute(StackFailureContext context, StackFailureEvent payload, Map<Object, Object> variables) throws Exception {
clusterStartService.handleClusterStartFailure(context.getStack(), payload.getException().getMessage());
sendEvent(context);
}
@Override
protected Selectable createRequest(StackFailureContext context) {
return new StackEvent(ClusterStartEvent.FAIL_HANDLED_EVENT.event(), context.getStack().getId());
}
};
}
}