@Override public void closeResponse(final CloseableHttpResponse response) throws CloudstackRESTException { try { s_logger.debug("Closing HTTP connection"); response.close(); } catch (final IOException e) { final StringBuilder sb = new StringBuilder(); sb.append("Failed to close response object for request.\nResponse: ").append(response); throw new CloudstackRESTException(sb.toString(), e); } }
public ControlClusterStatus getControlClusterStatus() throws NiciraNvpApiException { final String uri = NiciraConstants.CONTROL_CLUSTER_STATUS_URL; try { return restConnector.executeRetrieveObject(ControlClusterStatus.class, uri, new HashMap<String, String>()); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } }
niciraNvpApi = createNiciraNvpApi(ip, adminuser, adminpass); } catch (final CloudstackRESTException e) { throw new ConfigurationException("Could not create a Nicira Nvp API client: " + e.getMessage());
private <T> T readResponseBody(final CloseableHttpResponse response, final Type type) throws CloudstackRESTException { final HttpEntity entity = response.getEntity(); try { final String stringEntity = EntityUtils.toString(entity); //s_logger.debug("Response entity: " + stringEntity); EntityUtils.consumeQuietly(entity); return gson.fromJson(stringEntity, type); } catch (final IOException e) { throw new CloudstackRESTException("Could not deserialize response to JSON. Entity: " + entity, e); } finally { client.closeResponse(response); } }
/** * PUT item given a UUID as key and an item object with the new data * * @param item * @param uuid * @throws NiciraNvpApiException */ private <T> void updateWithUri(final T item, final String uri) throws NiciraNvpApiException { try { restConnector.executeUpdateObject(item, uri, Collections.<String, String> emptyMap()); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } }
@Override public CloseableHttpResponse execute(final HttpUriRequest request) throws CloudstackRESTException { logRequestExecution(request); try { return client.execute(clientContext.getTargetHost(), request, clientContext); } catch (final IOException e) { throw new CloudstackRESTException("Could not execute request " + request, e); } }
/** * POST * * @param entity * @return * @throws NiciraNvpApiException */ private <T> T createWithUri(final T entity, final String uri) throws NiciraNvpApiException { T createdEntity; try { createdEntity = restConnector.executeCreateObject(entity, uri, Collections.<String, String> emptyMap()); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } return createdEntity; }
protected NiciraNvpApi createNiciraNvpApi(final String host, final String username, final String password) throws CloudstackRESTException { try { return NiciraNvpApi.create().host(host).username(username).password(password).httpClient(HttpClientHelper.createHttpClient(MAX_REDIRECTS)).build(); } catch (final KeyManagementException e) { throw new CloudstackRESTException("Could not create HTTP client", e); } catch (final NoSuchAlgorithmException e) { throw new CloudstackRESTException("Could not create HTTP client", e); } catch (final KeyStoreException e) { throw new CloudstackRESTException("Could not create HTTP client", e); } }
/** * DELETE Security Profile given a UUID as key * * @param securityProfileUuid * @throws NiciraNvpApiException */ private void deleteWithUri(final String uri) throws NiciraNvpApiException { try { restConnector.executeDeleteObject(uri); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } }
private CloseableHttpResponse execute(final HttpUriRequest request, final int previousStatusCode) throws CloudstackRESTException { if (counter.hasReachedExecutionLimit()) { throw new CloudstackRESTException("Reached max executions limit of " + executionLimit); } counter.incrementExecutionCounter(); s_logger.debug("Executing " + request.getMethod() + " request [execution count = " + counter.getValue() + "]"); final CloseableHttpResponse response = super.execute(request); final StatusLine statusLine = response.getStatusLine(); final int statusCode = statusLine.getStatusCode(); s_logger.debug("Status of last request: " + statusLine.toString()); if (HttpStatusCodeHelper.isUnauthorized(statusCode)) { return handleUnauthorizedResponse(request, previousStatusCode, response, statusCode); } else if (HttpStatusCodeHelper.isSuccess(statusCode)) { return handleSuccessResponse(request, response); } else if (HttpStatusCodeHelper.isConflict(statusCode)) { throw new CloudstackRESTException("Conflict: " + statusLine.getReasonPhrase(), statusCode); } else { throw new CloudstackRESTException("Unexpected status code: " + statusCode, statusCode); } }
/** * GET list of items * * @param uuid * * @return * @throws NiciraNvpApiException */ private <T> List<T> find(final Optional<String> uuid, final Class<T> clazz) throws NiciraNvpApiException { final String uri = prefixMap.get(clazz); Map<String, String> params = defaultListParams; if (uuid.isPresent()) { params = new HashMap<String, String>(defaultListParams); params.put(UUID_QUERY_PARAMETER, uuid.get()); } NiciraNvpList<T> entities; try { entities = restConnector.executeRetrieveObject(listTypeMap.get(clazz), uri, params); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } if (entities == null) { throw new NiciraNvpApiException("Unexpected response from API"); } return entities.getResults(); }
private CloseableHttpResponse handleUnauthorizedResponse(final HttpUriRequest request, final int previousStatusCode, final CloseableHttpResponse response, final int statusCode) throws CloudstackRESTException { super.closeResponse(response); if (HttpStatusCodeHelper.isUnauthorized(previousStatusCode)) { s_logger.error(responseToErrorMessage(response)); throw new CloudstackRESTException("Two consecutive failed attempts to authenticate against REST server"); } final HttpUriRequest authenticateRequest = createAuthenticationRequest(); final CloseableHttpResponse loginResponse = execute(authenticateRequest, statusCode); final int loginStatusCode = loginResponse.getStatusLine().getStatusCode(); super.closeResponse(loginResponse); return execute(request, loginStatusCode); }
public List<LogicalRouterPort> findLogicalRouterPortByGatewayServiceAndVlanId(final String logicalRouterUuid, final String gatewayServiceUuid, final long vlanId) throws NiciraNvpApiException { final String uri = buildLogicalRouterElementUri(logicalRouterUuid, LPORT_PATH_SEGMENT); final Map<String, String> params = buildBasicParametersMap(WILDCARD_QUERY_PARAMETER); params.put(ATTACHMENT_GWSVC_UUID_QUERY_PARAMETER, gatewayServiceUuid); params.put(NiciraConstants.ATTACHMENT_VLAN_PARAMETER, Long.toString(vlanId)); try { final Type niciraListType = new TypeToken<NiciraNvpList<LogicalRouterPort>>() { }.getType(); return restConnector.<NiciraNvpList<LogicalRouterPort>> executeRetrieveObject(niciraListType, uri, params).getResults(); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } }
public List<L2GatewayServiceConfig> findL2GatewayServiceByUuidAndType(final String gatewayServiceUuid, final String serviceConfigType) throws NiciraNvpApiException{ final String uri = buildGatewayServiceUri(); final Map<String, String> params = buildBasicParametersMap(WILDCARD_QUERY_PARAMETER); params.put(UUID_QUERY_PARAMETER, gatewayServiceUuid); params.put(TYPES_QUERY_PARAMETER, serviceConfigType); try { final Type niciraListType = new TypeToken<NiciraNvpList<L2GatewayServiceConfig>>() { }.getType(); return restConnector.<NiciraNvpList<L2GatewayServiceConfig>> executeRetrieveObject(niciraListType, uri, params).getResults(); } catch (final CloudstackRESTException e){ throw new NiciraNvpApiException(e, e.getErrorCode()); } }
public List<LogicalRouterPort> findLogicalRouterPortsByUuid(final String logicalRouterUuid, final String logicalRouterPortUuid) throws NiciraNvpApiException { final String uri = buildLogicalRouterElementUri(logicalRouterUuid, LPORT_PATH_SEGMENT); final Map<String, String> params = buildBasicParametersMap(UUID_QUERY_PARAMETER); params.put(UUID_QUERY_PARAMETER, logicalRouterPortUuid); try { final Type niciraListType = new TypeToken<NiciraNvpList<LogicalRouterPort>>() { }.getType(); return restConnector.<NiciraNvpList<LogicalRouterPort>> executeRetrieveObject(niciraListType, uri, params).getResults(); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } }
public List<LogicalRouterPort> findLogicalRouterPortByAttachmentLSwitchUuid(final String logicalRouterUuid, final String attachmentLSwitchUuid) throws NiciraNvpApiException{ final String uri = buildLogicalRouterElementUri(logicalRouterUuid, LPORT_PATH_SEGMENT); final Map<String, String> params = buildBasicParametersMap(WILDCARD_QUERY_PARAMETER); params.put(NiciraConstants.ATTACHMENT_LSWITCH_UUID, attachmentLSwitchUuid); try{ final Type niciraListType = new TypeToken<NiciraNvpList<LogicalRouterPort>>() { }.getType(); return restConnector.<NiciraNvpList<LogicalRouterPort>> executeRetrieveObject(niciraListType, uri, params).getResults(); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } }
public List<LogicalSwitchPort> findLogicalSwitchPortsByUuid(final String logicalSwitchUuid, final String logicalSwitchPortUuid) throws NiciraNvpApiException { final String uri = buildLogicalSwitchElementUri(logicalSwitchUuid, LPORT_PATH_SEGMENT); final Map<String, String> params = buildBasicParametersMap(UUID_QUERY_PARAMETER); params.put(UUID_QUERY_PARAMETER, logicalSwitchPortUuid); try { final Type niciraListType = new TypeToken<NiciraNvpList<LogicalSwitchPort>>() { }.getType(); return restConnector.<NiciraNvpList<LogicalSwitchPort>> executeRetrieveObject(niciraListType, uri, params).getResults(); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } }
public List<LogicalRouterPort> findLogicalRouterPortByGatewayServiceUuid(final String logicalRouterUuid, final String l3GatewayServiceUuid) throws NiciraNvpApiException { final String uri = buildLogicalRouterElementUri(logicalRouterUuid, LPORT_PATH_SEGMENT); final Map<String, String> params = buildBasicParametersMap(WILDCARD_QUERY_PARAMETER); params.put(ATTACHMENT_GWSVC_UUID_QUERY_PARAMETER, l3GatewayServiceUuid); try { final Type niciraListType = new TypeToken<NiciraNvpList<LogicalRouterPort>>() { }.getType(); return restConnector.<NiciraNvpList<LogicalRouterPort>> executeRetrieveObject(niciraListType, uri, params).getResults(); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } }
public String findLogicalSwitchPortUuidByVifAttachmentUuid(final String logicalSwitchUuid, final String vifAttachmentUuid) throws NiciraNvpApiException { final String uri = buildLogicalSwitchElementUri(logicalSwitchUuid, LPORT_PATH_SEGMENT); final Map<String, String> params = buildBasicParametersMap(UUID_QUERY_PARAMETER); params.put(NiciraConstants.ATTACHMENT_VIF_UUID_QUERY_PARAMETER_NAME, vifAttachmentUuid); NiciraNvpList<LogicalSwitchPort> niciraList; try { final Type niciraListType = new TypeToken<NiciraNvpList<LogicalSwitchPort>>() { }.getType(); niciraList = restConnector.executeRetrieveObject(niciraListType, uri, params); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } final List<LogicalSwitchPort> lspl = niciraList.getResults(); final int listSize = lspl.size(); if (listSize != 1) { throw new NiciraNvpApiException("Expected 1 LogicalSwitchPort, but got " + listSize); } final LogicalSwitchPort lsp = lspl.get(0); return lsp.getUuid(); }
public List<NatRule> findNatRulesByLogicalRouterUuid(final String logicalRouterUuid) throws NiciraNvpApiException { final String uri = buildLogicalRouterElementUri(logicalRouterUuid, NAT_PATH_SEGMENT); final Map<String, String> params = buildBasicParametersMap(WILDCARD_QUERY_PARAMETER); try { final Type niciraListType = new TypeToken<NiciraNvpList<NatRule>>() { }.getType(); return restConnector.<NiciraNvpList<NatRule>> executeRetrieveObject(niciraListType, uri, params).getResults(); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } }