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");
}
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());
}
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();
}
}
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();
}
}
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();
}
}
Aggregations