Question: This is the code for class Switches from <https://github.com/osrg/ryu/blob/master/ryu/topology/switches.py#L429>

The member variables of particular interest to me in the class Switches are the following.

```
    self.dps = {}                 # datapath_id => Datapath class
    self.port_state = {}          # datapath_id => ports
    self.ports = PortDataState()  # Port class -> PortData class
    self.links = LinkState()      # Link class -> timestamp
    self.is_active = True

```

These are the member variables that the RYU uses to cache the topology details.I am trying to figure out how a topology can be represented using the following variables.

1) dps is a dictionary which maps datapath\_id to datapath class?

```
- Can someone explain to me what is a datapath_id and a datapath class?

```

2) port\_state is a dictionary which maps datapath id to ports

```
- As per my understanding on a switch without a VLAN all the ports will belong to the           same datapath id? In case of a switch with VLAN ports on the switch can have multiple datapath id's. Is my understanding correct?

```

3) ports is again a dictionary which maps Port class to PortData class?

```
- what does this mean?

```

4) links is again a dictionary which maps Link class to timestamp

```
- again what does this mean?

```

I am trying to get my head around how RYU controller stores the topology information using the above structures.Any help in understanding or explanation would be greatly appreciated.

Answer: 
Generically , Each OpenFlow switch connects to a controller on a specific port each such connection between a switch and a controller is seen as an individual 'datapath' and its generally referred to as DPID (DataPath ID). 

a link is perceived as a connection between ports belonging to 2 DPIDs, hence
DPID 1 (PORT1) <—> (PORT2)DPID2 is a link between SW1 P1 and SW2 P1. 

I use POX controller, so not very much across Ryu, but I think you can now make sense of it.
Here is what I think of the answers: 

1. save a datapath class for each datapth id. For example in line of 475 in [switches.py](https://github.com/osrg/ryu/blob/master/ryu/topology/switches.py):
`self.dps[dp.id] = dp` This way with one datapath id the whole class is easily findable.
2. `PortState` is defined like `PortState(dict)` at line 161 which means it's a dictionary. Where the dict has key of `int port_no` and a value of `OFPPort port`.  

So `self.port_state` saves the ports for each datapath id. For example for `self.port_state[1]` it would contain the ports with datapath id of 1.
3. `PortDataState` is defined like `PortDataState(dict)` at line 206 which means it's a dictionary. It is some sort of doubly linked list in terms of dictionary. 
It seems this is more like a list of ports as a key and then their status as a value.
4. LinkState keeps track of links.
For a link between `src` and `dst`, it creates a Link object. And saved a mapping between `dst` and `src` (This is done using self.\_map structure). Also, for each created Link object it keeps a record of the time (I dont know why it does that).  

Have a look at line 317 of [switches.py](https://github.com/osrg/ryu/blob/master/ryu/topology/switches.py#L317)

Here is good tutorial for [topo discovery in Ryu](https://github.com/Ehsan70/RyuApps/blob/master/TopoDiscoveryInRyu.md).