<s> '''<STR_LIT>''' <EOL> from pox . lib . util import connect_socket_with_backoff <EOL> from pox . lib . packet . ethernet import ethernet <EOL> from pox . lib . ioworker . io_worker import JSONIOWorker <EOL> from pox . openflow . software_switch import * <EOL> from pox . openflow . libopenflow_01 import * <EOL> from sts . entities import ControllerState <EOL> from sts . util . console import msg <EOL> from sts . util . network_namespace import bind_raw_socket <EOL> from sts . util . convenience import base64_encode <EOL> from sts . entities import ConnectionlessOFConnection <EOL> import subprocess <EOL> import os <EOL> import abc <EOL> import logging <EOL> log = logging . getLogger ( "<STR_LIT>" ) <EOL> class ControllerManager ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , controllers , simulation = None ) : <EOL> self . cid2controller = { <EOL> controller . cid : controller <EOL> for controller in controllers <EOL> } <EOL> self . simulation = simulation <EOL> def set_simulation ( self , simulation ) : <EOL> self . simulation = simulation <EOL> @ property <EOL> def controller_configs ( self ) : <EOL> return [ c . config for c in self . controllers ] <EOL> @ property <EOL> def controllers ( self ) : <EOL> cs = self . cid2controller . values ( ) <EOL> cs . sort ( key = lambda c : c . cid ) <EOL> return cs <EOL> @ property <EOL> def remote_controllers ( self ) : <EOL> return [ c for c in self . controllers if c . remote ] <EOL> @ property <EOL> def cids ( self ) : <EOL> ids = self . cid2controller . keys ( ) <EOL> ids . sort ( ) <EOL> return ids <EOL> @ property <EOL> def live_controllers ( self ) : <EOL> '''<STR_LIT>''' <EOL> alive = [ controller for controller in self . controllers if controller . state == ControllerState . ALIVE ] <EOL> return set ( alive ) <EOL> @ property <EOL> def down_controllers ( self ) : <EOL> '''<STR_LIT>''' <EOL> down = [ controller for controller in self . controllers if controller . state == ControllerState . DEAD ] <EOL> return set ( down ) <EOL> def all_controllers_down ( self ) : <EOL> return len ( self . live_controllers ) == <NUM_LIT:0> <EOL> def get_controller_by_label ( self , label ) : <EOL> for c in self . cid2controller . values ( ) : <EOL> if c . label == label : <EOL> return c <EOL> return None <EOL> def get_controller ( self , cid ) : <EOL> if cid not in self . cid2controller : <EOL> raise ValueError ( "<STR_LIT>" % str ( cid ) ) <EOL> return self . cid2controller [ cid ] <EOL> def kill_all ( self ) : <EOL> for c in self . live_controllers : <EOL> c . kill ( ) <EOL> self . cid2controller = { } <EOL> @ staticmethod <EOL> def kill_controller ( controller ) : <EOL> controller . kill ( ) <EOL> @ staticmethod <EOL> def reboot_controller ( controller ) : <EOL> controller . restart ( ) <EOL> def check_controller_status ( self ) : <EOL> '''<STR_LIT>''' <EOL> controllers_with_problems = [ ] <EOL> for c in self . controllers : <EOL> ( ok , msg ) = c . check_status ( self . simulation ) <EOL> if ok and c . state == ControllerState . STARTING : <EOL> c . state = ControllerState . ALIVE <EOL> if not ok and c . state != ControllerState . STARTING : <EOL> c . state = ControllerState . DEAD <EOL> controllers_with_problems . append ( ( c , msg ) ) <EOL> return controllers_with_problems <EOL> class ControllerPatchPanel ( object ) : <EOL> '''<STR_LIT>''' <EOL> __metaclass__ = abc . ABCMeta <EOL> def __init__ ( self , create_io_worker ) : <EOL> self . create_io_worker = create_io_worker <EOL> self . _cid2ethaddr = { } <EOL> def _initialize_switch ( self ) : <EOL> for dl_dst in [ EthAddr ( "<STR_LIT>" ) , EthAddr ( "<STR_LIT>" ) ] : <EOL> self . _send_command ( ofp_flow_mod ( match = ofp_match ( dl_dst = dl_dst ) , <EOL> action = ofp_action_output ( port = OFPP_FLOOD ) ) ) <EOL> self . _send_command ( ofp_flow_mod ( match = ofp_match ( ) , priority = <NUM_LIT:1> ) ) <EOL> @ abc . abstractmethod <EOL> def _send_command ( self , command ) : <EOL> pass <EOL> def register_controller ( self , cid , guest_eth_addr , host_device ) : <EOL> '''<STR_LIT>''' <EOL> self . _cid2ethaddr [ cid ] = guest_eth_addr <EOL> port = self . _create_port_for_controller ( guest_eth_addr , host_device ) <EOL> self . _send_command ( ofp_flow_mod ( match = ofp_match ( dl_dst = guest_eth_addr ) , <EOL> action = ofp_action_output ( port = port ) ) ) <EOL> @ abc . abstractmethod <EOL> def _create_port_for_controller ( self ) : <EOL> pass <EOL> def clean_up ( self ) : <EOL> pass <EOL> def block_controller_pair ( self , cid1 , cid2 ) : <EOL> '''<STR_LIT>''' <EOL> ethaddr1 = self . _cid2ethaddr [ cid1 ] <EOL> ethaddr2 = self . _cid2ethaddr [ cid2 ] <EOL> self . _send_command ( ofp_flow_mod ( match = ofp_match ( dl_dst = ethaddr1 , dl_src = ethaddr2 ) , <EOL> priority = OFP_DEFAULT_PRIORITY + <NUM_LIT:1> ) ) <EOL> self . _send_command ( ofp_flow_mod ( match = ofp_match ( dl_dst = ethaddr2 , dl_src = ethaddr1 ) , <EOL> priority = OFP_DEFAULT_PRIORITY + <NUM_LIT:1> ) ) <EOL> def unblock_controller_pair ( self , cid1 , cid2 ) : <EOL> '''<STR_LIT>''' <EOL> ethaddr1 = self . _cid2ethaddr [ cid1 ] <EOL> ethaddr2 = self . _cid2ethaddr [ cid2 ] <EOL> self . _send_command ( ofp_flow_mod ( match = ofp_match ( dl_dst = ethaddr1 , dl_src = ethaddr2 ) , <EOL> priority = OFP_DEFAULT_PRIORITY + <NUM_LIT:1> , <EOL> command = OFPFC_DELETE ) ) <EOL> self . _send_command ( ofp_flow_mod ( match = ofp_match ( dl_dst = ethaddr2 , dl_src = ethaddr1 ) , <EOL> priority = OFP_DEFAULT_PRIORITY + <NUM_LIT:1> , <EOL> command = OFPFC_DELETE ) ) <EOL> class UserSpaceControllerPatchPanel ( ControllerPatchPanel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , create_io_worker ) : <EOL> super ( UserSpaceControllerPatchPanel , self ) . __init__ ( create_io_worker ) <EOL> self . switch = SoftwareSwitch ( - <NUM_LIT:1> , ports = [ ] ) <EOL> self . _port2io_worker = { } <EOL> self . switch . addListener ( DpPacketOut , <EOL> lambda event : self . _port2io_worker [ event . port ] . send ( event . packet . pack ( ) ) ) <EOL> self . _initialize_switch ( ) <EOL> def clean_up ( self ) : <EOL> for io_worker in self . _port2io_worker . itervalues ( ) : <EOL> io_worker . close ( ) <EOL> def _send_command ( self , command ) : <EOL> self . switch . on_message_received ( None , command ) <EOL> def _create_port_for_controller ( self , guest_eth_addr , host_device ) : <EOL> port = ofp_phy_port ( port_no = len ( self . switch . ports ) + <NUM_LIT:1> ) <EOL> self . switch . bring_port_up ( port ) <EOL> raw_socket = bind_raw_socket ( host_device ) <EOL> io_worker = self . create_io_worker ( raw_socket ) <EOL> switch = self . switch <EOL> def _process_raw_socket_read ( io_worker ) : <EOL> data = io_worker . peek_receive_buf ( ) <EOL> packet = ethernet ( bytes ( data ) ) <EOL> io_worker . consume_receive_buf ( len ( data ) ) <EOL> if log . isEnabledFor ( logging . DEBUG ) : <EOL> log . debug ( "<STR_LIT>" % ( packet , port ) ) <EOL> switch . process_packet ( packet , port . port_no ) <EOL> io_worker . set_receive_handler ( _process_raw_socket_read ) <EOL> self . _port2io_worker [ port ] = io_worker <EOL> return port <EOL> class OVSControllerPatchPanel ( ControllerPatchPanel ) : <EOL> '''<STR_LIT>''' <EOL> of_port = <NUM_LIT> <EOL> messenger_port = <NUM_LIT> <EOL> def __init__ ( self , create_io_worker ) : <EOL> super ( OVSControllerPatchPanel , self ) . __init__ ( create_io_worker ) <EOL> args = str ( """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" % <EOL> ( self . of_port , self . messenger_port ) ) . split ( ) <EOL> self . pox = subprocess . Popen ( args , cwd = "<STR_LIT>" , preexec_fn = lambda : os . setsid ( ) ) <EOL> true_socket = connect_socket_with_backoff ( address = "<STR_LIT:127.0.0.1>" , <EOL> port = self . messenger_port ) <EOL> self . json_worker = JSONIOWorker ( create_io_worker ( true_socket ) ) <EOL> self . json_worker . send ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . _initialize_switch ( ) <EOL> def _send_command ( self , command ) : <EOL> b64_pkt = base64_encode ( command ) <EOL> self . json_worker . send ( b64_pkt ) <EOL> def _create_port_for_controller ( self , guest_eth_addr , host_device ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def clean_up ( self ) : <EOL> self . json_worker . close ( ) <EOL> self . pox . kill ( ) <EOL> class MockControllerManager ( object ) : <EOL> def __init__ ( self , controller_configs ) : <EOL> self . controller_configs = controller_configs <EOL> def set_simulation ( self , simulation ) : pass <EOL> def kill_all ( self ) : pass <EOL> @ property <EOL> def live_controllers ( self ) : return [ ] <EOL> @ property <EOL> def down_controllers ( self ) : return [ ] <EOL> def check_controller_status ( self ) : return [ ] <EOL> def all_controllers_down ( self ) : return False <EOL> def boot_mock_controllers ( controller_configs , snapshot_service , <EOL> sync_connection_manager , multiplex_sockets = False ) : <EOL> return MockControllerManager ( controller_configs ) <EOL> def create_mock_connection ( controller_config , software_switch , max_backoff_seconds = <NUM_LIT> ) : <EOL> return ConnectionlessOFConnection ( controller_config . cid , software_switch . dpid ) <EOL> def connect_to_mock_controllers ( simulation ) : <EOL> simulation . topology . connect_to_controllers ( simulation . controller_manager . controller_configs , <EOL> create_connection = create_mock_connection ) </s>
