<s> import re <EOL> import six <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from oslo_serialization import jsonutils <EOL> import requests <EOL> from networking_cisco . _i18n import _ <EOL> from neutron . plugins . ml2 import driver_api as api <EOL> LOG = logging . getLogger ( __name__ ) <EOL> ncs_opts = [ <EOL> cfg . StrOpt ( '<STR_LIT:url>' , <EOL> help = _ ( "<STR_LIT>" ) ) , <EOL> cfg . StrOpt ( '<STR_LIT:username>' , <EOL> help = _ ( "<STR_LIT>" ) ) , <EOL> cfg . StrOpt ( '<STR_LIT:password>' , secret = True , <EOL> help = _ ( "<STR_LIT>" ) ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , default = <NUM_LIT:10> , <EOL> help = _ ( "<STR_LIT>" ) ) <EOL> ] <EOL> cfg . CONF . register_opts ( ncs_opts , "<STR_LIT>" ) <EOL> class NCSMechanismDriver ( api . MechanismDriver ) : <EOL> """<STR_LIT>""" <EOL> out_of_sync = True <EOL> def initialize ( self ) : <EOL> self . url = cfg . CONF . ml2_ncs . url <EOL> self . timeout = cfg . CONF . ml2_ncs . timeout <EOL> self . username = cfg . CONF . ml2_ncs . username <EOL> self . password = cfg . CONF . ml2_ncs . password <EOL> def create_network_postcommit ( self , context ) : <EOL> self . synchronize ( '<STR_LIT>' , '<STR_LIT>' , context ) <EOL> def update_network_postcommit ( self , context ) : <EOL> self . synchronize ( '<STR_LIT>' , '<STR_LIT>' , context ) <EOL> def delete_network_postcommit ( self , context ) : <EOL> self . synchronize ( '<STR_LIT>' , '<STR_LIT>' , context ) <EOL> def create_subnet_postcommit ( self , context ) : <EOL> self . synchronize ( '<STR_LIT>' , '<STR_LIT>' , context ) <EOL> def update_subnet_postcommit ( self , context ) : <EOL> self . synchronize ( '<STR_LIT>' , '<STR_LIT>' , context ) <EOL> def delete_subnet_postcommit ( self , context ) : <EOL> self . synchronize ( '<STR_LIT>' , '<STR_LIT>' , context ) <EOL> def create_port_postcommit ( self , context ) : <EOL> self . synchronize ( '<STR_LIT>' , '<STR_LIT:port>' , context ) <EOL> def update_port_postcommit ( self , context ) : <EOL> self . synchronize ( '<STR_LIT>' , '<STR_LIT:port>' , context ) <EOL> def delete_port_postcommit ( self , context ) : <EOL> self . synchronize ( '<STR_LIT>' , '<STR_LIT:port>' , context ) <EOL> def synchronize ( self , operation , object_type , context ) : <EOL> """<STR_LIT>""" <EOL> if self . out_of_sync : <EOL> self . sync_full ( context ) <EOL> else : <EOL> self . sync_object ( operation , object_type , context ) <EOL> def sync_full ( self , context ) : <EOL> """<STR_LIT>""" <EOL> dbcontext = context . _plugin_context <EOL> networks = context . _plugin . get_networks ( dbcontext ) <EOL> subnets = context . _plugin . get_subnets ( dbcontext ) <EOL> ports = context . _plugin . get_ports ( dbcontext ) <EOL> for port in ports : <EOL> self . add_security_groups ( context , dbcontext , port ) <EOL> json = { '<STR_LIT>' : { '<STR_LIT>' : networks , <EOL> '<STR_LIT>' : subnets , <EOL> '<STR_LIT:port>' : ports } } <EOL> self . sendjson ( '<STR_LIT>' , '<STR_LIT>' , json ) <EOL> self . out_of_sync = False <EOL> def sync_object ( self , operation , object_type , context ) : <EOL> """<STR_LIT>""" <EOL> self . out_of_sync = True <EOL> dbcontext = context . _plugin_context <EOL> id = context . current [ '<STR_LIT:id>' ] <EOL> urlpath = object_type + '<STR_LIT:/>' + id <EOL> if operation == '<STR_LIT>' : <EOL> self . sendjson ( '<STR_LIT>' , urlpath , None ) <EOL> else : <EOL> assert operation == '<STR_LIT>' or operation == '<STR_LIT>' <EOL> if object_type == '<STR_LIT>' : <EOL> network = context . _plugin . get_network ( dbcontext , id ) <EOL> self . sendjson ( '<STR_LIT>' , urlpath , { '<STR_LIT>' : network } ) <EOL> elif object_type == '<STR_LIT>' : <EOL> subnet = context . _plugin . get_subnet ( dbcontext , id ) <EOL> self . sendjson ( '<STR_LIT>' , urlpath , { '<STR_LIT>' : subnet } ) <EOL> else : <EOL> assert object_type == '<STR_LIT:port>' <EOL> port = context . _plugin . get_port ( dbcontext , id ) <EOL> self . add_security_groups ( context , dbcontext , port ) <EOL> self . sendjson ( '<STR_LIT>' , urlpath , { '<STR_LIT:port>' : port } ) <EOL> self . out_of_sync = False <EOL> def add_security_groups ( self , context , dbcontext , port ) : <EOL> """<STR_LIT>""" <EOL> groups = [ context . _plugin . get_security_group ( dbcontext , sg ) <EOL> for sg in port [ '<STR_LIT>' ] ] <EOL> port [ '<STR_LIT>' ] = groups <EOL> def sendjson ( self , method , urlpath , obj ) : <EOL> obj = self . escape_keys ( obj ) <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> if obj is None : <EOL> data = None <EOL> else : <EOL> data = jsonutils . dumps ( obj , indent = <NUM_LIT:2> ) <EOL> auth = None <EOL> if self . username and self . password : <EOL> auth = ( self . username , self . password ) <EOL> if self . url : <EOL> url = '<STR_LIT:/>' . join ( [ self . url , urlpath ] ) <EOL> r = requests . request ( method , url = url , <EOL> headers = headers , data = data , <EOL> auth = auth , timeout = self . timeout ) <EOL> r . raise_for_status ( ) <EOL> def escape_keys ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , dict ) : <EOL> obj = dict ( ( self . escape ( k ) , self . escape_keys ( v ) ) <EOL> for k , v in six . iteritems ( obj ) ) <EOL> if isinstance ( obj , list ) : <EOL> obj = [ self . escape_keys ( x ) for x in obj ] <EOL> return obj <EOL> def escape ( self , string ) : <EOL> return re . sub ( '<STR_LIT>' , '<STR_LIT:->' , string ) </s>
