I am new to python.I am trying to develop and openflow application.The application that I begin with is the following file.As you can see it implements a simple Ethernet switch.
https://github.com/osrg/ryu/blob/master/ryu/app/simple_switch.py
Now I have another file
https://github.com/osrg/ryu/blob/master/ryu/topology/api.py
Which looks like it exposes the functions to return link and switch information in the topology.
How ever if I try to call the function as below inside init() of simple_switch.py it returns an error?
def __init__(self, *args, **kwargs):
super(SimpleSwitch, self).__init__(*args, **kwargs)
self.mac_to_port = {}
s_list = get_all_switch(app_manager)
This is the error that I get.
loading app ryu/app/simple_switch.py
loading app ryu.controller.ofp_handler
instantiating app ryu.controller.ofp_handler of OFPHandler
instantiating app ryu/app/simple_switch.py of SimpleSwitch
Traceback (most recent call last):
File "/usr/local/bin/ryu-manager", line 9, in <module>
load_entry_point('ryu==3.8', 'console_scripts', 'ryu-manager')()
File "/usr/local/lib/python2.7/dist-packages/ryu/cmd/manager.py", line 73, in main
services.extend(app_mgr.instantiate_apps(**contexts))
File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 434, in instantiate_apps
self._instantiate(app_name, cls, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 420, in _instantiate
app = cls(*args, **kwargs)
File "/home/karthik/Projects/ryu/ryu/app/simple_switch.py", line 35, in __init__
s_list = get_all_switch(app_manager)
NameError: global name 'get_all_switch' is not defined
My questions are as follows.
1) Can I get the topology information about my mininet topology using - get_all_switch() and - get_all_link() defined in ryu/topology/api.py?
2) If yes,Why isn't the code above not working as expected?
I am asking here because my working knowledge of python is not that great.I want to be able to use the functions inside topology/api.py in simple_switch.py
My import lines in the application simple_switch.py are as follows
import logging
import struct
from ryu.base import app_manager
from ryu.controller import mac_to_port
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_0
from ryu.lib.mac import haddr_to_bin
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
Updated based on Answer 1:
I have modified my code as follows:
from ryu.base import app_manager
from ryu.controller import mac_to_port
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_0
from ryu.lib.mac import haddr_to_bin
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
import ryu.topology.api
def __init__(self, *args, **kwargs):
super(SimpleSwitch, self).__init__(*args, **kwargs)
self.mac_to_port = {}
s_list = ryu.topology.api.get_all_switch(app_manager.RyuApp)
I am now getting a new error:
loading app ryu/app/simple_switch.py
loading app ryu.topology.switches
loading app ryu.controller.ofp_handler
loading app ryu.controller.ofp_handler
instantiating app ryu.topology.switches of Switches
instantiating app ryu.controller.ofp_handler of OFPHandler
instantiating app ryu/app/simple_switch.py of SimpleSwitch
Traceback (most recent call last):
File "/usr/local/bin/ryu-manager", line 9, in <module>
load_entry_point('ryu==3.8', 'console_scripts', 'ryu-manager')()
File "/usr/local/lib/python2.7/dist-packages/ryu/cmd/manager.py", line 73, in main
services.extend(app_mgr.instantiate_apps(**contexts))
File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 434, in instantiate_apps
self._instantiate(app_name, cls, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 420, in _instantiate
app = cls(*args, **kwargs)
File "/home/karthik/Projects/ryu/ryu/app/simple_switch.py", line 36, in __init__
s_list = ryu.topology.api.get_all_switch(app_manager.RyuApp)
File "/usr/local/lib/python2.7/dist-packages/ryu/topology/api.py", line 26, in get_all_switch
return get_switch(app)
File "/usr/local/lib/python2.7/dist-packages/ryu/topology/api.py", line 21, in get_switch
rep = app.send_request(event.EventSwitchRequest(dpid))
TypeError: unbound method send_request() must be called with RyuApp instance as first argument (got EventSwitchRequest instance instead)