Search in sources :

Example 1 with NeutronRestApiException

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

the class OpenDaylightControllerResource method executeRequest.

private Answer executeRequest(AddHypervisorCommand cmd) {
    NeutronNodesNorthboundAction nodeActions = new NeutronNodesNorthboundAction(controllerUrl, controllerUsername, controllerPassword);
    try {
        NeutronNodesList<NeutronNodeWrapper> nodes = nodeActions.listAllNodes();
        if (nodes.getNodes() != null) {
            for (NeutronNodeWrapper nodeWrapper : nodes.getNodes()) {
                NeutronNode node = nodeWrapper.getNode();
                if (node.getId().equals(cmd.getHostId())) {
                    return new AddHypervisorAnswer(cmd, true, "Hypervisor already connected");
                }
            }
        }
        // Not found in the existing node list, add it
        nodeActions.updateNeutronNodeV2("OVS", cmd.getHostId(), cmd.getIpAddress(), 6640);
    } catch (NeutronRestApiException e) {
        s_logger.error("Call to OpenDaylight failed", e);
        return new AddHypervisorAnswer(cmd, e);
    }
    return new AddHypervisorAnswer(cmd, true, "Hypervisor " + cmd.getHostId() + " added");
}
Also used : NeutronNodeWrapper(org.apache.cloudstack.network.opendaylight.api.model.NeutronNodeWrapper) NeutronNode(org.apache.cloudstack.network.opendaylight.api.model.NeutronNode) NeutronNodesNorthboundAction(org.apache.cloudstack.network.opendaylight.api.resources.NeutronNodesNorthboundAction) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException) AddHypervisorAnswer(org.apache.cloudstack.network.opendaylight.agent.responses.AddHypervisorAnswer)

Example 2 with NeutronRestApiException

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

the class OpenDaylightControllerResource method executeRequest.

private Answer executeRequest(ConfigureNetworkCommand cmd) {
    NeutronNetworksNorthboundAction configureNetwork = new NeutronNetworksNorthboundAction(controllerUrl, controllerUsername, controllerPassword);
    // Find free gre key
    int gre_key = -1;
    Random keyGenerator = new Random(System.currentTimeMillis());
    try {
        NeutronNetworksList<NeutronNetwork> networks = configureNetwork.listAllNetworks();
        while (true) {
            int i = keyGenerator.nextInt();
            for (NeutronNetwork network : networks.getNetworks()) {
                if (network.getSegmentationId() == i) {
                    continue;
                }
            }
            gre_key = i;
            break;
        }
    } catch (NeutronRestApiException e) {
        s_logger.error("Failed to list existing networks on the ODL Controller", e);
        return new ConfigureNetworkAnswer(cmd, e);
    }
    NeutronNetwork newNetwork = new NeutronNetwork();
    // Configuration from the command
    newNetwork.setName(cmd.getName());
    newNetwork.setTenantId(cmd.getTenantId());
    // Static configuation
    newNetwork.setNetworkType("gre");
    newNetwork.setShared(false);
    newNetwork.setSegmentationId(gre_key);
    newNetwork.setId(UUID.randomUUID());
    NeutronNetworkWrapper wrapper = new NeutronNetworkWrapper();
    wrapper.setNetwork(newNetwork);
    try {
        wrapper = configureNetwork.createNeutronNetwork(wrapper);
    } catch (NeutronRestApiException e) {
        s_logger.error("createNeutronNetwork failed", e);
        return new ConfigureNetworkAnswer(cmd, e);
    }
    return new ConfigureNetworkAnswer(cmd, true, null, wrapper.getNetwork().getId().toString());
}
Also used : NeutronNetwork(org.apache.cloudstack.network.opendaylight.api.model.NeutronNetwork) Random(java.util.Random) ConfigureNetworkAnswer(org.apache.cloudstack.network.opendaylight.agent.responses.ConfigureNetworkAnswer) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException) NeutronNetworkWrapper(org.apache.cloudstack.network.opendaylight.api.model.NeutronNetworkWrapper) NeutronNetworksNorthboundAction(org.apache.cloudstack.network.opendaylight.api.resources.NeutronNetworksNorthboundAction)

Example 3 with NeutronRestApiException

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

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

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

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