<s> """<STR_LIT>""" <EOL> import base64 <EOL> import random <EOL> from twisted . python import log <EOL> from twisted . web . error import Error as WebError <EOL> from opennsa import constants as cnt , config <EOL> from opennsa . backends . common import genericbackend <EOL> from opennsa . protocols . shared import httpclient <EOL> NCS_TIMEOUT = <NUM_LIT> <EOL> NO_OUT_OF_SYNC_CHECK = '<STR_LIT>' <EOL> ETHERNET_VPN_PAYLOAD_BASE = """<STR_LIT>""" <EOL> ETHERNET_VLAN_VPN_PAYLOAD_BASE = """<STR_LIT>""" <EOL> ETHERNET_VLAN_REWRITE_VPN_PAYLOAD_BASE = """<STR_LIT>""" <EOL> LOG_SYSTEM = '<STR_LIT>' <EOL> class NCSVPNTarget ( object ) : <EOL> def __init__ ( self , router , interface , vlan = None ) : <EOL> self . router = router <EOL> self . interface = interface <EOL> self . vlan = vlan <EOL> def __str__ ( self ) : <EOL> if self . vlan : <EOL> return '<STR_LIT>' % ( self . router , self . interface , self . vlan ) <EOL> else : <EOL> return '<STR_LIT>' % ( self . router , self . interface ) <EOL> def createVPNPayload ( service_name , source_target , dest_target ) : <EOL> intps = { <EOL> '<STR_LIT>' : service_name , <EOL> '<STR_LIT>' : service_name , <EOL> '<STR_LIT>' : source_target . router , <EOL> '<STR_LIT>' : source_target . interface , <EOL> '<STR_LIT>' : dest_target . router , <EOL> '<STR_LIT>' : dest_target . interface <EOL> } <EOL> if source_target . vlan and dest_target . vlan : <EOL> if source_target . vlan == dest_target . vlan : <EOL> intps [ '<STR_LIT>' ] = source_target . vlan <EOL> payload = ETHERNET_VLAN_VPN_PAYLOAD_BASE % intps <EOL> else : <EOL> intps [ '<STR_LIT>' ] = source_target . vlan <EOL> intps [ '<STR_LIT>' ] = dest_target . vlan <EOL> payload = ETHERNET_VLAN_REWRITE_VPN_PAYLOAD_BASE % intps <EOL> else : <EOL> payload = ETHERNET_VPN_PAYLOAD_BASE % intps <EOL> return payload <EOL> def _extractErrorMessage ( failure ) : <EOL> if isinstance ( failure . value , WebError ) : <EOL> return failure . value . response <EOL> else : <EOL> return failure . getErrorMessage ( ) <EOL> class NCSVPNConnectionManager : <EOL> def __init__ ( self , ncs_services_url , user , password , port_map , log_system ) : <EOL> self . ncs_services_url = ncs_services_url <EOL> self . user = user <EOL> self . password = password <EOL> self . port_map = port_map <EOL> self . log_system = log_system <EOL> def getResource ( self , port , label_type , label_value ) : <EOL> assert label_type in ( None , cnt . ETHERNET_VLAN ) , '<STR_LIT>' <EOL> return port + '<STR_LIT::>' + 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> ri = self . port_map [ port ] <EOL> router , interface = ri . split ( '<STR_LIT::>' ) <EOL> return NCSVPNTarget ( router , interface , 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 label_type == cnt . ETHERNET_VLAN <EOL> def _createAuthzHeader ( self ) : <EOL> return '<STR_LIT>' + base64 . b64encode ( self . user + '<STR_LIT::>' + self . password ) <EOL> def _createHeaders ( self ) : <EOL> headers = { } <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> headers [ '<STR_LIT>' ] = self . _createAuthzHeader ( ) <EOL> return headers <EOL> def setupLink ( self , connection_id , source_target , dest_target , bandwidth ) : <EOL> service_url = self . ncs_services_url + '<STR_LIT:?>' + NO_OUT_OF_SYNC_CHECK <EOL> payload = createVPNPayload ( connection_id , source_target , dest_target ) <EOL> headers = self . _createHeaders ( ) <EOL> def linkUp ( _ ) : <EOL> log . msg ( '<STR_LIT>' % ( source_target , dest_target ) , system = self . log_system ) <EOL> def error ( failure ) : <EOL> log . msg ( '<STR_LIT>' % ( source_target , dest_target ) , system = self . log_system ) <EOL> log . msg ( '<STR_LIT>' % _extractErrorMessage ( failure ) , system = self . log_system ) <EOL> return failure <EOL> d = httpclient . httpRequest ( service_url , payload , headers , method = '<STR_LIT:POST>' , timeout = NCS_TIMEOUT ) <EOL> d . addCallbacks ( linkUp , error ) <EOL> return d <EOL> def teardownLink ( self , connection_id , source_target , dest_target , bandwidth ) : <EOL> service_url = self . ncs_services_url + '<STR_LIT>' + connection_id + '<STR_LIT:?>' + NO_OUT_OF_SYNC_CHECK <EOL> headers = self . _createHeaders ( ) <EOL> def linkDown ( _ ) : <EOL> log . msg ( '<STR_LIT>' % ( source_target , dest_target ) , system = self . log_system ) <EOL> def error ( failure ) : <EOL> log . msg ( '<STR_LIT>' % ( source_target , dest_target ) , system = self . log_system ) <EOL> log . msg ( '<STR_LIT>' % _extractErrorMessage ( failure ) , system = self . log_system ) <EOL> return failure <EOL> d = httpclient . httpRequest ( service_url , None , headers , method = '<STR_LIT>' , timeout = NCS_TIMEOUT ) <EOL> d . addCallbacks ( linkDown , error ) <EOL> return d <EOL> def NCSVPNBackend ( 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> ncs_services_url = str ( cfg [ config . NCS_SERVICES_URL ] ) <EOL> user = cfg [ config . NCS_USER ] <EOL> password = cfg [ config . NCS_PASSWORD ] <EOL> cm = NCSVPNConnectionManager ( ncs_services_url , user , password , port_map , name ) <EOL> return genericbackend . GenericBackend ( network_name , nrm_map , cm , parent_requester , name ) </s>
