Goal

This tutorial demonstrates the Location Service Set functionality - how to get, set the location information and add listeners for location change events.

NOTE: Available on IOS-XE (ASR1K) only.

Tutorial Code

The code used in this tutorial is available in the LocationTutorial.py file located under <SDK Location>/python/tutorials/location/LocationTutorial.py.

Requirements/Prerequisites

These steps assume the application can connect properly to a network element. Please see the Connecting to a Network Element tutorial for information on how to make the initial connection.

Steps In Detail

Civic Location

Create a civic location object.

civic_location = CivicLocation(CivicType.LOCATION_CIVIC_POSTAL_CODE, '95035')

Custom Location

Create a custom location object.

custom_location = CustomLocation('campus', 'sjc')

Geo Location

Create a geo location object.

geo_location = GeoLocation(37.41, -121.91, 
                           altitude = 26,
                           alt_type = AltType.LOCATION_ALT_TYPE_METERS)

Location

Create a location object.

location = Location(civic_list=civic_location_list, custom_list=custom_location_list, geo=geo_location)

Add civic location

Add a civic location to the location object.

location.add_civic_location(CivicType.LOCATION_CIVIC_TYPE_OF_PLACE, 'office')

Add custom location

Add a custom location to the location object.

intf_location.add_custom_location('rack', '367')

Set network element location

Set the location of the network element.

tutorial.network_element.set_location(location)

Set network interface location

Set the location of a network interface.

intf.set_location(intf_location)

Example Location change Listener

Create a location change Listener that listens to location change events.

class ExampleLocationChangeListener(LocationChangeListener):
    def handle_event(self, event, client_data):
        if event.has_subtype(LocationSubtype.LOCATION_TYPE_GEO):
            logger.info('Geo location information changed.')
        if event.has_subtype(LocationSubtype.LOCATION_TYPE_CUSTOM):
            logger.info('Custom location information changed.')
        if event.has_subtype(LocationSubtype.LOCATION_TYPE_CIVIC):
            logger.info('Civic location information changed.')
        logger.info('The location is changed to: \n' +str(event.location))

Create location change filter

Next, create the filter to filter the required events. Add the event subtypes to only catch the location changes which you want.

loc_filter = LocationChangeFilter()
loc_filter.add_subtype(LocationSubtype.LOCATION_TYPE_GEO)
loc_filter.add_subtype(LocationSubtype.LOCATION_TYPE_CUSTOM)
loc_filter.add_subtype(LocationSubtype.LOCATION_TYPE_CIVIC)

Add location change Listener

Apply the location change listener created earlier.

event_handle = location.add_change_listener(loc_listener, loc_filter, 'client_data')

To create the event modify the location on the Network element.

Get the location

Get the location of the network element.

location = tutorial.network_element.get_location()

Remove geo location

Remove the geo location information from the location object.

location.remove_geo_location()

Remove custom location list

Remove the custom location information list from the location object.

location.remove_custom_location_list()

Remove civic location

Remove a particular civic location information from the location object.

location.remove_civic_location(CivicType.LOCATION_CIVIC_TYPE_OF_PLACE)

Remove location change Listener

Finally, at the end of the tutorial, remove the location change Listener.

location.remove_change_listener(event_handle)

Result

Congratulations! You are now able to use your onePK application to get, set location information and receive notification for location change events.

To try out this tutorial code by running it, you can find the code located in the directory <SDK Location>/tutorials/python/location/LocationTutorial.py