// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.deploymentmanager.implementation;

import com.azure.core.annotation.BodyParam;
import com.azure.core.annotation.Delete;
import com.azure.core.annotation.ExpectedResponses;
import com.azure.core.annotation.Get;
import com.azure.core.annotation.HeaderParam;
import com.azure.core.annotation.Headers;
import com.azure.core.annotation.Host;
import com.azure.core.annotation.HostParam;
import com.azure.core.annotation.PathParam;
import com.azure.core.annotation.Put;
import com.azure.core.annotation.QueryParam;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceInterface;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.annotation.UnexpectedResponseExceptionType;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.management.exception.ManagementException;
import com.azure.core.management.polling.PollResult;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.core.util.polling.PollerFlux;
import com.azure.core.util.polling.SyncPoller;
import com.azure.resourcemanager.deploymentmanager.fluent.ServiceUnitsClient;
import com.azure.resourcemanager.deploymentmanager.fluent.models.ServiceUnitResourceInner;
import java.nio.ByteBuffer;
import java.util.List;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/** An instance of this class provides access to all the operations defined in ServiceUnitsClient. */
public final class ServiceUnitsClientImpl implements ServiceUnitsClient {
    /** The proxy service used to perform REST calls. */
    private final ServiceUnitsService service;

    /** The service client containing this operation class. */
    private final AzureDeploymentManagerImpl client;

    /**
     * Initializes an instance of ServiceUnitsClientImpl.
     *
     * @param client the instance of the service client containing this operation class.
     */
    ServiceUnitsClientImpl(AzureDeploymentManagerImpl client) {
        this.service =
            RestProxy.create(ServiceUnitsService.class, client.getHttpPipeline(), client.getSerializerAdapter());
        this.client = client;
    }

    /**
     * The interface defining all the services for AzureDeploymentManagerServiceUnits to be used by the proxy service to
     * perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "AzureDeploymentManag")
    public interface ServiceUnitsService {
        @Headers({"Content-Type: application/json"})
        @Put(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DeploymentManager"
                + "/serviceTopologies/{serviceTopologyName}/services/{serviceName}/serviceUnits/{serviceUnitName}")
        @ExpectedResponses({201})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Flux<ByteBuffer>>> createOrUpdate(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("serviceTopologyName") String serviceTopologyName,
            @PathParam("serviceName") String serviceName,
            @PathParam("serviceUnitName") String serviceUnitName,
            @QueryParam("api-version") String apiVersion,
            @BodyParam("application/json") ServiceUnitResourceInner serviceUnitInfo,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DeploymentManager"
                + "/serviceTopologies/{serviceTopologyName}/services/{serviceName}/serviceUnits/{serviceUnitName}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<ServiceUnitResourceInner>> get(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("serviceTopologyName") String serviceTopologyName,
            @PathParam("serviceName") String serviceName,
            @PathParam("serviceUnitName") String serviceUnitName,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Delete(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DeploymentManager"
                + "/serviceTopologies/{serviceTopologyName}/services/{serviceName}/serviceUnits/{serviceUnitName}")
        @ExpectedResponses({200, 204})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Void>> delete(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("serviceTopologyName") String serviceTopologyName,
            @PathParam("serviceName") String serviceName,
            @PathParam("serviceUnitName") String serviceUnitName,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DeploymentManager"
                + "/serviceTopologies/{serviceTopologyName}/services/{serviceName}/serviceUnits")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<List<ServiceUnitResourceInner>>> list(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("serviceTopologyName") String serviceTopologyName,
            @PathParam("serviceName") String serviceName,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);
    }

    /**
     * Creates or updates a service unit under the service in the service topology.
     *
     * <p>This is an asynchronous operation and can be polled to completion using the operation resource returned by
     * this operation.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param serviceUnitInfo The service unit resource object.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return represents the response of a service unit resource along with {@link Response} on successful completion
     *     of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> createOrUpdateWithResponseAsync(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        ServiceUnitResourceInner serviceUnitInfo) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (serviceTopologyName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceTopologyName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName is required and cannot be null."));
        }
        if (serviceUnitName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceUnitName is required and cannot be null."));
        }
        if (serviceUnitInfo == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceUnitInfo is required and cannot be null."));
        } else {
            serviceUnitInfo.validate();
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .createOrUpdate(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            serviceTopologyName,
                            serviceName,
                            serviceUnitName,
                            this.client.getApiVersion(),
                            serviceUnitInfo,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Creates or updates a service unit under the service in the service topology.
     *
     * <p>This is an asynchronous operation and can be polled to completion using the operation resource returned by
     * this operation.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param serviceUnitInfo The service unit resource object.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return represents the response of a service unit resource along with {@link Response} on successful completion
     *     of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> createOrUpdateWithResponseAsync(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        ServiceUnitResourceInner serviceUnitInfo,
        Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (serviceTopologyName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceTopologyName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName is required and cannot be null."));
        }
        if (serviceUnitName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceUnitName is required and cannot be null."));
        }
        if (serviceUnitInfo == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceUnitInfo is required and cannot be null."));
        } else {
            serviceUnitInfo.validate();
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .createOrUpdate(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                serviceTopologyName,
                serviceName,
                serviceUnitName,
                this.client.getApiVersion(),
                serviceUnitInfo,
                accept,
                context);
    }

    /**
     * Creates or updates a service unit under the service in the service topology.
     *
     * <p>This is an asynchronous operation and can be polled to completion using the operation resource returned by
     * this operation.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param serviceUnitInfo The service unit resource object.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link PollerFlux} for polling of represents the response of a service unit resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<ServiceUnitResourceInner>, ServiceUnitResourceInner> beginCreateOrUpdateAsync(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        ServiceUnitResourceInner serviceUnitInfo) {
        Mono<Response<Flux<ByteBuffer>>> mono =
            createOrUpdateWithResponseAsync(
                resourceGroupName, serviceTopologyName, serviceName, serviceUnitName, serviceUnitInfo);
        return this
            .client
            .<ServiceUnitResourceInner, ServiceUnitResourceInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                ServiceUnitResourceInner.class,
                ServiceUnitResourceInner.class,
                this.client.getContext());
    }

    /**
     * Creates or updates a service unit under the service in the service topology.
     *
     * <p>This is an asynchronous operation and can be polled to completion using the operation resource returned by
     * this operation.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param serviceUnitInfo The service unit resource object.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link PollerFlux} for polling of represents the response of a service unit resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<ServiceUnitResourceInner>, ServiceUnitResourceInner> beginCreateOrUpdateAsync(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        ServiceUnitResourceInner serviceUnitInfo,
        Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono =
            createOrUpdateWithResponseAsync(
                resourceGroupName, serviceTopologyName, serviceName, serviceUnitName, serviceUnitInfo, context);
        return this
            .client
            .<ServiceUnitResourceInner, ServiceUnitResourceInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                ServiceUnitResourceInner.class,
                ServiceUnitResourceInner.class,
                context);
    }

    /**
     * Creates or updates a service unit under the service in the service topology.
     *
     * <p>This is an asynchronous operation and can be polled to completion using the operation resource returned by
     * this operation.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param serviceUnitInfo The service unit resource object.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link SyncPoller} for polling of represents the response of a service unit resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<ServiceUnitResourceInner>, ServiceUnitResourceInner> beginCreateOrUpdate(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        ServiceUnitResourceInner serviceUnitInfo) {
        return this
            .beginCreateOrUpdateAsync(
                resourceGroupName, serviceTopologyName, serviceName, serviceUnitName, serviceUnitInfo)
            .getSyncPoller();
    }

    /**
     * Creates or updates a service unit under the service in the service topology.
     *
     * <p>This is an asynchronous operation and can be polled to completion using the operation resource returned by
     * this operation.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param serviceUnitInfo The service unit resource object.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link SyncPoller} for polling of represents the response of a service unit resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<ServiceUnitResourceInner>, ServiceUnitResourceInner> beginCreateOrUpdate(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        ServiceUnitResourceInner serviceUnitInfo,
        Context context) {
        return this
            .beginCreateOrUpdateAsync(
                resourceGroupName, serviceTopologyName, serviceName, serviceUnitName, serviceUnitInfo, context)
            .getSyncPoller();
    }

    /**
     * Creates or updates a service unit under the service in the service topology.
     *
     * <p>This is an asynchronous operation and can be polled to completion using the operation resource returned by
     * this operation.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param serviceUnitInfo The service unit resource object.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return represents the response of a service unit resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ServiceUnitResourceInner> createOrUpdateAsync(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        ServiceUnitResourceInner serviceUnitInfo) {
        return beginCreateOrUpdateAsync(
                resourceGroupName, serviceTopologyName, serviceName, serviceUnitName, serviceUnitInfo)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Creates or updates a service unit under the service in the service topology.
     *
     * <p>This is an asynchronous operation and can be polled to completion using the operation resource returned by
     * this operation.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param serviceUnitInfo The service unit resource object.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return represents the response of a service unit resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ServiceUnitResourceInner> createOrUpdateAsync(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        ServiceUnitResourceInner serviceUnitInfo,
        Context context) {
        return beginCreateOrUpdateAsync(
                resourceGroupName, serviceTopologyName, serviceName, serviceUnitName, serviceUnitInfo, context)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Creates or updates a service unit under the service in the service topology.
     *
     * <p>This is an asynchronous operation and can be polled to completion using the operation resource returned by
     * this operation.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param serviceUnitInfo The service unit resource object.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return represents the response of a service unit resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ServiceUnitResourceInner createOrUpdate(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        ServiceUnitResourceInner serviceUnitInfo) {
        return createOrUpdateAsync(
                resourceGroupName, serviceTopologyName, serviceName, serviceUnitName, serviceUnitInfo)
            .block();
    }

    /**
     * Creates or updates a service unit under the service in the service topology.
     *
     * <p>This is an asynchronous operation and can be polled to completion using the operation resource returned by
     * this operation.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param serviceUnitInfo The service unit resource object.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return represents the response of a service unit resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ServiceUnitResourceInner createOrUpdate(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        ServiceUnitResourceInner serviceUnitInfo,
        Context context) {
        return createOrUpdateAsync(
                resourceGroupName, serviceTopologyName, serviceName, serviceUnitName, serviceUnitInfo, context)
            .block();
    }

    /**
     * Gets the service unit.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the service unit along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ServiceUnitResourceInner>> getWithResponseAsync(
        String resourceGroupName, String serviceTopologyName, String serviceName, String serviceUnitName) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (serviceTopologyName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceTopologyName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName is required and cannot be null."));
        }
        if (serviceUnitName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceUnitName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .get(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            serviceTopologyName,
                            serviceName,
                            serviceUnitName,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Gets the service unit.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the service unit along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ServiceUnitResourceInner>> getWithResponseAsync(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (serviceTopologyName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceTopologyName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName is required and cannot be null."));
        }
        if (serviceUnitName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceUnitName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .get(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                serviceTopologyName,
                serviceName,
                serviceUnitName,
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Gets the service unit.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the service unit on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ServiceUnitResourceInner> getAsync(
        String resourceGroupName, String serviceTopologyName, String serviceName, String serviceUnitName) {
        return getWithResponseAsync(resourceGroupName, serviceTopologyName, serviceName, serviceUnitName)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Gets the service unit.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the service unit along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<ServiceUnitResourceInner> getWithResponse(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        Context context) {
        return getWithResponseAsync(resourceGroupName, serviceTopologyName, serviceName, serviceUnitName, context)
            .block();
    }

    /**
     * Gets the service unit.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the service unit.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ServiceUnitResourceInner get(
        String resourceGroupName, String serviceTopologyName, String serviceName, String serviceUnitName) {
        return getWithResponse(resourceGroupName, serviceTopologyName, serviceName, serviceUnitName, Context.NONE)
            .getValue();
    }

    /**
     * Deletes the service unit.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Void>> deleteWithResponseAsync(
        String resourceGroupName, String serviceTopologyName, String serviceName, String serviceUnitName) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (serviceTopologyName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceTopologyName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName is required and cannot be null."));
        }
        if (serviceUnitName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceUnitName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .delete(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            serviceTopologyName,
                            serviceName,
                            serviceUnitName,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Deletes the service unit.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Void>> deleteWithResponseAsync(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (serviceTopologyName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceTopologyName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName is required and cannot be null."));
        }
        if (serviceUnitName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceUnitName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .delete(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                serviceTopologyName,
                serviceName,
                serviceUnitName,
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Deletes the service unit.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return A {@link Mono} that completes when a successful response is received.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Void> deleteAsync(
        String resourceGroupName, String serviceTopologyName, String serviceName, String serviceUnitName) {
        return deleteWithResponseAsync(resourceGroupName, serviceTopologyName, serviceName, serviceUnitName)
            .flatMap(ignored -> Mono.empty());
    }

    /**
     * Deletes the service unit.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<Void> deleteWithResponse(
        String resourceGroupName,
        String serviceTopologyName,
        String serviceName,
        String serviceUnitName,
        Context context) {
        return deleteWithResponseAsync(resourceGroupName, serviceTopologyName, serviceName, serviceUnitName, context)
            .block();
    }

    /**
     * Deletes the service unit.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param serviceUnitName The name of the service unit resource.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public void delete(
        String resourceGroupName, String serviceTopologyName, String serviceName, String serviceUnitName) {
        deleteWithResponse(resourceGroupName, serviceTopologyName, serviceName, serviceUnitName, Context.NONE);
    }

    /**
     * Lists the service units under a service in the service topology.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the list of service units along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<List<ServiceUnitResourceInner>>> listWithResponseAsync(
        String resourceGroupName, String serviceTopologyName, String serviceName) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (serviceTopologyName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceTopologyName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .list(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            serviceTopologyName,
                            serviceName,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Lists the service units under a service in the service topology.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the list of service units along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<List<ServiceUnitResourceInner>>> listWithResponseAsync(
        String resourceGroupName, String serviceTopologyName, String serviceName, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (serviceTopologyName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter serviceTopologyName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .list(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                serviceTopologyName,
                serviceName,
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Lists the service units under a service in the service topology.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the list of service units on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<List<ServiceUnitResourceInner>> listAsync(
        String resourceGroupName, String serviceTopologyName, String serviceName) {
        return listWithResponseAsync(resourceGroupName, serviceTopologyName, serviceName)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Lists the service units under a service in the service topology.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the list of service units along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<List<ServiceUnitResourceInner>> listWithResponse(
        String resourceGroupName, String serviceTopologyName, String serviceName, Context context) {
        return listWithResponseAsync(resourceGroupName, serviceTopologyName, serviceName, context).block();
    }

    /**
     * Lists the service units under a service in the service topology.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceTopologyName The name of the service topology .
     * @param serviceName The name of the service resource.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the list of service units.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public List<ServiceUnitResourceInner> list(
        String resourceGroupName, String serviceTopologyName, String serviceName) {
        return listWithResponse(resourceGroupName, serviceTopologyName, serviceName, Context.NONE).getValue();
    }
}
