Subscribing to ANC Endpoint Topic
The pxGrid client subscribes to the ANC topic so they can obtain ANC information in real-time. For more information on the ANC Configuration topic, please see: ANC Configuration
Code Step-Through
The //Subscribe handler class this will print the contents of the session that is opened by STOMP.
Main parses the SampleConfiguration config file. The sample config object contains the pxGrid client connection parameters such as the pxGrid hostname, identity filename (.jks file) and trusted keystore filesname (.jks file) or pre-share keys if implemented.
For //Account Activate, we wait 60 seconds for the account to be enabled. The ISE admin need to approve the pxGrid client account. We retrieve the pxGrid controller version.
For // pxGrid ServiceLookup for session service, we lookup the session service, com.cisco.ise.session, since we are interested in obtaining the session information. The session service can be found:Github. This returns a list of the ISE nodes that are publishing the restBaseURL that will be used for WebSockets REST API calls.
For // Use first service. Note that ServiceLookup randomize ordering of services. The pubsub service provides a list of ISE pxGrid nodes, if you have Active/Active.
For example if you have (3) ISE pxGrid nodes, the value is randomized so you will only connect to one pxGrid node. However the service will be shared across all three ISE pxGrid nodes, this distributes the load.
We get the properties for “wsPubService” and “sessionTopic” service names. All the services use WebSockets Pubsub service name, “com.cisco.ise.pubsub” and here we interested in the sessionTopic, “com.cisco.ise.config.anc".
For //pxGrid service Lookup for pubsub service, this provides us with the ISE pxgrid node the publishes the session service
For //Use first service, we get pubsub service which resturns the “wsUrl” or WebSockets URL value
For //pxGrid get AccessSecret, we get the accessecret from the ISE pxGrid node containing the pubsub service.
For //WebSocket config, we get the credentials
For //WebSocket connect, we first make a WebSockets connection and then create a STOMP over WebSockets connection
For //Stompconnect, the pxGrid client connects
For //Subscribe, the pxGrid client subscribes to the session topic over STOMP
For //Give time for connection to establish before prompt disconnect. Once the endpoint disconnects, the ID “ID-123”, which can be any value, provides a receipt. The ISE pxGrid node will send back an acknowledgement to say that the pxGrid client has disconnected
JAVA Sample Code
package com.cisco.pxgrid.samples.ise;
import java.net.URI;
import org.apache.commons.cli.ParseException;
import org.glassfish.tyrus.client.ClientManager;
import org.glassfish.tyrus.client.ClientProperties;
import org.glassfish.tyrus.client.SslEngineConfigurator;
import org.glassfish.tyrus.client.auth.Credentials;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cisco.pxgrid.samples.ise.model.AccountState;
import com.cisco.pxgrid.samples.ise.model.Service;
/**
* Demonstrates how to subscribe to ANC Topic
*/
public class ANCSubscribe {
private static Logger logger = LoggerFactory.getLogger(ANCSubscribe.class);
// Subscribe handler class
private static class SessionHandler implements StompSubscription.Handler {
@Override
public void handle(StompFrame message) {
logger.info("Content={}", new String(message.getContent()));
}
}
public static void main(String[] args) throws Exception {
// Parse arguments
SampleConfiguration config = new SampleConfiguration();
try {
config.parse(args);
} catch (ParseException e) {
config.printHelp("SessionSubscribe");
System.exit(1);
}
// AccountActivate
PxgridControl control = new PxgridControl(config);
while (control.accountActivate() != AccountState.ENABLED) {
Thread.sleep(60000);
}
logger.info("pxGrid controller version={}", control.getControllerVersion());
// pxGrid ServiceLookup for session service
Service[] services = control.serviceLookup("com.cisco.ise.config.anc");
if (services.length == 0) {
logger.info("Session service unavailabe");
return;
}
// Use first service. Note that ServiceLookup randomize ordering of services
Service sessionService = services[0];
String wsPubsubServiceName = sessionService.getProperties().get("wsPubsubService");
String statusTopic = sessionService.getProperties().get("statusTopic");
logger.info("wsPubsubServiceName={} sessionTopic={}", wsPubsubServiceName, statusTopic);
// pxGrid ServiceLookup for pubsub service
services = control.serviceLookup(wsPubsubServiceName);
if (services.length == 0) {
logger.info("Pubsub service unavailabe");
return;
}
// Use first service
Service wsPubsubService = services[0];
String wsURL = wsPubsubService.getProperties().get("wsUrl");
logger.info("wsUrl={}", wsURL);
// pxGrid get AccessSecret
String secret = control.getAccessSecret(wsPubsubService.getNodeName());
// WebSocket config
ClientManager client = ClientManager.createClient();
SslEngineConfigurator sslEngineConfigurator = new SslEngineConfigurator(config.getSSLContext());
client.getProperties().put(ClientProperties.SSL_ENGINE_CONFIGURATOR, sslEngineConfigurator);
client.getProperties().put(ClientProperties.CREDENTIALS,
new Credentials(config.getNodeName(), secret.getBytes()));
// WebSocket connect
StompPubsubClientEndpoint endpoint = new StompPubsubClientEndpoint();
URI uri = new URI(wsURL);
javax.websocket.Session session = client.connectToServer(endpoint, uri);
// STOMP connect
endpoint.connect(uri.getHost());
// Subscribe
StompSubscription subscription = new StompSubscription(statusTopic, new SessionHandler());
endpoint.subscribe(subscription);
// Give time for connection to establish before prompt
Thread.sleep(1000);
SampleHelper.prompt("press <enter> to disconnect...");
// STOMP disconnect
endpoint.disconnect("ID-123");
// Wait for disconnect receipt
Thread.sleep(3000);
// Websocket close
session.close();
}
}
Output
23:46:15.799 [main] INFO com.cisco.pxgrid.samples.ise.PxgridControl - ServiceLookup response={"services":[{"name":"com.cisco.ise.config.anc","nodeName":"ise-admin-ise24fc3","properties":{"wsPubsubService":"com.cisco.ise.pubsub","restBaseUrl":"https://ise24fc3.lab10.com:8910/pxgrid/ise/config/anc","statusTopic":"/topic/com.cisco.ise.config.anc.status"}}]}
23:46:15.799 [main] INFO com.cisco.pxgrid.samples.ise.ANCSubscribe - wsPubsubServiceName=com.cisco.ise.pubsub sessionTopic=/topic/com.cisco.ise.config.anc.status
23:46:15.800 [main] INFO com.cisco.pxgrid.samples.ise.PxgridControl - ServiceLookup request={"name":"com.cisco.ise.pubsub"}
23:46:15.815 [main] INFO com.cisco.pxgrid.samples.ise.PxgridControl - ServiceLookup response={"services":[{"name":"com.cisco.ise.pubsub","nodeName":"ise-pubsub-ise24fc3","properties":{"wsUrl":"wss://ise24fc3.lab10.com:8910/pxgrid/ise/pubsub"}}]}
23:46:15.815 [main] INFO com.cisco.pxgrid.samples.ise.ANCSubscribe - wsUrl=wss://ise24fc3.lab10.com:8910/pxgrid/ise/pubsub
23:46:15.819 [main] INFO com.cisco.pxgrid.samples.ise.PxgridControl - AccessSecret request={"peerNodeName":"ise-pubsub-ise24fc3"}
23:46:15.832 [main] INFO com.cisco.pxgrid.samples.ise.PxgridControl - AccessSecret response={"secret":"th0ehKVMKdfnkfxM"}
23:46:17.442 [Grizzly(1)] INFO com.cisco.pxgrid.samples.ise.StompPubsubClientEndpoint - WS onOpen
23:46:17.471 [main] INFO com.cisco.pxgrid.samples.ise.StompPubsubClientEndpoint - STOMP CONNECT host=ise24fc3.lab10.com
23:46:17.476 [main] INFO com.cisco.pxgrid.samples.ise.StompPubsubClientEndpoint - STOMP SUBSCRIBE topic=/topic/com.cisco.ise.config.anc.status
23:46:17.478 [Grizzly(2)] INFO com.cisco.pxgrid.samples.ise.StompPubsubClientEndpoint - STOMP CONNECTED version=1.2
press <enter> to disconnect...
23:46:54.956 [Grizzly(1)] INFO com.cisco.pxgrid.samples.ise.ANCSubscribe - Content={"operationId":"ise24fc3.lab10.com:5","macAddress":"00:0E:C6:8F:B4:9B","status":"SUCCESS"}
What you see in ISE
Select Administration->pxGrid Services->Web Clients
