Search in sources :

Example 1 with NeutronRestApi

use of org.apache.cloudstack.network.opendaylight.api.NeutronRestApi in project cloudstack by apache.

the class Action method executePut.

protected String executePut(final String uri) throws NeutronRestApiException {
    try {
        validateCredentials();
    } catch (NeutronInvalidCredentialsException e) {
        throw new NeutronRestApiException("Invalid credentials!", e);
    }
    NeutronRestFactory factory = NeutronRestFactory.getInstance();
    NeutronRestApi neutronRestApi = factory.getNeutronApi(PutMethod.class);
    PutMethod putMethod = (PutMethod) neutronRestApi.createMethod(url, uri);
    try {
        String encodedCredentials = encodeCredentials();
        putMethod.setRequestHeader("Authorization", "Basic " + encodedCredentials);
        neutronRestApi.executeMethod(putMethod);
        if (putMethod.getStatusCode() != HttpStatus.SC_OK) {
            String errorMessage = responseToErrorMessage(putMethod);
            putMethod.releaseConnection();
            s_logger.error("Failed to update object : " + errorMessage);
            throw new NeutronRestApiException("Failed to create object : " + errorMessage);
        }
        return putMethod.getResponseBodyAsString();
    } catch (NeutronRestApiException e) {
        s_logger.error("NeutronRestApiException caught while trying to execute HTTP Method on the Neutron Controller", e);
        throw new NeutronRestApiException("API call to Neutron Controller Failed", e);
    } catch (IOException e) {
        throw new NeutronRestApiException("Failed to load json response body", e);
    } finally {
        putMethod.releaseConnection();
    }
}
Also used : NeutronRestFactory(org.apache.cloudstack.network.opendaylight.api.NeutronRestFactory) NeutronRestApi(org.apache.cloudstack.network.opendaylight.api.NeutronRestApi) NeutronInvalidCredentialsException(org.apache.cloudstack.network.opendaylight.api.NeutronInvalidCredentialsException) PutMethod(org.apache.commons.httpclient.methods.PutMethod) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException) IOException(java.io.IOException)

Example 2 with NeutronRestApi

use of org.apache.cloudstack.network.opendaylight.api.NeutronRestApi in project cloudstack by apache.

the class Action method executePost.

protected String executePost(final String uri, final StringRequestEntity entity) throws NeutronRestApiException {
    try {
        validateCredentials();
    } catch (NeutronInvalidCredentialsException e) {
        throw new NeutronRestApiException("Invalid credentials!", e);
    }
    NeutronRestFactory factory = NeutronRestFactory.getInstance();
    NeutronRestApi neutronRestApi = factory.getNeutronApi(PostMethod.class);
    PostMethod postMethod = (PostMethod) neutronRestApi.createMethod(url, uri);
    try {
        postMethod.setRequestHeader(CONTENT_TYPE, JSON_CONTENT_TYPE);
        postMethod.setRequestEntity(entity);
        String encodedCredentials = encodeCredentials();
        postMethod.setRequestHeader("Authorization", "Basic " + encodedCredentials);
        neutronRestApi.executeMethod(postMethod);
        if (postMethod.getStatusCode() != HttpStatus.SC_CREATED) {
            String errorMessage = responseToErrorMessage(postMethod);
            postMethod.releaseConnection();
            s_logger.error("Failed to create object : " + errorMessage);
            throw new NeutronRestApiException("Failed to create object : " + errorMessage);
        }
        return postMethod.getResponseBodyAsString();
    } catch (NeutronRestApiException e) {
        s_logger.error("NeutronRestApiException caught while trying to execute HTTP Method on the Neutron Controller", e);
        throw new NeutronRestApiException("API call to Neutron Controller Failed", e);
    } catch (IOException e) {
        throw new NeutronRestApiException("Failed to load json response body", e);
    } finally {
        postMethod.releaseConnection();
    }
}
Also used : NeutronRestFactory(org.apache.cloudstack.network.opendaylight.api.NeutronRestFactory) NeutronRestApi(org.apache.cloudstack.network.opendaylight.api.NeutronRestApi) NeutronInvalidCredentialsException(org.apache.cloudstack.network.opendaylight.api.NeutronInvalidCredentialsException) PostMethod(org.apache.commons.httpclient.methods.PostMethod) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException) IOException(java.io.IOException)

Example 3 with NeutronRestApi

use of org.apache.cloudstack.network.opendaylight.api.NeutronRestApi in project cloudstack by apache.

the class Action method executePut.

protected void executePut(final String uri, final StringRequestEntity entity) throws NeutronRestApiException {
    try {
        validateCredentials();
    } catch (NeutronInvalidCredentialsException e) {
        throw new NeutronRestApiException("Invalid credentials!", e);
    }
    NeutronRestFactory factory = NeutronRestFactory.getInstance();
    NeutronRestApi neutronRestApi = factory.getNeutronApi(PutMethod.class);
    PutMethod putMethod = (PutMethod) neutronRestApi.createMethod(url, uri);
    try {
        putMethod.setRequestHeader(CONTENT_TYPE, JSON_CONTENT_TYPE);
        putMethod.setRequestEntity(entity);
        String encodedCredentials = encodeCredentials();
        putMethod.setRequestHeader("Authorization", "Basic " + encodedCredentials);
        neutronRestApi.executeMethod(putMethod);
        if (putMethod.getStatusCode() != HttpStatus.SC_OK) {
            String errorMessage = responseToErrorMessage(putMethod);
            putMethod.releaseConnection();
            s_logger.error("Failed to update object : " + errorMessage);
            throw new NeutronRestApiException("Failed to create object : " + errorMessage);
        }
    } catch (NeutronRestApiException e) {
        s_logger.error("NeutronRestApiException caught while trying to execute HTTP Method on the Neutron Controller", e);
        throw new NeutronRestApiException("API call to Neutron Controller Failed", e);
    } finally {
        putMethod.releaseConnection();
    }
}
Also used : NeutronRestFactory(org.apache.cloudstack.network.opendaylight.api.NeutronRestFactory) NeutronRestApi(org.apache.cloudstack.network.opendaylight.api.NeutronRestApi) NeutronInvalidCredentialsException(org.apache.cloudstack.network.opendaylight.api.NeutronInvalidCredentialsException) PutMethod(org.apache.commons.httpclient.methods.PutMethod) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException)

Example 4 with NeutronRestApi

use of org.apache.cloudstack.network.opendaylight.api.NeutronRestApi in project cloudstack by apache.

the class Action method executeDelete.

protected void executeDelete(final String uri) throws NeutronRestApiException {
    try {
        validateCredentials();
    } catch (NeutronInvalidCredentialsException e) {
        throw new NeutronRestApiException("Invalid credentials!", e);
    }
    NeutronRestFactory factory = NeutronRestFactory.getInstance();
    NeutronRestApi neutronRestApi = factory.getNeutronApi(DeleteMethod.class);
    DeleteMethod deleteMethod = (DeleteMethod) neutronRestApi.createMethod(url, uri);
    try {
        deleteMethod.setRequestHeader(CONTENT_TYPE, JSON_CONTENT_TYPE);
        String encodedCredentials = encodeCredentials();
        deleteMethod.setRequestHeader("Authorization", "Basic " + encodedCredentials);
        neutronRestApi.executeMethod(deleteMethod);
        if (deleteMethod.getStatusCode() != HttpStatus.SC_NO_CONTENT) {
            String errorMessage = responseToErrorMessage(deleteMethod);
            deleteMethod.releaseConnection();
            s_logger.error("Failed to update object : " + errorMessage);
            throw new NeutronRestApiException("Failed to create object : " + errorMessage);
        }
    } catch (NeutronRestApiException e) {
        s_logger.error("NeutronRestApiException caught while trying to execute HTTP Method on the Neutron Controller", e);
        throw new NeutronRestApiException("API call to Neutron Controller Failed", e);
    } finally {
        deleteMethod.releaseConnection();
    }
}
Also used : NeutronRestFactory(org.apache.cloudstack.network.opendaylight.api.NeutronRestFactory) NeutronRestApi(org.apache.cloudstack.network.opendaylight.api.NeutronRestApi) DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) NeutronInvalidCredentialsException(org.apache.cloudstack.network.opendaylight.api.NeutronInvalidCredentialsException) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException)

Example 5 with NeutronRestApi

use of org.apache.cloudstack.network.opendaylight.api.NeutronRestApi in project cloudstack by apache.

the class NeutronRestApiMock method resourceHttpGetInstances.

@Test
public void resourceHttpGetInstances() throws NeutronRestApiException {
    NeutronRestApi newHttpGet = factory.getNeutronApi(GetMethod.class);
    assertTrue(httpGet == newHttpGet);
}
Also used : NeutronRestApi(org.apache.cloudstack.network.opendaylight.api.NeutronRestApi) Test(org.junit.Test)

Aggregations

NeutronRestApi (org.apache.cloudstack.network.opendaylight.api.NeutronRestApi)9 NeutronInvalidCredentialsException (org.apache.cloudstack.network.opendaylight.api.NeutronInvalidCredentialsException)5 NeutronRestApiException (org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException)5 NeutronRestFactory (org.apache.cloudstack.network.opendaylight.api.NeutronRestFactory)5 Test (org.junit.Test)4 IOException (java.io.IOException)3 PutMethod (org.apache.commons.httpclient.methods.PutMethod)2 ArrayList (java.util.ArrayList)1 NameValuePair (org.apache.commons.httpclient.NameValuePair)1 DeleteMethod (org.apache.commons.httpclient.methods.DeleteMethod)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 PostMethod (org.apache.commons.httpclient.methods.PostMethod)1