How to ... Create an HCS Assurance APIs Client Application
This sample application demonstrates how to create a Prime Central for HCS web service consumer client using the Apache CFX framework. The CFX framework can be used generate stub classes from the Prime Central Web Service WSDL.
You can then use these generated classes to connect to the Cisco Prime Central Web Service interfaces. You can download the code for this example. The following article walks through how to generate the consumer classes and how to create a web client that performs the following tasks:
- Authenticate and retrieve a session token
- Retrieve the number of archived events of type 1
Before You begin
To complete this exercise, you will need access to the following tools.
- Access to an HCS deployment
- The Prime Central Admin username and password
- The Apache CFX framework (http://cxf.apache.org)
Download and Install the CXF Framework
- Go to http://cxf.apache.org and download and install CFX.
- After, you have downloaded CFX, you may need to set up your environment to use CFX. You can do so by entering the following on the command line. Use the version of CFX that corresponds to the version you downloaded.
1
2
3
4
5
6
7
|
< strong style = "font:'Courier
New', Courier, monospace; font-size:14px;" > < table class = "tablesorter"
cellpadding = "0"
cellspacing = "0" > < tbody > < tr > < td
style = "border-right-color:#6ce26c;
border-right-width:thick; border-left-style:none;
border-bottom-style:none; border-top-style:none
" >< span style = "color:#999;" >1</ span > </ td >
< td
style = "border-right-style:
none; border-left-style:none; border-bottom-style:none;
border-top-style:none; " >$< span style = "color:#f952ac;" >export</ span >CXF_HOME=/Your_CXF_Download_Location/apache-cxf-2.X.X/</ td ></ tr ></ tbody ></ table ></ strong >
|
Generating the consumer classes
In this example, we will use the CFX framework to generate classes from the Prime Central Web Service WSDL. We will then use these generated classes to call the APIs.
Get the WSDL's
For this application, we need get the Event Management wsdl from the Prime Central deployment.
Event Management WSDL
https://your-prime-central-server:9090/EventManagement?wsdl
- Create a directory for your project.
- Create a directory inside of your project directory for the wsdl files.
- Save all of the wsdl files in the wsdl directory.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<strong
style= "font:'Courier New',
Courier, monospace; font-size:14px;" > <table
class= "tablesorter" cellpadding= "0" cellspacing= "0" > <tbody>
< tr > <td
style= "width: 10px;
border-right-color:#6ce26c; border-right-width:thick;
border-left-style: none; border-bottom-style:none;
border-top-style:none " ><span
style= "color:#999;" >1< /span > <span
style= "color:#999;" >2< /span > <span
style= "color:#999;" >3< /span > <span
style= "color:#999;" >4< /span > <span
style= "color:#999;" >5< /span > <span
style= "color:#999;" >6< /span > < /td > <td
style= "border-right-style:
none; border-left-style:none; border-bottom-style:none;
border-top-style:none; border-left-margin: -20px;" >$ <span style= "color:#f952ac;" > pwd < /span >
/home/alice $
<span style= "color:#f952ac;" > mkdir < /span >
myProject $
<span style= "color:#f952ac;" > cd < /span >
myProject $
<span style= "color:#f952ac;" > mkdir < /span >
wsdl $
<span style= "color:#f952ac;" > cd < /span >
wsdl< /td >< /tr >< /tbody >< /table >< /strong > |
Generate the Classes
Create a directory for the generated classes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< strong style = "font:'Courier
New', Courier, monospace; font-size:14px;" > < table class = "tablesorter"
cellpadding = "0"
cellspacing = "0" > < tbody > < tr > < td
style = "width:
10px; border-right-color:#6ce26c; border-right-width:thick;
border-left-style:none; border-bottom-style:none;
border-top-style:none " >< span style = "color:#999;" >1</ span > < span style = "color:#999;" >2</ span > < span style = "color:#999;" >3</ span > < span style = "color:#999;" >4</ span > < span style = "color:#999;" >5</ span > </ td >
< td
style = "border-right-style:
none; border-left-style:none; border-bottom-style:none;
border-top-style:none;" >$< span style = "color:#f952ac;" > pwd</ span > /home/alice/myProject
$< span style = "color:#f952ac;" > mkdir</ span >
autogen $< span style = "color:#f952ac;" > mkdir</ span >
autogen/src $< span style = "color:#f952ac;" > mkdir</ span >
autogen/build</ td ></ tr ></ tbody ></ table ></ strong > |
Next, we will use wsdl2java to generate the classes.
You may hit a java.lang.OutOfMemoryError while generating the code. To fix this you will need to increase the maximum heap size of wsdl2java. To increase the heap size do the following:
- open /Your_CXF_Download_Location/apache-cxf-2.X.X/bin/wsdl2java in an text editor.
- Change
$JAVA_HOME/bin/java -Xmx128M <SNIP>
- to
$JAVA_HOME/bin/java -Xmx256M <SNIP>
- Close and save the wsdl2java file.
Run the following commands to generate the stub classes. These commands make take a few minutes to complete. After they have finished, you should see the generated classes in the autogen/src and autogen/build directories.
1
2
3
4
5
6
7
8
9
10
11
|
< strong style = "font:'Courier
New', Courier, monospace; font-size:14px;" > < table class = "tablesorter"
cellpadding = "0"
cellspacing = "0" > < tbody > < tr > < td
style = "border-right-color:#6ce26c;
border-right-width:thick; border-left-style:none;
border-bottom-style:none; border-top-style:none
" >< span style = "color:#999;" >1</ span > < span style = "color:#999;" >2</ span > < span style = "color:#999;" >3</ span > </ td >
< td
style = "border-right-style:
none; border-left-style:none; border-bottom-style:none;
border-top-style:none;" >$
< span style = "color:#f952ac;" > pwd</ span > /home/alice/myProject
$
/Your_CXF_Download_Location/apache-cxf-2.X.X/bin/wsdl2java -classdir
autogen/build/ -frontend jaxws21 -d autogen/src/ -compile
./wsdl/EventManagement.wsdl</ td ></ tr ></ tbody ></ table ></ strong > |
Create the Web Client
Next, we will add the code that connects to the Prime Central Web Services and uses some of the APIs.
Create a directory for the source files
First, we will create a directory to contain our business logic source.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< strong style = "font:'Courier
New', Courier, monospace; font-size:14px;" > < table class = "tablesorter"
cellpadding = "0"
cellspacing = "0" > < tbody > < tr > < td
style = "width:
10px; border-right-color:#6ce26c; border-right-width:thick;
border-left-style:none; border-bottom-style:none;
border-top-style:none " >< span style = "color:#999;" >1</ span > < span style = "color:#999;" >2</ span > < span style = "color:#999;" >3</ span > < span style = "color:#999;" >4</ span > < span style = "color:#999;" >5</ span > </ td >
< td
style = "border-right-style:
none; border-left-style:none; border-bottom-style:none;
border-top-style:none;" >$
< span style = "color:#f952ac;" > pwd</ span > /home/alice/myProject
$
< span style = "color:#f952ac;" > mkdir</ span >
src $
< span style = "color:#f952ac;" > mkdir</ span >
src/com $
< span style = "color:#f952ac;" > mkdir</ span >
src/com/example </ td ></ tr ></ tbody ></ table ></ strong > |
We will also create a directory for the compiled business logic class files.
1
2
3
4
5
6
7
8
9
10
11
|
< strong style = "font:'Courier
New', Courier, monospace; font-size:14px;" > < table class = "tablesorter"
cellpadding = "0"
cellspacing = "0" > < tbody > < tr > < td
style = "width:
10px; border-right-color:#6ce26c; border-right-width:thick;
border-left-style:none; border-bottom-style:none;
border-top-style:none " >< span style = "color:#999;" >1</ span > < span style = "color:#999;" >2</ span > < span style = "color:#999;" >3</ span > </ td >
< td
style = "border-right-style:
none; border-left-style:none; border-bottom-style:none;
border-top-style:none;" >$
< span style = "color:#f952ac;" > pwd</ span > /home/alice/myProject
$
< span style = "color:#f952ac;" > mkdir</ span >
build</ td ></ tr ></ tbody ></ table ></ strong > |
Create a Connector
To complete the web client, we are going to create three classes. You can download these sample classes.
- HCSConnector.java - a simple base connector client that helps you connect to to the Prime Central SOAP Interface. Handles setting up the remote host, credentials, logging and certificate management.
You should store this file as /home/alice/myProject/src/com/example/HCSConnector.java. - HCSConnectorImpl.java - extends the HCSConnector base client and implements a version specific Prime Central Web Service Connector. Uses settings from the base class and the CXF autogenerated stubs to connect to the Prime Central SOAP interface.
You should store this file as /home/alice/myProject/src/com/example/HCSConnectorImpl.java. - AssuranceWebClient.java - uses the version specific connector and automates a sample workflow.
You should store this file as /home/alice/myProject/src/com/example/AssuranceWebClient.java.
Using the Prime Central Web Service APIs
The AssuranceWebClient.java demonstrates how to implement the following workflow:
- Authenticate and retrieve a session token
- Retrieve the number of archived events of type 1 (active)
You can see the code that performs this workflow in the following segment.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
< strong style = "font:'Courier
New', Courier, monospace; font-size:14px;" > < table class = "tablesorter"
cellpadding = "0"
cellspacing = "0" > < tbody > < tr > < td
style = "border-right-color:#6ce26c;
border-right-width:thick; border-left-style:none;
border-bottom-style:none; border-top-style:none
" >< span style = "color:#999;" >1</ span > < span style = "color:#999;" >2</ span > < span style = "color:#999;" >3</ span > < span style = "color:#999;" >4</ span > < span style = "color:#999;" >5</ span > < span style = "color:#999;" >6</ span > < span style = "color:#999;" >7</ span > < span style = "color:#999;" >8</ span > < span style = "color:#999;" >9</ span > < span style = "color:#999;" >10</ span > < span style = "color:#999;" >11</ span > < span style = "color:#999;" >12</ span > < span style = "color:#999;" >13</ span > < span style = "color:#999;" >14</ span > < span style = "color:#999;" >15</ span > < span style = "color:#999;" >16</ span > < span style = "color:#999;" >17</ span > < span style = "color:#999;" >18</ span > < span style = "color:#999;" >19</ span > < span style = "color:#999;" >20</ span > < span style = "color:#999;" >21</ span > < span style = "color:#999;" >22</ span > < span style = "color:#999;" >23</ span > < span style = "color:#999;" >24</ span > < span style = "color:#999;" >25</ span > < span style = "color:#999;" >26</ span > < span style = "color:#999;" >27</ span > < span style = "color:#999;" >28</ span > < span style = "color:#999;" >29</ span > </ td >
< td
style = "border-right-style:
none; border-left-style:none; border-bottom-style:none;
border-top-style:none;" >< span style = "color:#38a963;" >//Perform a Sample work
flow</ span > < span style = "color:#009;" >public void</ span >
doSampleWorkFlow () < span
style = "color:#009;" >throws</ span >
com.cisco.hcm.fronthandler.EventManagementException_Exception{
< span style = "color:#38a963;" >//Step 1: Authenticate</ span > String
sessionToken = authenticate(); < span style = "color:#38a963;" >//Step 2: Get number of archived events</ span > < span style = "color:#009;" >int</ span > numberArchivedEvents =
getNumArchivedEvents(sessionToken); < span style = "color:#38a963;" >//Step 3: Display number
events</ span > displayEvents(numberArchivedEvents);
}
< span style = "color:#009;" >private</ span >
String authenticate() < span style = "color:#009;" >throws</ span >
com.cisco.hcm.fronthandler.EventManagementException_Exception
{ String
token = eventManagementWebService.authenticate(user, pwd);
< span style = "color:#009;" >return</ span >
token; }
< span style = "color:#009;" >private</ span >
Integer getNumArchivedEvents(String token) throws
com.cisco.hcm.fronthandler.EventManagementException_Exception{
String
whereClause = "< span
style = "color:#09F;" >type=1"</ span >; Integer
numEvents =
eventManagementWebService.getArchivedEventCount(whereClause,
token); < span style = "color:#009;" >return</ span >
numEvents; }
< span style = "color:#009;" >private void</ span >
displayEvents(Integer numEvents){ System.out.println("< span style = "color:#09F;" >Number of Archived Events (type=1)= "</ span > +
numEvents); }</ td ></ tr ></ tbody ></ table ></ strong > |
Build the web client
The sample code download includes an ant build.xml file that you can use to build this project.
The build.xml file performs the following tasks:
- Compile the autogenerated code stubs.
- Archive all the autogenerated code and class files into a jar file (call this jar file "HCSWebClientGeneratedClasses.jar").
- Compile the business logic class files.
- Archive the business logic code and the class files into a jar file. (This jar file will depend on the HCSWebClientGeneratedClasses.jar, and will be called AssuranceWebClient.jar.)
- Archive the downloaded WSDL files.
- Use the main class of AssuranceWebClient as the class that is executed when the jar is run.
You can store the build.xml file from the sample code download in /home/alice/myProject/build.xml.
Be sure to set your CXF_HOME environment variable as follows:
1
2
3
4
5
6
7
8
|
< strong style = "font:'Courier
New', Courier, monospace; font-size:14px;" > < table class = "tablesorter"
cellpadding = "0"
cellspacing = "0" > < tbody > < tr > < td
style = "width:
10px; border-right-color:#6ce26c; border-right-width:thick;
border-left-style:none; border-bottom-style:none;
border-top-style:none " >< span style = "color:#999;" >1</ span > </ td >
< td
style = "border-right-style:
none; border-left-style:none; border-bottom-style:none;
border-top-style:none;" >$
< span style = "color:#f952ac;" >export</ span >
CXF_HOME=/Your_CXF_Download_Location/apache-cxf-2.X.X/
</ td ></ tr ></ tbody ></ table ></ strong > |
You can build the client as follows:
1
2
3
4
5
6
7
8
9
10
11
12
|
< strong style = "font:'Courier
New', Courier, monospace; font-size:14px;" > < table class = "tablesorter"
cellpadding = "0"
cellspacing = "0" > < tbody > < tr > < td
style = "width:
10px; border-right-color:#6ce26c; border-right-width:thick;
border-right-margin: 5px;border-left-style:none;
border-bottom-style:none; border-top-style:none
" >< span style = "color:#999;" >1</ span > < span style = "color:#999;" >2</ span > < span style = "color:#999;" >3</ span > </ td >
< td
style = "border-right-style:
none; border-left-style:none; border-bottom-style:none;
border-top-style:none;" >$
< span style = "color:#f952ac;" >pwd</ span > /home/alice/myProject
$
ant all </ td ></ tr ></ tbody ></ table ></ strong > |
Run the web client
To run the web client, use the following command. Enter the appropriate server address, username and password for your system.
1
2
3
4
5
6
7
8
9
10
11
12
|
< strong style = "font:'Courier
New', Courier, monospace; font-size:14px;" > < table class = "tablesorter"
cellpadding = "0"
cellspacing = "0" > < tbody > < tr > < td
style = "width:
10px; border-right-color:#6ce26c; border-right-width:thick;
border-left-style:none; border-bottom-style:none;
border-top-style:none " >< span style = "color:#999;" >1</ span > < span style = "color:#999;" >2</ span > < span style = "color:#999;" >3</ span > </ td >
< td
style = "border-right-style:
none; border-left-style:none; border-bottom-style:none;
border-top-style:none;" >$
< span style = "color:#f952ac;" >pwd</ span > /home/alice/myProject
$
java -Dhcs=< span style = "color:#09F;" >"my-hcs-box"</ span >
-Dusername=< span style = "color:#09F;" >"my_username"</ span >
-Dpassword=< span style = "color:#09F;" >"my_password"</ span > -jar
./jar/WorkflowHCSWebClient.jar </ td ></ tr ></ tbody ></ table ></ strong > |