Search in sources :

Example 6 with NeutronRestApiException

use of org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException 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 7 with NeutronRestApiException

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

the class NeutronNetworksNorthboundAction method updateNeutronNetwork.

public <T> void updateNeutronNetwork(final String networkId, final NeutronNetworkWrapper newNetworkWrapper) throws NeutronRestApiException {
    try {
        String uri = NeutronNorthboundEnum.NETWORK_PARAM_URI.getUri();
        uri = MessageFormat.format(uri, networkId);
        StringRequestEntity entity = new StringRequestEntity(gsonNeutronNetwork.toJson(newNetworkWrapper), JSON_CONTENT_TYPE, null);
        executePut(uri, entity);
    } catch (UnsupportedEncodingException e) {
        throw new NeutronRestApiException("Failed to encode json request body", e);
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException)

Example 8 with NeutronRestApiException

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

the class NeutronPortsNorthboundAction method createNeutronPort.

@SuppressWarnings("unchecked")
public <T> T createNeutronPort(final NeutronPortWrapper newPortWrapper) throws NeutronRestApiException {
    try {
        String uri = NeutronNorthboundEnum.PORTS_URI.getUri();
        StringRequestEntity entity = new StringRequestEntity(gsonNeutronPort.toJson(newPortWrapper), JSON_CONTENT_TYPE, null);
        String bodystring = executePost(uri, entity);
        T result = (T) gsonNeutronPort.fromJson(bodystring, TypeToken.get(NeutronPortWrapper.class).getType());
        return result;
    } catch (UnsupportedEncodingException e) {
        throw new NeutronRestApiException("Failed to encode json request body", e);
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException) NeutronPortWrapper(org.apache.cloudstack.network.opendaylight.api.model.NeutronPortWrapper)

Example 9 with NeutronRestApiException

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

the class NeutronPortsNorthboundAction method updateNeutronPort.

public <T> void updateNeutronPort(final String portId, final NeutronPortWrapper newPortWrapper) throws NeutronRestApiException {
    try {
        String uri = NeutronNorthboundEnum.PORTS_PARAM_URI.getUri();
        uri = MessageFormat.format(uri, portId);
        StringRequestEntity entity = new StringRequestEntity(gsonNeutronPort.toJson(newPortWrapper), JSON_CONTENT_TYPE, null);
        executePut(uri, entity);
    } catch (UnsupportedEncodingException e) {
        throw new NeutronRestApiException("Failed to encode json request body", e);
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException)

Example 10 with NeutronRestApiException

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

the class Action method executeGet.

public String executeGet(final String uri, final Map<String, String> parameters) throws NeutronRestApiException {
    try {
        validateCredentials();
    } catch (NeutronInvalidCredentialsException e) {
        throw new NeutronRestApiException("Invalid credentials!", e);
    }
    NeutronRestFactory factory = NeutronRestFactory.getInstance();
    NeutronRestApi neutronRestApi = factory.getNeutronApi(GetMethod.class);
    GetMethod getMethod = (GetMethod) neutronRestApi.createMethod(url, uri);
    try {
        getMethod.setRequestHeader(CONTENT_TYPE, JSON_CONTENT_TYPE);
        String encodedCredentials = encodeCredentials();
        getMethod.setRequestHeader("Authorization", "Basic " + encodedCredentials);
        if (parameters != null && !parameters.isEmpty()) {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(parameters.size());
            for (Entry<String, String> e : parameters.entrySet()) {
                nameValuePairs.add(new NameValuePair(e.getKey(), e.getValue()));
            }
            getMethod.setQueryString(nameValuePairs.toArray(new NameValuePair[0]));
        }
        neutronRestApi.executeMethod(getMethod);
        if (getMethod.getStatusCode() != HttpStatus.SC_OK) {
            String errorMessage = responseToErrorMessage(getMethod);
            getMethod.releaseConnection();
            s_logger.error("Failed to retrieve object : " + errorMessage);
            throw new NeutronRestApiException("Failed to retrieve object : " + errorMessage);
        }
        return getMethod.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(e);
    } finally {
        getMethod.releaseConnection();
    }
}
Also used : NeutronRestFactory(org.apache.cloudstack.network.opendaylight.api.NeutronRestFactory) NeutronRestApi(org.apache.cloudstack.network.opendaylight.api.NeutronRestApi) NameValuePair(org.apache.commons.httpclient.NameValuePair) NeutronInvalidCredentialsException(org.apache.cloudstack.network.opendaylight.api.NeutronInvalidCredentialsException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) ArrayList(java.util.ArrayList) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException) IOException(java.io.IOException)

Aggregations

NeutronRestApiException (org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException)12 NeutronInvalidCredentialsException (org.apache.cloudstack.network.opendaylight.api.NeutronInvalidCredentialsException)5 NeutronRestApi (org.apache.cloudstack.network.opendaylight.api.NeutronRestApi)5 NeutronRestFactory (org.apache.cloudstack.network.opendaylight.api.NeutronRestFactory)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)4 IOException (java.io.IOException)3 NeutronNetworkWrapper (org.apache.cloudstack.network.opendaylight.api.model.NeutronNetworkWrapper)2 NeutronPortWrapper (org.apache.cloudstack.network.opendaylight.api.model.NeutronPortWrapper)2 PutMethod (org.apache.commons.httpclient.methods.PutMethod)2 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 AddHypervisorAnswer (org.apache.cloudstack.network.opendaylight.agent.responses.AddHypervisorAnswer)1 ConfigureNetworkAnswer (org.apache.cloudstack.network.opendaylight.agent.responses.ConfigureNetworkAnswer)1 ConfigurePortAnswer (org.apache.cloudstack.network.opendaylight.agent.responses.ConfigurePortAnswer)1 NeutronNetwork (org.apache.cloudstack.network.opendaylight.api.model.NeutronNetwork)1 NeutronNode (org.apache.cloudstack.network.opendaylight.api.model.NeutronNode)1 NeutronNodeWrapper (org.apache.cloudstack.network.opendaylight.api.model.NeutronNodeWrapper)1 NeutronPort (org.apache.cloudstack.network.opendaylight.api.model.NeutronPort)1 NeutronNetworksNorthboundAction (org.apache.cloudstack.network.opendaylight.api.resources.NeutronNetworksNorthboundAction)1