public <T> T executeRetrieveObject(final Type returnObjectType, final String path) throws CloudstackRESTException { return executeRetrieveObject(returnObjectType, path, new HashMap<String, String>()); }
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()); } }
/** * 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(); }
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<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> 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()); } }
@Test public void testCustomDeserializerForCustomLists() throws Exception { final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); when(response.getEntity()).thenReturn(new StringEntity("{results: [{field : \"SomeValue\"}], results_count: 1}")); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final Class<? extends CollectionType> clazzListOfTestPojo = new ObjectMapper().getTypeFactory().constructCollectionType(List.class, TestPojo.class).getClass(); final RESTServiceConnector connector = new RESTServiceConnector.Builder() .client(restClient) .classToDeserializerEntry(clazzListOfTestPojo, new CustomListDeserializer<TestPojoDeserializer>()) .build(); connector.executeRetrieveObject(TestPojo.class, "/somepath"); }
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()); } }
@Test(expected = JsonParseException.class) public void testCustomDeserializerTypeMismatch() throws Exception { final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); when(response.getEntity()).thenReturn(new StringEntity("[{somethig_not_type : \"WrongType\"}]")); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final RESTServiceConnector connector = new RESTServiceConnector.Builder() .client(restClient) .classToDeserializerEntry(TestPojo.class, new TestPojoDeserializer()) .build(); connector.executeRetrieveObject(TestPojo.class, "/somepath"); }
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(); }
@Test public void testExecuteRetrieveObject() throws Exception { final TestPojo existingObject = new TestPojo(); existingObject.setField("existingValue"); final String newObjectJson = gson.toJson(existingObject); final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getEntity()).thenReturn(new StringEntity(newObjectJson)); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final RESTServiceConnector connector = new RESTServiceConnector.Builder().client(restClient).build(); final TestPojo object = connector.executeRetrieveObject(TestPojo.class, "/somepath"); assertThat(object, notNullValue()); assertThat(object, equalTo(existingObject)); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("GET"), any(HttpClientContext.class)); verify(response).close(); }
@Test public void testExecuteRetrieveObjectWithParameters() throws Exception { final TestPojo existingObject = new TestPojo(); existingObject.setField("existingValue"); final String newObjectJson = gson.toJson(existingObject); final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getEntity()).thenReturn(new StringEntity(newObjectJson)); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final RESTServiceConnector connector = new RESTServiceConnector.Builder().client(restClient).build(); final TestPojo object = connector.executeRetrieveObject(TestPojo.class, "/somepath", DEFAULT_TEST_PARAMETERS); assertThat(object, notNullValue()); assertThat(object, equalTo(existingObject)); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("GET"), any(HttpClientContext.class)); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestQueryMatcher.aQueryThatContains("arg2=val2"), any(HttpClientContext.class)); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestQueryMatcher.aQueryThatContains("arg1=val1"), any(HttpClientContext.class)); verify(response).close(); }
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()); } }
public <T> T executeRetrieveObject(final Type returnObjectType, final String path) throws CosmicRESTException { return executeRetrieveObject(returnObjectType, path, new HashMap<>()); }
@Test(expected = JsonParseException.class) public void testCustomDeserializerTypeMismatch() throws Exception { final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); when(response.getEntity()).thenReturn(new StringEntity("[{somethig_not_type : \"WrongType\"}]")); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final RESTServiceConnector connector = new RESTServiceConnector.Builder() .client(restClient) .classToDeserializerEntry(TestPojo.class, new TestPojoDeserializer()) .build(); connector.executeRetrieveObject(TestPojo.class, "/somepath"); }
@Test public void testExecuteRetrieveObject() throws Exception { final TestPojo existingObject = new TestPojo(); existingObject.setField("existingValue"); final String newObjectJson = gson.toJson(existingObject); final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getEntity()).thenReturn(new StringEntity(newObjectJson)); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final RESTServiceConnector connector = new RESTServiceConnector.Builder().client(restClient).build(); final TestPojo object = connector.executeRetrieveObject(TestPojo.class, "/somepath"); assertThat(object, notNullValue()); assertThat(object, equalTo(existingObject)); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("GET"), any(HttpClientContext.class)); verify(response).close(); }
@Test public void testCustomDeserializerForCustomLists() throws Exception { final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); when(response.getEntity()).thenReturn(new StringEntity("{results: [{field : \"SomeValue\"}], results_count: 1}")); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final Class<? extends CollectionType> clazzListOfTestPojo = new ObjectMapper().getTypeFactory().constructCollectionType(List.class, TestPojo.class).getClass(); final RESTServiceConnector connector = new RESTServiceConnector.Builder() .client(restClient) .classToDeserializerEntry(clazzListOfTestPojo, new CustomListDeserializer<TestPojoDeserializer>()) .build(); connector.executeRetrieveObject(TestPojo.class, "/somepath"); }
@Test public void testExecuteRetrieveObjectWithParameters() throws Exception { final TestPojo existingObject = new TestPojo(); existingObject.setField("existingValue"); final String newObjectJson = gson.toJson(existingObject); final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getEntity()).thenReturn(new StringEntity(newObjectJson)); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final RESTServiceConnector connector = new RESTServiceConnector.Builder().client(restClient).build(); final TestPojo object = connector.executeRetrieveObject(TestPojo.class, "/somepath", DEFAULT_TEST_PARAMETERS); assertThat(object, notNullValue()); assertThat(object, equalTo(existingObject)); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("GET"), any(HttpClientContext.class)); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestQueryMatcher.aQueryThatContains("arg2=val2"), any(HttpClientContext.class)); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestQueryMatcher.aQueryThatContains("arg1=val1"), any(HttpClientContext.class)); verify(response).close(); }