I thank Mr. Michael Karl for helping me in understanding the OpenFlow modules.
# Import helper classes
execfile('/opt/openflow-main/nox/src/nox/coreapps/mycontroller/main/myrouting.py')
# Initialize helper classes
myrouting = My_Routing()
Ramakrishna = RamakrishnaModule(); # init Ramakrishna's module
def oflogger(txt):
logger.warn('-OPENFLOW / WARN- : ' + txt + '\n')
def oferror(txt):
logger.warn('\n\n\n-OPENFLOW / ERROR- : ' + txt + ' \n\n\n')
class mycontroller(Component):
""" Main control component for NOX of My OpenFlow Network
"""
def __init__(self, ctxt):
""" Initialize the my controller
@param ctxt: nox context. This is only needed for the super constructor
"""
# Call inherited constructor
Component.__init__(self, ctxt)
Ramakrishna.setCtrl(self)
# init internal variables
self._discovery = self.resolve('nox.netapps.discovery.discovery.discovery')
logger.debug('OPENFLOW-CONTROL Initialized')
def install(self):
""" Install self as NOX component. This method will be called by the NOX framework.
All we do is register ourselves to the appropriate events.
"""
# Handler
self.register_for_packet_in(self.handle_my_packet_in)
self.register_for_datapath_join(self.handle_my_dp_join)
self.register_for_datapath_leave(self.handle_my_dp_leave)
self.register_for_flow_removed(self.handle_my_flow_removed)
self.register_for_flow_mod(self.handle_my_flow_mod)
self.register_handler(Link_event.static_my_get_name(), self.handle_my_link_event)
logger.debug('OF-CONTROL Installed')
def getInterface(self):
""" Returns a string representation of this my controller object """
return str(mycontroller)
def handle_my_packet_in(self, dpid, inport, reason, length, bufid, packet):
"""
This function is called to notify the controller of an incoming packet on a router
@param dpid: DPID of the switch receiving the packet
@param inport: port at the switch on which the packet was received
@param reason: ???
@param length: Length of the incoming packet
@param bufid: ???
@param packet: the packet blob
"""
# Could not parse packet. Ignore
if not packet.parsed:
return CONTINUE
# Forward every packet seen by the controller also to Ramakrishna's module
Ramakrishna.PacketIn(dpid, inport, reason, length, bufid, packet)
return CONTINUE
def handle_my_dp_join(self, dpid, stats):
"""
This method is called when a switch joins us. We notify our helpers, reset the switch and set some parameters
@param dpid: DPID of the switch that just joined
@param stats: stats of this switch
"""
Ramakrishna.DPJoin(dpid, stats)
self.switch_reset(dpid) # Reset switch and set parameters
return CONTINUE
def handle_my_dp_leave(self, dpid):
"""
This method is called when a switch leaves. We notify our helpers and delete the flows.
@param dpid: DPID of the switch that just left
"""
Ramakrishna.DPLeave(dpid, stats)
return CONTINUE
def handle_my_link_event(self, event):
"""
This handles link add and remove events. Basically only notify our helpers (and delete flows).
@param event: the link event. Contains action and link details
"""
Ramakrishna.LinkEvent(event)
return CONTINUE
def handle_my_flow_removed(self, dpid, flow, priority, reason, cookie, dursec, durnsec, bytecount, packetcount):
"""
This method handles removed flows by removing them in the helpers
@param dpid: is the datapath id of the switch.
@param flow: is a NOX flow dictionary
@param priority: is the flow's priority
@param reason: why the flow was removed (see ofp_flow_removed_reason)
@param cookie: is the flow's cookie
@param dursec: is how long the flow was alive in (s).
@param durnsec: is how long the flow was alive beyond dur_sec in (ns).
@param bytecount: is the number of bytes passed through this flow.
@param packetcount: is the number of packets passed through this flow.
"""
Ramakrishna.FlowRemoved(dpid, flow, priority, reason, cookie, dursec, durnsec, bytecount, packetcount)
return CONTINUE
def handle_my_flow_mod(self, dpid, flow, command, idle_to, hard_to, buffer_id, priority, cookie):
"""
Manages modified flows by telling the DataStore (except for delay measurement flows)
@param dpid: is the datapath id of the switch.
@param flow: is a NOX flow dictionary
@param command: is the type of flow mod (see ofp_flow_mod_command)
@param idle_to: is the idle timeout of the flow
@param hard_to: is the hard timeout of the flow
@param buffer_id: is a buffer id assigned by the switch, or -1 if there is no buffer. (This is not meaningful for OFPFC_DELETE command).
@param priority: is the flow's priority
@param cookie: is the flow's cookie
"""
Ramakrishna.FlowModification(dpid, flow, command, idle_to, hard_to, buffer_id, priority, cookie)
return CONTINUE
def getFactory():
"""Builds a factory class for ofcontrol
"""
class Factory:
def instance(self, ctxt):
"""Returns a new mycontroller instance
"""
return mycontroller(ctxt)
return Factory()
Execution Steps:
cd /opt/openflow-main/nox/build/src/
./nox_core -i ptcp:6633 monitoring mycontroller
# Import helper classes
execfile('/opt/openflow-main/nox/src/nox/coreapps/mycontroller/main/myrouting.py')
# Initialize helper classes
myrouting = My_Routing()
Ramakrishna = RamakrishnaModule(); # init Ramakrishna's module
def oflogger(txt):
logger.warn('-OPENFLOW / WARN- : ' + txt + '\n')
def oferror(txt):
logger.warn('\n\n\n-OPENFLOW / ERROR- : ' + txt + ' \n\n\n')
class mycontroller(Component):
""" Main control component for NOX of My OpenFlow Network
"""
def __init__(self, ctxt):
""" Initialize the my controller
@param ctxt: nox context. This is only needed for the super constructor
"""
# Call inherited constructor
Component.__init__(self, ctxt)
Ramakrishna.setCtrl(self)
# init internal variables
self._discovery = self.resolve('nox.netapps.discovery.discovery.discovery')
logger.debug('OPENFLOW-CONTROL Initialized')
def install(self):
""" Install self as NOX component. This method will be called by the NOX framework.
All we do is register ourselves to the appropriate events.
"""
# Handler
self.register_for_packet_in(self.handle_my_packet_in)
self.register_for_datapath_join(self.handle_my_dp_join)
self.register_for_datapath_leave(self.handle_my_dp_leave)
self.register_for_flow_removed(self.handle_my_flow_removed)
self.register_for_flow_mod(self.handle_my_flow_mod)
self.register_handler(Link_event.static_my_get_name(), self.handle_my_link_event)
logger.debug('OF-CONTROL Installed')
def getInterface(self):
""" Returns a string representation of this my controller object """
return str(mycontroller)
def handle_my_packet_in(self, dpid, inport, reason, length, bufid, packet):
"""
This function is called to notify the controller of an incoming packet on a router
@param dpid: DPID of the switch receiving the packet
@param inport: port at the switch on which the packet was received
@param reason: ???
@param length: Length of the incoming packet
@param bufid: ???
@param packet: the packet blob
"""
# Could not parse packet. Ignore
if not packet.parsed:
return CONTINUE
# Forward every packet seen by the controller also to Ramakrishna's module
Ramakrishna.PacketIn(dpid, inport, reason, length, bufid, packet)
return CONTINUE
def handle_my_dp_join(self, dpid, stats):
"""
This method is called when a switch joins us. We notify our helpers, reset the switch and set some parameters
@param dpid: DPID of the switch that just joined
@param stats: stats of this switch
"""
Ramakrishna.DPJoin(dpid, stats)
self.switch_reset(dpid) # Reset switch and set parameters
return CONTINUE
def handle_my_dp_leave(self, dpid):
"""
This method is called when a switch leaves. We notify our helpers and delete the flows.
@param dpid: DPID of the switch that just left
"""
Ramakrishna.DPLeave(dpid, stats)
return CONTINUE
def handle_my_link_event(self, event):
"""
This handles link add and remove events. Basically only notify our helpers (and delete flows).
@param event: the link event. Contains action and link details
"""
Ramakrishna.LinkEvent(event)
return CONTINUE
def handle_my_flow_removed(self, dpid, flow, priority, reason, cookie, dursec, durnsec, bytecount, packetcount):
"""
This method handles removed flows by removing them in the helpers
@param dpid: is the datapath id of the switch.
@param flow: is a NOX flow dictionary
@param priority: is the flow's priority
@param reason: why the flow was removed (see ofp_flow_removed_reason)
@param cookie: is the flow's cookie
@param dursec: is how long the flow was alive in (s).
@param durnsec: is how long the flow was alive beyond dur_sec in (ns).
@param bytecount: is the number of bytes passed through this flow.
@param packetcount: is the number of packets passed through this flow.
"""
Ramakrishna.FlowRemoved(dpid, flow, priority, reason, cookie, dursec, durnsec, bytecount, packetcount)
return CONTINUE
def handle_my_flow_mod(self, dpid, flow, command, idle_to, hard_to, buffer_id, priority, cookie):
"""
Manages modified flows by telling the DataStore (except for delay measurement flows)
@param dpid: is the datapath id of the switch.
@param flow: is a NOX flow dictionary
@param command: is the type of flow mod (see ofp_flow_mod_command)
@param idle_to: is the idle timeout of the flow
@param hard_to: is the hard timeout of the flow
@param buffer_id: is a buffer id assigned by the switch, or -1 if there is no buffer. (This is not meaningful for OFPFC_DELETE command).
@param priority: is the flow's priority
@param cookie: is the flow's cookie
"""
Ramakrishna.FlowModification(dpid, flow, command, idle_to, hard_to, buffer_id, priority, cookie)
return CONTINUE
def getFactory():
"""Builds a factory class for ofcontrol
"""
class Factory:
def instance(self, ctxt):
"""Returns a new mycontroller instance
"""
return mycontroller(ctxt)
return Factory()
Execution Steps:
cd /opt/openflow-main/nox/build/src/
./nox_core -i ptcp:6633 monitoring mycontroller
No comments:
Post a Comment