<s> """<STR_LIT>""" <EOL> import random <EOL> from twisted . python import log <EOL> from twisted . internet import defer <EOL> from opennsa import constants as cnt , config <EOL> from opennsa . backends . common import genericbackend , ssh <EOL> COMMAND_CONFIGURE = '<STR_LIT>' <EOL> COMMAND_COMMIT = '<STR_LIT>' <EOL> COMMAND_SET_VLAN = '<STR_LIT>' <EOL> COMMAND_SET_INTERFACE_VLAN = '<STR_LIT>' <EOL> COMMAND_DELETE_VLAN = '<STR_LIT>' <EOL> COMMAND_DELETE_INTERFACE_VLAN = '<STR_LIT>' <EOL> LOG_SYSTEM = '<STR_LIT>' <EOL> def createConfigureCommands ( source_nrm_port , dest_nrm_port , vlan ) : <EOL> vl = COMMAND_SET_VLAN % ( vlan , vlan ) <EOL> p1 = COMMAND_SET_INTERFACE_VLAN % ( source_nrm_port , vlan ) <EOL> p2 = COMMAND_SET_INTERFACE_VLAN % ( dest_nrm_port , vlan ) <EOL> commands = [ vl , p1 , p2 ] <EOL> return commands <EOL> def createDeleteCommands ( source_nrm_port , dest_nrm_port , vlan ) : <EOL> p1 = COMMAND_DELETE_INTERFACE_VLAN % ( source_nrm_port , vlan ) <EOL> p2 = COMMAND_DELETE_INTERFACE_VLAN % ( dest_nrm_port , vlan ) <EOL> vl = COMMAND_DELETE_VLAN % vlan <EOL> commands = [ p1 , p2 , vl ] <EOL> return commands <EOL> class SSHChannel ( ssh . SSHChannel ) : <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self , conn ) : <EOL> ssh . SSHChannel . __init__ ( self , conn = conn ) <EOL> self . line = '<STR_LIT>' <EOL> self . wait_defer = None <EOL> self . wait_line = None <EOL> @ defer . inlineCallbacks <EOL> def sendCommands ( self , commands ) : <EOL> LT = '<STR_LIT:\r>' <EOL> try : <EOL> yield self . conn . sendRequest ( self , '<STR_LIT>' , '<STR_LIT>' , wantReply = <NUM_LIT:1> ) <EOL> d = self . waitForLine ( '<STR_LIT:>>' ) <EOL> self . write ( COMMAND_CONFIGURE + LT ) <EOL> yield d <EOL> log . msg ( '<STR_LIT>' , debug = True , system = LOG_SYSTEM ) <EOL> for cmd in commands : <EOL> log . msg ( '<STR_LIT>' % cmd , system = LOG_SYSTEM ) <EOL> d = self . waitForLine ( '<STR_LIT>' ) <EOL> self . write ( cmd + LT ) <EOL> yield d <EOL> d = self . waitForLine ( '<STR_LIT>' ) <EOL> self . write ( COMMAND_COMMIT + LT ) <EOL> yield d <EOL> except Exception , e : <EOL> log . msg ( '<STR_LIT>' % str ( e ) ) <EOL> raise e <EOL> log . msg ( '<STR_LIT>' , debug = True , system = LOG_SYSTEM ) <EOL> self . sendEOF ( ) <EOL> self . closeIt ( ) <EOL> def waitForLine ( self , line ) : <EOL> self . wait_line = line <EOL> self . wait_defer = defer . Deferred ( ) <EOL> return self . wait_defer <EOL> def matchLine ( self , line ) : <EOL> if self . wait_line and self . wait_defer : <EOL> if self . wait_line in line . strip ( ) : <EOL> d = self . wait_defer <EOL> self . wait_line = None <EOL> self . wait_defer = None <EOL> d . callback ( self ) <EOL> else : <EOL> pass <EOL> def dataReceived ( self , data ) : <EOL> if len ( data ) == <NUM_LIT:0> : <EOL> pass <EOL> else : <EOL> self . line += data <EOL> if '<STR_LIT:\n>' in data : <EOL> lines = [ line . strip ( ) for line in self . line . split ( '<STR_LIT:\n>' ) if line . strip ( ) ] <EOL> self . line = '<STR_LIT>' <EOL> for l in lines : <EOL> self . matchLine ( l ) <EOL> class JuniperEXCommandSender : <EOL> def __init__ ( self , host , port , ssh_host_fingerprint , user , ssh_public_key_path , ssh_private_key_path ) : <EOL> self . ssh_connection_creator = ssh . SSHConnectionCreator ( host , port , [ ssh_host_fingerprint ] , user , ssh_public_key_path , ssh_private_key_path ) <EOL> self . ssh_connection = None <EOL> def _getSSHChannel ( self ) : <EOL> def setSSHConnectionCache ( ssh_connection ) : <EOL> log . msg ( '<STR_LIT>' , system = LOG_SYSTEM ) <EOL> self . ssh_connection = ssh_connection <EOL> return ssh_connection <EOL> def gotSSHConnection ( ssh_connection ) : <EOL> channel = SSHChannel ( conn = ssh_connection ) <EOL> ssh_connection . openChannel ( channel ) <EOL> return channel . channel_open <EOL> if self . ssh_connection and not self . ssh_connection . transport . factory . stopped : <EOL> log . msg ( '<STR_LIT>' , debug = True , system = LOG_SYSTEM ) <EOL> return gotSSHConnection ( self . ssh_connection ) <EOL> else : <EOL> log . msg ( '<STR_LIT>' , system = LOG_SYSTEM ) <EOL> d = self . ssh_connection_creator . getSSHConnection ( ) <EOL> d . addCallback ( setSSHConnectionCache ) <EOL> d . addCallback ( gotSSHConnection ) <EOL> return d <EOL> def _sendCommands ( self , commands ) : <EOL> def gotChannel ( channel ) : <EOL> d = channel . sendCommands ( commands ) <EOL> return d <EOL> d = self . _getSSHChannel ( ) <EOL> d . addCallback ( gotChannel ) <EOL> return d <EOL> def setupLink ( self , source_nrm_port , dest_nrm_port , vlan ) : <EOL> commands = createConfigureCommands ( source_nrm_port , dest_nrm_port , vlan ) <EOL> return self . _sendCommands ( commands ) <EOL> def teardownLink ( self , source_nrm_port , dest_nrm_port , vlan ) : <EOL> commands = createDeleteCommands ( source_nrm_port , dest_nrm_port , vlan ) <EOL> return self . _sendCommands ( commands ) <EOL> class JunosEXTarget ( object ) : <EOL> def __init__ ( self , port , vlan = None ) : <EOL> self . port = port <EOL> self . vlan = vlan <EOL> def __str__ ( self ) : <EOL> if self . vlan : <EOL> return '<STR_LIT>' % ( self . port , self . vlan ) <EOL> else : <EOL> return '<STR_LIT>' % self . port <EOL> class JuniperEXConnectionManager : <EOL> def __init__ ( self , port_map , host , port , host_fingerprint , user , ssh_public_key , ssh_private_key ) : <EOL> self . port_map = port_map <EOL> self . command_sender = JuniperEXCommandSender ( host , port , host_fingerprint , user , ssh_public_key , ssh_private_key ) <EOL> def getResource ( self , port , label_type , label_value ) : <EOL> assert label_type in ( None , cnt . ETHERNET_VLAN ) , '<STR_LIT>' <EOL> return str ( label_value ) <EOL> def getTarget ( self , port , label_type , label_value ) : <EOL> assert label_type in ( None , cnt . ETHERNET_VLAN ) , '<STR_LIT>' <EOL> if label_type == cnt . ETHERNET_VLAN : <EOL> vlan = int ( label_value ) <EOL> assert <NUM_LIT:1> <= vlan <= <NUM_LIT> , '<STR_LIT>' % label_value <EOL> return JunosEXTarget ( self . port_map [ port ] , vlan ) <EOL> def createConnectionId ( self , source_target , dest_target ) : <EOL> return '<STR_LIT>' + str ( random . randint ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> def canSwapLabel ( self , label_type ) : <EOL> return False <EOL> def setupLink ( self , connection_id , source_target , dest_target , bandwidth ) : <EOL> assert source_target . vlan == dest_target . vlan , '<STR_LIT>' <EOL> def linkUp ( _ ) : <EOL> log . msg ( '<STR_LIT>' % ( source_target , dest_target ) , system = LOG_SYSTEM ) <EOL> d = self . command_sender . setupLink ( source_target . port , dest_target . port , dest_target . vlan ) <EOL> d . addCallback ( linkUp ) <EOL> return d <EOL> def teardownLink ( self , connection_id , source_target , dest_target , bandwidth ) : <EOL> assert source_target . vlan == dest_target . vlan , '<STR_LIT>' <EOL> def linkDown ( _ ) : <EOL> log . msg ( '<STR_LIT>' % ( source_target , dest_target ) , system = LOG_SYSTEM ) <EOL> d = self . command_sender . teardownLink ( source_target . port , dest_target . port , dest_target . vlan ) <EOL> d . addCallback ( linkDown ) <EOL> return d <EOL> def JuniperEXBackend ( network_name , nrm_ports , parent_requester , cfg ) : <EOL> name = '<STR_LIT>' % network_name <EOL> nrm_map = dict ( [ ( p . name , p ) for p in nrm_ports ] ) <EOL> port_map = dict ( [ ( p . name , p . interface ) for p in nrm_ports ] ) <EOL> host = cfg [ config . JUNIPER_HOST ] <EOL> port = cfg . get ( config . JUNIPER_PORT , <NUM_LIT> ) <EOL> host_fingerprint = cfg [ config . JUNIPER_HOST_FINGERPRINT ] <EOL> user = cfg [ config . JUNIPER_USER ] <EOL> ssh_public_key = cfg [ config . JUNIPER_SSH_PUBLIC_KEY ] <EOL> ssh_private_key = cfg [ config . JUNIPER_SSH_PRIVATE_KEY ] <EOL> cm = JuniperEXConnectionManager ( port_map , host , port , host_fingerprint , user , ssh_public_key , ssh_private_key ) <EOL> return genericbackend . GenericBackend ( network_name , nrm_map , cm , parent_requester , name ) </s>
