Goal

This tutorial show you how to use the Topology Service Set API. You register a CDP listener and look for the neighbors of the connected network element.

Tutorial Code

The code used in this tutorial is located in the TopologyTutorial.py file under <SDK Location>/python/tutorials/topology/TopologyTutorial.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

Example Topology Listener

First, create a TopologyListener to listen to any changes in the network topology.

class ExampleTopologyListener(TopologyListener):
    
    name = str()
    
    """
      Creates an Example Topology Event Listener.
      @param name
            Identifies the instance.
    """
    def __init__(self, name):        
        super(TopologyListener, self).__init__()
        self.name = name

    """
      Invoked when an event is received from a network element.
      @param event
                 An event object that indicates that an event has occurred in a network element.
      @param clientData
                 The clientData is an object that is passed in when the application calls an API to add/register
                 the event listener. The application is responsible for casting the input clientData to the
                 appropriate class before using it.
    """    
    def handle_event(self, event, clientData):
        
        print "\nReceived Topology Event from " + self.name
        for type in event.types:
            if (type == TopologyEvent.TopologyEventType.EDGES_ADD):
                print "Some edges were added"
            if (type == TopologyEvent.TopologyEventType.EDGES_DELETE):
                print "Some edges were deleted"
            if (type == TopologyEvent.TopologyEventType.NODES_ADD):
                print "Some nodes were added"
            if (type == TopologyEvent.TopologyEventType.NODES_DELETE):
                print "Some nodes were deleted"
        print "No of edges changed" , len(event.edge_list)

Add Topology Listener

Next, add the TopologyListener that you created earlier.

listener = ExampleTopologyListener("TopologyListener1")
topology = TopologyClass(network_element, TopologyClass.TopologyType.CDP)
event_type = []
event_type.append(TopologyEvent.TopologyEventType.EDGES_ADD)
event_type.append(TopologyEvent.TopologyEventType.EDGES_DELETE)
filter = TopologyFilter(event_type)
event_handle = topology.add_topology_listener(listener, filter, "TopologyClient1")

Discover CDP Topology

You can then discover the network topology through CDP events.

#  Create a topology object
topology = TopologyClass(network_element, TopologyClass.TopologyType.CDP)
#  Get the graph object 
graph = topology.get_graph()
#  Get the list of edges from the Graph object. 
edgeList = graph.get_edge_list(Edge.EdgeType.UNDIRECTED)
#  Get the Node and Node connectors          
edge_count  = 0
if len(edgeList) > 0:
    for edge in edgeList:
        edge_count += 1
        print "*** Edge no" , edge_count
    
        print "Local Host (Head node) is: ", edge.head_node
        print "Remote Host (Tail node) is :", edge.tail_node
        print "Local interface (Head node connector) is:", edge.head_node_connector
        print "Remote interface (Tail node connector) is:", edge.tail_node_connector
else:
    print "There are no edges."

Result

Congratulations! Now you know how to register a CDP listener to look for the neighbors of the network element.

To try out this tutorial code by compiling and running it, you can find the code located at: <SDK Location>/python/topology/TopologyTutorial.py.