/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 *
 * Code generated by Microsoft (R) AutoRest Code Generator.
 */

package com.microsoft.azure.management.customerinsights.implementation;

import retrofit2.Retrofit;
import com.google.common.reflect.TypeToken;
import com.microsoft.azure.AzureServiceFuture;
import com.microsoft.azure.CloudException;
import com.microsoft.azure.ListOperationCallback;
import com.microsoft.azure.Page;
import com.microsoft.azure.PagedList;
import com.microsoft.rest.ServiceCallback;
import com.microsoft.rest.ServiceFuture;
import com.microsoft.rest.ServiceResponse;
import com.microsoft.rest.Validator;
import java.io.IOException;
import java.util.List;
import okhttp3.ResponseBody;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.Path;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Query;
import retrofit2.http.Url;
import retrofit2.Response;
import rx.functions.Func1;
import rx.Observable;

/**
 * An instance of this class provides access to all the operations defined
 * in AuthorizationPolicies.
 */
public class AuthorizationPoliciesInner {
    /** The Retrofit service to perform REST calls. */
    private AuthorizationPoliciesService service;
    /** The service client containing this operation class. */
    private CustomerInsightsManagementClientImpl client;

    /**
     * Initializes an instance of AuthorizationPoliciesInner.
     *
     * @param retrofit the Retrofit instance built from a Retrofit Builder.
     * @param client the instance of the service client containing this operation class.
     */
    public AuthorizationPoliciesInner(Retrofit retrofit, CustomerInsightsManagementClientImpl client) {
        this.service = retrofit.create(AuthorizationPoliciesService.class);
        this.client = client;
    }

    /**
     * The interface defining all the services for AuthorizationPolicies to be
     * used by Retrofit to perform actually REST calls.
     */
    interface AuthorizationPoliciesService {
        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.customerinsights.AuthorizationPolicies createOrUpdate" })
        @PUT("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CustomerInsights/hubs/{hubName}/authorizationPolicies/{authorizationPolicyName}")
        Observable<Response<ResponseBody>> createOrUpdate(@Path("resourceGroupName") String resourceGroupName, @Path("hubName") String hubName, @Path("authorizationPolicyName") String authorizationPolicyName, @Path("subscriptionId") String subscriptionId, @Body AuthorizationPolicyResourceFormatInner parameters, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.customerinsights.AuthorizationPolicies get" })
        @GET("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CustomerInsights/hubs/{hubName}/authorizationPolicies/{authorizationPolicyName}")
        Observable<Response<ResponseBody>> get(@Path("resourceGroupName") String resourceGroupName, @Path("hubName") String hubName, @Path("authorizationPolicyName") String authorizationPolicyName, @Path("subscriptionId") String subscriptionId, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.customerinsights.AuthorizationPolicies listByHub" })
        @GET("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CustomerInsights/hubs/{hubName}/authorizationPolicies")
        Observable<Response<ResponseBody>> listByHub(@Path("resourceGroupName") String resourceGroupName, @Path("hubName") String hubName, @Path("subscriptionId") String subscriptionId, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.customerinsights.AuthorizationPolicies regeneratePrimaryKey" })
        @POST("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CustomerInsights/hubs/{hubName}/authorizationPolicies/{authorizationPolicyName}/regeneratePrimaryKey")
        Observable<Response<ResponseBody>> regeneratePrimaryKey(@Path("resourceGroupName") String resourceGroupName, @Path("hubName") String hubName, @Path("authorizationPolicyName") String authorizationPolicyName, @Path("subscriptionId") String subscriptionId, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.customerinsights.AuthorizationPolicies regenerateSecondaryKey" })
        @POST("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CustomerInsights/hubs/{hubName}/authorizationPolicies/{authorizationPolicyName}/regenerateSecondaryKey")
        Observable<Response<ResponseBody>> regenerateSecondaryKey(@Path("resourceGroupName") String resourceGroupName, @Path("hubName") String hubName, @Path("authorizationPolicyName") String authorizationPolicyName, @Path("subscriptionId") String subscriptionId, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.customerinsights.AuthorizationPolicies listByHubNext" })
        @GET
        Observable<Response<ResponseBody>> listByHubNext(@Url String nextUrl, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

    }

    /**
     * Creates an authorization policy or updates an existing authorization policy.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @param parameters Parameters supplied to the CreateOrUpdate authorization policy operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws CloudException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the AuthorizationPolicyResourceFormatInner object if successful.
     */
    public AuthorizationPolicyResourceFormatInner createOrUpdate(String resourceGroupName, String hubName, String authorizationPolicyName, AuthorizationPolicyResourceFormatInner parameters) {
        return createOrUpdateWithServiceResponseAsync(resourceGroupName, hubName, authorizationPolicyName, parameters).toBlocking().single().body();
    }

    /**
     * Creates an authorization policy or updates an existing authorization policy.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @param parameters Parameters supplied to the CreateOrUpdate authorization policy operation.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<AuthorizationPolicyResourceFormatInner> createOrUpdateAsync(String resourceGroupName, String hubName, String authorizationPolicyName, AuthorizationPolicyResourceFormatInner parameters, final ServiceCallback<AuthorizationPolicyResourceFormatInner> serviceCallback) {
        return ServiceFuture.fromResponse(createOrUpdateWithServiceResponseAsync(resourceGroupName, hubName, authorizationPolicyName, parameters), serviceCallback);
    }

    /**
     * Creates an authorization policy or updates an existing authorization policy.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @param parameters Parameters supplied to the CreateOrUpdate authorization policy operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the AuthorizationPolicyResourceFormatInner object
     */
    public Observable<AuthorizationPolicyResourceFormatInner> createOrUpdateAsync(String resourceGroupName, String hubName, String authorizationPolicyName, AuthorizationPolicyResourceFormatInner parameters) {
        return createOrUpdateWithServiceResponseAsync(resourceGroupName, hubName, authorizationPolicyName, parameters).map(new Func1<ServiceResponse<AuthorizationPolicyResourceFormatInner>, AuthorizationPolicyResourceFormatInner>() {
            @Override
            public AuthorizationPolicyResourceFormatInner call(ServiceResponse<AuthorizationPolicyResourceFormatInner> response) {
                return response.body();
            }
        });
    }

    /**
     * Creates an authorization policy or updates an existing authorization policy.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @param parameters Parameters supplied to the CreateOrUpdate authorization policy operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the AuthorizationPolicyResourceFormatInner object
     */
    public Observable<ServiceResponse<AuthorizationPolicyResourceFormatInner>> createOrUpdateWithServiceResponseAsync(String resourceGroupName, String hubName, String authorizationPolicyName, AuthorizationPolicyResourceFormatInner parameters) {
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (hubName == null) {
            throw new IllegalArgumentException("Parameter hubName is required and cannot be null.");
        }
        if (authorizationPolicyName == null) {
            throw new IllegalArgumentException("Parameter authorizationPolicyName is required and cannot be null.");
        }
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (parameters == null) {
            throw new IllegalArgumentException("Parameter parameters is required and cannot be null.");
        }
        if (this.client.apiVersion() == null) {
            throw new IllegalArgumentException("Parameter this.client.apiVersion() is required and cannot be null.");
        }
        Validator.validate(parameters);
        return service.createOrUpdate(resourceGroupName, hubName, authorizationPolicyName, this.client.subscriptionId(), parameters, this.client.apiVersion(), this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<AuthorizationPolicyResourceFormatInner>>>() {
                @Override
                public Observable<ServiceResponse<AuthorizationPolicyResourceFormatInner>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<AuthorizationPolicyResourceFormatInner> clientResponse = createOrUpdateDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<AuthorizationPolicyResourceFormatInner> createOrUpdateDelegate(Response<ResponseBody> response) throws CloudException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<AuthorizationPolicyResourceFormatInner, CloudException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<AuthorizationPolicyResourceFormatInner>() { }.getType())
                .register(201, new TypeToken<AuthorizationPolicyResourceFormatInner>() { }.getType())
                .registerError(CloudException.class)
                .build(response);
    }

    /**
     * Gets an authorization policy in the hub.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws CloudException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the AuthorizationPolicyResourceFormatInner object if successful.
     */
    public AuthorizationPolicyResourceFormatInner get(String resourceGroupName, String hubName, String authorizationPolicyName) {
        return getWithServiceResponseAsync(resourceGroupName, hubName, authorizationPolicyName).toBlocking().single().body();
    }

    /**
     * Gets an authorization policy in the hub.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<AuthorizationPolicyResourceFormatInner> getAsync(String resourceGroupName, String hubName, String authorizationPolicyName, final ServiceCallback<AuthorizationPolicyResourceFormatInner> serviceCallback) {
        return ServiceFuture.fromResponse(getWithServiceResponseAsync(resourceGroupName, hubName, authorizationPolicyName), serviceCallback);
    }

    /**
     * Gets an authorization policy in the hub.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the AuthorizationPolicyResourceFormatInner object
     */
    public Observable<AuthorizationPolicyResourceFormatInner> getAsync(String resourceGroupName, String hubName, String authorizationPolicyName) {
        return getWithServiceResponseAsync(resourceGroupName, hubName, authorizationPolicyName).map(new Func1<ServiceResponse<AuthorizationPolicyResourceFormatInner>, AuthorizationPolicyResourceFormatInner>() {
            @Override
            public AuthorizationPolicyResourceFormatInner call(ServiceResponse<AuthorizationPolicyResourceFormatInner> response) {
                return response.body();
            }
        });
    }

    /**
     * Gets an authorization policy in the hub.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the AuthorizationPolicyResourceFormatInner object
     */
    public Observable<ServiceResponse<AuthorizationPolicyResourceFormatInner>> getWithServiceResponseAsync(String resourceGroupName, String hubName, String authorizationPolicyName) {
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (hubName == null) {
            throw new IllegalArgumentException("Parameter hubName is required and cannot be null.");
        }
        if (authorizationPolicyName == null) {
            throw new IllegalArgumentException("Parameter authorizationPolicyName is required and cannot be null.");
        }
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (this.client.apiVersion() == null) {
            throw new IllegalArgumentException("Parameter this.client.apiVersion() is required and cannot be null.");
        }
        return service.get(resourceGroupName, hubName, authorizationPolicyName, this.client.subscriptionId(), this.client.apiVersion(), this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<AuthorizationPolicyResourceFormatInner>>>() {
                @Override
                public Observable<ServiceResponse<AuthorizationPolicyResourceFormatInner>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<AuthorizationPolicyResourceFormatInner> clientResponse = getDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<AuthorizationPolicyResourceFormatInner> getDelegate(Response<ResponseBody> response) throws CloudException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<AuthorizationPolicyResourceFormatInner, CloudException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<AuthorizationPolicyResourceFormatInner>() { }.getType())
                .registerError(CloudException.class)
                .build(response);
    }

    /**
     * Gets all the authorization policies in a specified hub.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws CloudException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the PagedList&lt;AuthorizationPolicyResourceFormatInner&gt; object if successful.
     */
    public PagedList<AuthorizationPolicyResourceFormatInner> listByHub(final String resourceGroupName, final String hubName) {
        ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>> response = listByHubSinglePageAsync(resourceGroupName, hubName).toBlocking().single();
        return new PagedList<AuthorizationPolicyResourceFormatInner>(response.body()) {
            @Override
            public Page<AuthorizationPolicyResourceFormatInner> nextPage(String nextPageLink) {
                return listByHubNextSinglePageAsync(nextPageLink).toBlocking().single().body();
            }
        };
    }

    /**
     * Gets all the authorization policies in a specified hub.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<List<AuthorizationPolicyResourceFormatInner>> listByHubAsync(final String resourceGroupName, final String hubName, final ListOperationCallback<AuthorizationPolicyResourceFormatInner> serviceCallback) {
        return AzureServiceFuture.fromPageResponse(
            listByHubSinglePageAsync(resourceGroupName, hubName),
            new Func1<String, Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>>>() {
                @Override
                public Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>> call(String nextPageLink) {
                    return listByHubNextSinglePageAsync(nextPageLink);
                }
            },
            serviceCallback);
    }

    /**
     * Gets all the authorization policies in a specified hub.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the PagedList&lt;AuthorizationPolicyResourceFormatInner&gt; object
     */
    public Observable<Page<AuthorizationPolicyResourceFormatInner>> listByHubAsync(final String resourceGroupName, final String hubName) {
        return listByHubWithServiceResponseAsync(resourceGroupName, hubName)
            .map(new Func1<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>, Page<AuthorizationPolicyResourceFormatInner>>() {
                @Override
                public Page<AuthorizationPolicyResourceFormatInner> call(ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>> response) {
                    return response.body();
                }
            });
    }

    /**
     * Gets all the authorization policies in a specified hub.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the PagedList&lt;AuthorizationPolicyResourceFormatInner&gt; object
     */
    public Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>> listByHubWithServiceResponseAsync(final String resourceGroupName, final String hubName) {
        return listByHubSinglePageAsync(resourceGroupName, hubName)
            .concatMap(new Func1<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>, Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>>>() {
                @Override
                public Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>> call(ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>> page) {
                    String nextPageLink = page.body().nextPageLink();
                    if (nextPageLink == null) {
                        return Observable.just(page);
                    }
                    return Observable.just(page).concatWith(listByHubNextWithServiceResponseAsync(nextPageLink));
                }
            });
    }

    /**
     * Gets all the authorization policies in a specified hub.
     *
    ServiceResponse<PageImpl<AuthorizationPolicyResourceFormatInner>> * @param resourceGroupName The name of the resource group.
    ServiceResponse<PageImpl<AuthorizationPolicyResourceFormatInner>> * @param hubName The name of the hub.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the PagedList&lt;AuthorizationPolicyResourceFormatInner&gt; object wrapped in {@link ServiceResponse} if successful.
     */
    public Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>> listByHubSinglePageAsync(final String resourceGroupName, final String hubName) {
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (hubName == null) {
            throw new IllegalArgumentException("Parameter hubName is required and cannot be null.");
        }
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (this.client.apiVersion() == null) {
            throw new IllegalArgumentException("Parameter this.client.apiVersion() is required and cannot be null.");
        }
        return service.listByHub(resourceGroupName, hubName, this.client.subscriptionId(), this.client.apiVersion(), this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>>>() {
                @Override
                public Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<PageImpl<AuthorizationPolicyResourceFormatInner>> result = listByHubDelegate(response);
                        return Observable.just(new ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>(result.body(), result.response()));
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<PageImpl<AuthorizationPolicyResourceFormatInner>> listByHubDelegate(Response<ResponseBody> response) throws CloudException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<PageImpl<AuthorizationPolicyResourceFormatInner>, CloudException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<PageImpl<AuthorizationPolicyResourceFormatInner>>() { }.getType())
                .registerError(CloudException.class)
                .build(response);
    }

    /**
     * Regenerates the primary policy key of the specified authorization policy.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws CloudException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the AuthorizationPolicyInner object if successful.
     */
    public AuthorizationPolicyInner regeneratePrimaryKey(String resourceGroupName, String hubName, String authorizationPolicyName) {
        return regeneratePrimaryKeyWithServiceResponseAsync(resourceGroupName, hubName, authorizationPolicyName).toBlocking().single().body();
    }

    /**
     * Regenerates the primary policy key of the specified authorization policy.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<AuthorizationPolicyInner> regeneratePrimaryKeyAsync(String resourceGroupName, String hubName, String authorizationPolicyName, final ServiceCallback<AuthorizationPolicyInner> serviceCallback) {
        return ServiceFuture.fromResponse(regeneratePrimaryKeyWithServiceResponseAsync(resourceGroupName, hubName, authorizationPolicyName), serviceCallback);
    }

    /**
     * Regenerates the primary policy key of the specified authorization policy.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the AuthorizationPolicyInner object
     */
    public Observable<AuthorizationPolicyInner> regeneratePrimaryKeyAsync(String resourceGroupName, String hubName, String authorizationPolicyName) {
        return regeneratePrimaryKeyWithServiceResponseAsync(resourceGroupName, hubName, authorizationPolicyName).map(new Func1<ServiceResponse<AuthorizationPolicyInner>, AuthorizationPolicyInner>() {
            @Override
            public AuthorizationPolicyInner call(ServiceResponse<AuthorizationPolicyInner> response) {
                return response.body();
            }
        });
    }

    /**
     * Regenerates the primary policy key of the specified authorization policy.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the AuthorizationPolicyInner object
     */
    public Observable<ServiceResponse<AuthorizationPolicyInner>> regeneratePrimaryKeyWithServiceResponseAsync(String resourceGroupName, String hubName, String authorizationPolicyName) {
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (hubName == null) {
            throw new IllegalArgumentException("Parameter hubName is required and cannot be null.");
        }
        if (authorizationPolicyName == null) {
            throw new IllegalArgumentException("Parameter authorizationPolicyName is required and cannot be null.");
        }
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (this.client.apiVersion() == null) {
            throw new IllegalArgumentException("Parameter this.client.apiVersion() is required and cannot be null.");
        }
        return service.regeneratePrimaryKey(resourceGroupName, hubName, authorizationPolicyName, this.client.subscriptionId(), this.client.apiVersion(), this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<AuthorizationPolicyInner>>>() {
                @Override
                public Observable<ServiceResponse<AuthorizationPolicyInner>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<AuthorizationPolicyInner> clientResponse = regeneratePrimaryKeyDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<AuthorizationPolicyInner> regeneratePrimaryKeyDelegate(Response<ResponseBody> response) throws CloudException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<AuthorizationPolicyInner, CloudException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<AuthorizationPolicyInner>() { }.getType())
                .registerError(CloudException.class)
                .build(response);
    }

    /**
     * Regenerates the secondary policy key of the specified authorization policy.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws CloudException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the AuthorizationPolicyInner object if successful.
     */
    public AuthorizationPolicyInner regenerateSecondaryKey(String resourceGroupName, String hubName, String authorizationPolicyName) {
        return regenerateSecondaryKeyWithServiceResponseAsync(resourceGroupName, hubName, authorizationPolicyName).toBlocking().single().body();
    }

    /**
     * Regenerates the secondary policy key of the specified authorization policy.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<AuthorizationPolicyInner> regenerateSecondaryKeyAsync(String resourceGroupName, String hubName, String authorizationPolicyName, final ServiceCallback<AuthorizationPolicyInner> serviceCallback) {
        return ServiceFuture.fromResponse(regenerateSecondaryKeyWithServiceResponseAsync(resourceGroupName, hubName, authorizationPolicyName), serviceCallback);
    }

    /**
     * Regenerates the secondary policy key of the specified authorization policy.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the AuthorizationPolicyInner object
     */
    public Observable<AuthorizationPolicyInner> regenerateSecondaryKeyAsync(String resourceGroupName, String hubName, String authorizationPolicyName) {
        return regenerateSecondaryKeyWithServiceResponseAsync(resourceGroupName, hubName, authorizationPolicyName).map(new Func1<ServiceResponse<AuthorizationPolicyInner>, AuthorizationPolicyInner>() {
            @Override
            public AuthorizationPolicyInner call(ServiceResponse<AuthorizationPolicyInner> response) {
                return response.body();
            }
        });
    }

    /**
     * Regenerates the secondary policy key of the specified authorization policy.
     *
     * @param resourceGroupName The name of the resource group.
     * @param hubName The name of the hub.
     * @param authorizationPolicyName The name of the policy.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the AuthorizationPolicyInner object
     */
    public Observable<ServiceResponse<AuthorizationPolicyInner>> regenerateSecondaryKeyWithServiceResponseAsync(String resourceGroupName, String hubName, String authorizationPolicyName) {
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (hubName == null) {
            throw new IllegalArgumentException("Parameter hubName is required and cannot be null.");
        }
        if (authorizationPolicyName == null) {
            throw new IllegalArgumentException("Parameter authorizationPolicyName is required and cannot be null.");
        }
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (this.client.apiVersion() == null) {
            throw new IllegalArgumentException("Parameter this.client.apiVersion() is required and cannot be null.");
        }
        return service.regenerateSecondaryKey(resourceGroupName, hubName, authorizationPolicyName, this.client.subscriptionId(), this.client.apiVersion(), this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<AuthorizationPolicyInner>>>() {
                @Override
                public Observable<ServiceResponse<AuthorizationPolicyInner>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<AuthorizationPolicyInner> clientResponse = regenerateSecondaryKeyDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<AuthorizationPolicyInner> regenerateSecondaryKeyDelegate(Response<ResponseBody> response) throws CloudException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<AuthorizationPolicyInner, CloudException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<AuthorizationPolicyInner>() { }.getType())
                .registerError(CloudException.class)
                .build(response);
    }

    /**
     * Gets all the authorization policies in a specified hub.
     *
     * @param nextPageLink The NextLink from the previous successful call to List operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws CloudException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the PagedList&lt;AuthorizationPolicyResourceFormatInner&gt; object if successful.
     */
    public PagedList<AuthorizationPolicyResourceFormatInner> listByHubNext(final String nextPageLink) {
        ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>> response = listByHubNextSinglePageAsync(nextPageLink).toBlocking().single();
        return new PagedList<AuthorizationPolicyResourceFormatInner>(response.body()) {
            @Override
            public Page<AuthorizationPolicyResourceFormatInner> nextPage(String nextPageLink) {
                return listByHubNextSinglePageAsync(nextPageLink).toBlocking().single().body();
            }
        };
    }

    /**
     * Gets all the authorization policies in a specified hub.
     *
     * @param nextPageLink The NextLink from the previous successful call to List operation.
     * @param serviceFuture the ServiceFuture object tracking the Retrofit calls
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<List<AuthorizationPolicyResourceFormatInner>> listByHubNextAsync(final String nextPageLink, final ServiceFuture<List<AuthorizationPolicyResourceFormatInner>> serviceFuture, final ListOperationCallback<AuthorizationPolicyResourceFormatInner> serviceCallback) {
        return AzureServiceFuture.fromPageResponse(
            listByHubNextSinglePageAsync(nextPageLink),
            new Func1<String, Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>>>() {
                @Override
                public Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>> call(String nextPageLink) {
                    return listByHubNextSinglePageAsync(nextPageLink);
                }
            },
            serviceCallback);
    }

    /**
     * Gets all the authorization policies in a specified hub.
     *
     * @param nextPageLink The NextLink from the previous successful call to List operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the PagedList&lt;AuthorizationPolicyResourceFormatInner&gt; object
     */
    public Observable<Page<AuthorizationPolicyResourceFormatInner>> listByHubNextAsync(final String nextPageLink) {
        return listByHubNextWithServiceResponseAsync(nextPageLink)
            .map(new Func1<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>, Page<AuthorizationPolicyResourceFormatInner>>() {
                @Override
                public Page<AuthorizationPolicyResourceFormatInner> call(ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>> response) {
                    return response.body();
                }
            });
    }

    /**
     * Gets all the authorization policies in a specified hub.
     *
     * @param nextPageLink The NextLink from the previous successful call to List operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the PagedList&lt;AuthorizationPolicyResourceFormatInner&gt; object
     */
    public Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>> listByHubNextWithServiceResponseAsync(final String nextPageLink) {
        return listByHubNextSinglePageAsync(nextPageLink)
            .concatMap(new Func1<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>, Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>>>() {
                @Override
                public Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>> call(ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>> page) {
                    String nextPageLink = page.body().nextPageLink();
                    if (nextPageLink == null) {
                        return Observable.just(page);
                    }
                    return Observable.just(page).concatWith(listByHubNextWithServiceResponseAsync(nextPageLink));
                }
            });
    }

    /**
     * Gets all the authorization policies in a specified hub.
     *
    ServiceResponse<PageImpl<AuthorizationPolicyResourceFormatInner>> * @param nextPageLink The NextLink from the previous successful call to List operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the PagedList&lt;AuthorizationPolicyResourceFormatInner&gt; object wrapped in {@link ServiceResponse} if successful.
     */
    public Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>> listByHubNextSinglePageAsync(final String nextPageLink) {
        if (nextPageLink == null) {
            throw new IllegalArgumentException("Parameter nextPageLink is required and cannot be null.");
        }
        String nextUrl = String.format("%s", nextPageLink);
        return service.listByHubNext(nextUrl, this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>>>() {
                @Override
                public Observable<ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<PageImpl<AuthorizationPolicyResourceFormatInner>> result = listByHubNextDelegate(response);
                        return Observable.just(new ServiceResponse<Page<AuthorizationPolicyResourceFormatInner>>(result.body(), result.response()));
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<PageImpl<AuthorizationPolicyResourceFormatInner>> listByHubNextDelegate(Response<ResponseBody> response) throws CloudException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<PageImpl<AuthorizationPolicyResourceFormatInner>, CloudException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<PageImpl<AuthorizationPolicyResourceFormatInner>>() { }.getType())
                .registerError(CloudException.class)
                .build(response);
    }

}
