Sample Code: Device Discovery

  1. Create a Queue with "queueName":
public String createQueue(String queueName) throws Exception {
	String mediatype = "application/hornetq.jms.queue+xml";
	Credentials credentials = new UsernamePasswordCredentials("uname", "password");
	HttpClient httpClient = new HttpClient();
	httpClient.getState().setCredentials(AuthScope.ANY, credentials);
	httpClient.getParams().setAuthenticationPreemptive(true);
	ClientExecutor clientExecutor = new ApacheHttpClientExecutor(httpClient);
	ClientRequest request = new ClientRequest("/api/space/hornet-q/queues", clientExecutor);

	String requestBody = "<queue name=\"" + queueName + "\">" +
			"<durable>true</durable>" +
			"</queue>";
	
	request.body(mediatype, requestBody);
	ClientResponse<String> response = request.post(String.class);
	int status = response.getStatus();
	String queueURL = response.getHeaders().get("Location");
	if (status == 201) // OK!
		System.out.println("QUEUE created : " + queueURL);
	else
		throw new RuntimeException("QUEUE creation failed!");
	
	return queueURL;
}

  1. Start the Discovery Process with queueURL returned by createQueue():
public void startDiscovery(String queueURL) {
	Credentials credentials = new
	UsernamePasswordCredentials("super", "juniper123"); 
	HttpClient httpClient = new HttpClient();
	httpClient.getState().setCredentials(AuthScope.ANY,
			credentials);
	httpClient.getParams().setAuthenticationPreemptive(true);
	ClientExecutor clientExecutor = new
			ApacheHttpClientExecutor(httpClient); 
	String mediatype =
			"application/vnd.net.juniper.space.device-management.discover-devices+xml;version=1;charset=UTF-8";

	ClientRequest request = new ClientRequest("/api/space/device-management/discover-devices?queue=" + queueURL, clientExecutor); 	
	request.header("Content-Type", mediatype);
			
	String input = 
			"<discover-devices> " +
				"<ipRangeDiscoveryTarget> " +
					"<exclude>false</exclude> " +
					"<lowerIp>192.168.21.9</lowerIp> " +
					"<upperIp> 192.168.21.10</upperIp> " +
				"</ipRangeDiscoveryTarget> " +
				"<usePing>false</usePing> " +
				"<useSnmp>false</useSnmp> " +
				"<snmpV2CSetting> " +
					"<communityName>testCommunityName</communityName>" + 
				"</snmpV2CSetting> " +
				"<manageDiscoveredSystemsFlag>true</manageDiscoveredSystemsFlag>" + 
				"<sshCredential> " +
				"<userName>root</userName> " +
				"<password>juniper</password> " +
				"</sshCredential> " + 
			"</discover-devices>";
			
	request.body(mediatype, input); ClientResponse<Task>
	response = request.post(Task.class); 
	int status = response.getStatus(); 
	if (status == 202) // OK!
		System.out.println("JobID : " + response.getEntity().getId());
	else 
		System.out.println("Discover-devices Failed......");
}

  1. Poll the Queue:
public void pollQueue(String queueURL) {
	ClientRequest request = new ClientRequest(queueURL); 
	ClientResponse res = request.get(); 
	String pullConsumers = (String) (((ArrayList) res.getHeaders().get("msg-pull-consumers")).get(0));
	request = new ClientRequest(pullConsumers); 
	res = request.post(); 
	String ackNext = (String) ((ArrayList) res.getHeaders().get("msg-consume-next")).get(0);
	while (true){ 
		System.out.println("Waiting..."); request = new
		ClientRequest(ackNext); 
		res = request.header("Accept-Wait", "10").post(); 
		if (res.getStatus() == 503) {
			System.out.println("Timeout..."); 
			ackNext = (String) ((ArrayList) res.getHeaders().get("msg-consume-next")).get(0);
		} else if (res.getStatus() == 200) { 
			String result = (String)res.getEntity(String.class); 
			System.out.println(result);
			if(result.contains("<data>")) { // Parse the data tag. Can use XPath 
				break; 
			} 
			ackNext = (String) ((ArrayList)res.getHeaders().get("msg-consume-next")).get(0); 
		} else {
			throw new RuntimeException("Failure! " + res.getStatus()); 
		}
	}
}