Junos Space SDK > Developer Guides > Junos Space Application Developer Guide > Programming with the Junos Space SDK > Reference Applications > GoogleMashupApp

Coding the View Devices Task

The list of devices displayed by the View Devices task is obtained through the Junos Space REST API URL: /api/space/device-management/devices. For example (returns have been added to the following for clarity):

{"devices":{"@size":"10","@uri":"/api/space/device-management/devices/","device":

[

{"@key":"196618", "@uri":"/api/space/device-management/devices/196618","deviceFamily":"junos-es","OSVersion":"10.2R2.10","platform":"SRX210-HM","serialNumber":"AD3009aa000a","connectionStatus":"down","ipAddr":"192.168.1.9","managedStatus":"Connecting","name":"Veera-Srx210hm-61.61"},

{"@key":"196621","@uri":"/api/space/device-management/devices/196621","deviceFamily":"junos-es","OSVersion":"10.2R2.10","platform":"SRX210-HM","serialNumber":"AD3009aa000b","connectionStatus":"down","ipAddr":"192.168.1.10","managedStatus":"Connecting","name":"Veera-Srx210hm-61.61"},

{"@key":"196624","@uri":"/api/space/device-management/devices/196624","deviceFamily":"junos-es","OSVersion":"10.2R2.10","platform":"SRX210-HM","serialNumber":"AD3009aa0009","connectionStatus":"down","ipAddr":"192.168.1.8","managedStatus":"Connecting","name":"Veera-Srx210hm-61.61"},

{"@key":"196630","@uri":"/api/space/device-management/devices/196630","deviceFamily":"junos","OSVersion":9.3,"platform":"M10","serialNumber":"JN10b8397b00","connectionStatus":"down","ipAddr":"192.168.1.6","managedStatus":"Connecting","name":"192.168.1.6"},

{"@key":"196634","@uri":"/api/space/device-management/devices/196634","deviceFamily":"junos","OSVersion":9.3,"platform":"EX","serialNumber":"JN10b8397afb","connectionStatus":"down","ipAddr":"192.168.1.5","managedStatus":"Connecting","name":"192.168.1.5"},

{"@key":"196638","@uri":"/api/space/device-management/devices/196638","deviceFamily":"junos","OSVersion":9.3,"platform":"EX","serialNumber":"JN10b8397afd","connectionStatus":"down","ipAddr":"192.168.1.3","managedStatus":"Connecting","name":"192.168.1.3"},

{"@key":"196642","@uri":"/api/space/device-management/devices/196642","deviceFamily":"junos","OSVersion":9.3,"platform":"EX","serialNumber":"JN10b8397afc","connectionStatus":"down","ipAddr":"192.168.1.4","managedStatus":"Connecting","name":"192.168.1.4"},

{"@key":"196646","@uri":"/api/space/device-management/devices/196646","deviceFamily":"junos","OSVersion":9.3,"platform":"M10","serialNumber":"JN10b8397afe","connectionStatus":"down","ipAddr":"192.168.1.2","managedStatus":"Connecting","name":"192.168.1.2"},

{"@key":"196650","@uri":"/api/space/device-management/devices/196650","deviceFamily":"junos","OSVersion":9.3,"platform":"M10","serialNumber":"JN10b8397b01","connectionStatus":"down","ipAddr":"192.168.1.7","managedStatus":"Connecting","name":"192.168.1.7"},

{"@key":"196651","@uri":"/api/space/device-management/devices/196651","deviceFamily":"junos","OSVersion":9.3,"platform":"M10","serialNumber":"JN10b8397aff","connectionStatus":"down","ipAddr":"192.168.1.1","managedStatus":"Connecting","name":"192.168.1.1"}

]

}} 

The /api/space/device-management/devices API does not return any location information for devices. To plot devices on Google Maps, you need to provide a Google Maps JavaScript API with location information in the form of latitude and longitude coordinates.

The GoogleMashupApp has a mashup REST service, /google-mashup-app/devices , which gets the devices information using /api/space/device-management/devices and adds location information to the device information. The interface and implementation for this service are in the GoogleMashupAppWebSvc module, GoogleMashupApp.java interface and GoogleMashupAppImpl.java respectively.

The /google-mashup-app/devices service contains the following API to return the list of devices with location information. This code is contained in the interface GoogleMashupApp.java:

@GET
@Path("/devices")
@Produces("application/vnd.net.juniper.space.googlemashup.device+json;version=1")
public Devices getDevices(@Context HttpServletRequest request);

Coding the GoogleMashupAppImpl Class

The constructor for the GoogleMashupAppImpl class creates an HttpClient object that contains the Space credentials and authentication type that will be used by the code that makes the actual REST call to Junos Space for device information:

credentials = new UsernamePasswordCredentials("super", "juniper123");
httpClient = new HttpClient();
httpClient.getState().setCredentials(AuthScope.ANY, credentials);
httpClient.getParams().setAuthenticationPreemptive(true);
clientExecutor = new ApacheHttpClientExecutor(httpClient);

The HTTP GET generated when the task is launched causes a GoogleMashupAppImpl object to be instantiated and getDevices(HttpServletRequest servletRequest) to be executed. This function creates a request using the /api/space/device-management/devices URL that returns the information for devices. The request contains a reference to the HttpClient object cited above, and the URI for the /api/space/device-management/devices REST API.

The request is executed in the request.get(String.class) method call. The response to get.request() is a json-format string containing the device data from Space. This string is parsed into device objects for each returned device, and each device is added to the devices collection that is returned by getDevices(). Latitude and longitude data is set for each device (dummy data generated using a random number generator). The rest of the class should be self-explanatory.


logger.warn("Base URL of the Server " + baseURL);
logger.warn("Executor instance " + clientExecutor);
ClientRequest request = new ClientRequest(baseURL + DEVICES_URL, clientExecutor);
ClientResponse response = request.get(String.class);

if (SUCCESS != response.getStatus()) {
  logger.error("Could not get the devices: " + response.getStatus());
  logger.error("Response " + response.getEntity());
} 

String json = response.getEntity();

The @Produces annotation takes the JSON-format devices collection returned by getDevices() that contains the collection of devices.

The response from the above REST API looks like this (returns have been added to the following for clarity):


  {"devices":

    [{"name":"Veera-Srx210hm-61.61","id":196618,"status":"down","ipAddress":"192.168.1.9","platform":"SRX210-HM","image":"m-generic-normal.png","family":"junos-es","latitude":48.574966,"longitude":49.91826},

     {"name":"Veera-Srx210hm-61.61","id":196621,"status":"down","ipAddress":"192.168.1.10","platform":"SRX210-HM","image":"m-generic-normal.png","family":"junos-es","latitude":132.21037,"longitude":62.668594},

     {"name":"Veera-Srx210hm-61.61","id":196624,"status":"down","ipAddress":"192.168.1.8","platform":"SRX210-HM","image":"m-generic-normal.png","family":"junos-es","latitude":18.77797,"longitude":43.67717},

     {"name":"192.168.1.6","id":196630,"status":"down","ipAddress":"192.168.1.6","platform":"M10","image":"m-generic-normal.png","family":"junos","latitude":6.9841313,"longitude":32.116043},

     {"name":"192.168.1.5","id":196634,"status":"down","ipAddress":"192.168.1.5","platform":"EX","image":"ex-generic-normal.png","family":"junos","latitude":138.69965,"longitude":45.49969},

     {"name":"192.168.1.3","id":196638,"status":"down","ipAddress":"192.168.1.3","platform":"EX","image":"ex-generic-normal.png","family":"junos","latitude":154.41362,"longitude":7.118446},

     {"name":"192.168.1.4","id":196642,"status":"down","ipAddress":"192.168.1.4","platform":"EX","image":"ex-generic-normal.png","family":"junos","latitude":26.911917,"longitude":43.738136},

     {"name":"192.168.1.2","id":196646,"status":"down","ipAddress":"192.168.1.2","platform":"M10","image":"m-generic-normal.png","family":"junos","latitude":76.10585,"longitude":21.3949},

     {"name":"192.168.1.7","id":196650,"status":"down","ipAddress":"192.168.1.7","platform":"M10","image":"m-generic-normal.png","family":"junos","latitude":37.83491,"longitude":13.452257},

     {"name":"192.168.1.1","id":196651,"status":"down","ipAddress":"192.168.1.1","platform":"M10","image":"m-generic-normal.png","family":"junos","latitude":37.584274,"longitude":80.33626}

     ]} 

}

Understanding the UI code

The Space GUI framework builds UI pages by writing a function in a Java Script file. The function and Java Script file is specified in the module.xml file.

For example, the UI Configuration parameter should as mentioned below for the View Devices task:

  1. Top Level JS File - The Top level *.JS location for ViewDevices Task is /GooogleMashupAppWeb/js/tasks/viewdevices/ViewDevices.js
  2. Ext.ns - The name space is Js.viewdevices.

In this case the framework will use these values and attempt to load the base javascript file from the location

/GoogleMashupAppWeb/js/tasks/viewdevicestask/ViewDevicesTask.js

The framework combines the value of basepath and className to formulate the base file name ViewDevicesTask.js.

The file ViewDevicesTask.js must implement the three life cycle methods. That is, initialize, show, and destroy as required by the GUI framework.