All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cloudfoundry.multiapps.controller.process.steps.CollectServicesInProgressStateStep Maven / Gradle / Ivy

There is a newer version: 1.183.0
Show newest version
package org.cloudfoundry.multiapps.controller.process.steps;

import com.sap.cloudfoundry.client.facade.CloudControllerClient;
import com.sap.cloudfoundry.client.facade.domain.CloudEntity;
import com.sap.cloudfoundry.client.facade.domain.CloudServiceInstance;
import com.sap.cloudfoundry.client.facade.domain.ServiceOperation;
import org.cloudfoundry.multiapps.common.util.JsonUtil;
import org.cloudfoundry.multiapps.controller.client.lib.domain.CloudServiceInstanceExtended;
import org.cloudfoundry.multiapps.controller.client.lib.domain.ImmutableCloudServiceInstanceExtended;
import org.cloudfoundry.multiapps.controller.process.Messages;
import org.cloudfoundry.multiapps.controller.process.util.ServiceOperationGetter;
import org.cloudfoundry.multiapps.controller.process.util.ServiceProgressReporter;
import org.cloudfoundry.multiapps.controller.process.variables.Variables;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public abstract class CollectServicesInProgressStateStep extends TimeoutAsyncFlowableStep {

    protected ServiceOperationGetter serviceOperationGetter;
    protected ServiceProgressReporter serviceProgressReporter;

    CollectServicesInProgressStateStep(ServiceOperationGetter serviceOperationGetter, ServiceProgressReporter serviceProgressReporter) {
        this.serviceOperationGetter = serviceOperationGetter;
        this.serviceProgressReporter = serviceProgressReporter;
    }

    @Override
    protected StepPhase executeAsyncStep(ProcessContext context) {
        List existingServicesInProgress = getExistingServicesInProgress(context);
        if (existingServicesInProgress.isEmpty()) {
            return StepPhase.DONE;
        }

        getStepLogger().info(Messages.WAITING_PREVIOUS_OPERATIONS_TO_FINISH);

        Map servicesOperationTypes = getServicesOperationTypes(existingServicesInProgress);
        getStepLogger().debug(Messages.SERVICES_IN_PROGRESS, JsonUtil.toJson(servicesOperationTypes, true));
        context.setVariable(Variables.TRIGGERED_SERVICE_OPERATIONS, servicesOperationTypes);

        context.setVariable(Variables.SERVICES_DATA, existingServicesInProgress);

        return StepPhase.POLL;
    }

    protected abstract List getExistingServicesInProgress(ProcessContext context);

    protected CloudServiceInstanceExtended getExistingService(CloudControllerClient client, CloudServiceInstanceExtended service) {
        CloudServiceInstance existingService = client.getServiceInstance(service.getName(), false);
        if (existingService != null) {
            return ImmutableCloudServiceInstanceExtended.builder()
                                                        .from(service)
                                                        .from(existingService)
                                                        .metadata(existingService.getMetadata())
                                                        .build();
        }
        return null;
    }

    protected boolean isServiceOperationInProgress(CloudServiceInstanceExtended service) {
        ServiceOperation lastServiceOperation = service.getLastOperation();
        return lastServiceOperation != null && lastServiceOperation.getState() == ServiceOperation.State.IN_PROGRESS;
    }

    private Map getServicesOperationTypes(List servicesInProgressState) {
        return servicesInProgressState.stream()
                                      .collect(Collectors.toMap(CloudEntity::getName, service -> service.getLastOperation()
                                                                                                        .getType()));
    }

    @Override
    protected List getAsyncStepExecutions(ProcessContext context) {
        return Collections.singletonList(new PollServiceInProgressOperationsExecution(serviceOperationGetter, serviceProgressReporter));
    }

    @Override
    protected String getStepErrorMessage(ProcessContext context) {
        return Messages.ERROR_MONITORING_OPERATIONS_OVER_SERVICES;
    }

    @Override
    public Duration getTimeout(ProcessContext context) {
        return context.getVariable(Variables.SERVICE_IN_PROGRESS_TIMEOUT);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy