<s> from networking_cisco . plugins . ml2 . drivers . cisco . n1kv import exceptions as c_exc <EOL> from networking_cisco . plugins . ml2 . drivers . cisco . n1kv import n1kv_client <EOL> _resource_metadata = { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:description>' , '<STR_LIT:id>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT:port>' : [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:name>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:description>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT>' ] } <EOL> class TestClient ( n1kv_client . Client ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . broken = False <EOL> self . inject_params = False <EOL> self . total_profiles = <NUM_LIT:2> <EOL> self . shared_net = False <EOL> self . upd_shared_net = False <EOL> super ( TestClient , self ) . __init__ ( ) <EOL> def _get_total_profiles ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . total_profiles <EOL> def _do_request ( self , method , action , body = None , headers = None , <EOL> vsm_ip = None ) : <EOL> """<STR_LIT>""" <EOL> if self . broken : <EOL> raise c_exc . VSMError ( reason = '<STR_LIT>' ) <EOL> if self . inject_params and body : <EOL> body [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if method == '<STR_LIT:POST>' and self . shared_net : <EOL> return _validate_shared_net_resource ( action , body ) <EOL> elif ( method == '<STR_LIT:POST>' and self . upd_shared_net and <EOL> '<STR_LIT>' in action ) : <EOL> ret = _validate_resource ( action , body ) <EOL> self . shared_net = True <EOL> return ret <EOL> elif method == '<STR_LIT:POST>' : <EOL> return _validate_resource ( action , body ) <EOL> elif method == '<STR_LIT:GET>' : <EOL> if '<STR_LIT>' in action : <EOL> return _policy_profile_generator ( <EOL> self . _get_total_profiles ( ) ) <EOL> else : <EOL> raise c_exc . VSMError ( reason = '<STR_LIT>' ) <EOL> class TestClientInvalidRequest ( TestClient ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( TestClientInvalidRequest , self ) . __init__ ( ) <EOL> self . inject_params = True <EOL> class TestClientInvalidResponse ( TestClient ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( TestClientInvalidResponse , self ) . __init__ ( ) <EOL> self . broken = True <EOL> class TestClientSharedNetwork ( TestClient ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( TestClientSharedNetwork , self ) . __init__ ( ) <EOL> self . shared_net = True <EOL> class TestClientUpdateSharedNetwork ( TestClient ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( TestClientUpdateSharedNetwork , self ) . __init__ ( ) <EOL> self . upd_shared_net = True <EOL> class TestClientVSMRetry ( n1kv_client . Client ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( TestClientVSMRetry , self ) . __init__ ( ) <EOL> self . pool . spawn = self . _fake_pool_spawn <EOL> def _fake_pool_spawn ( self ) : <EOL> pass <EOL> def _validate_resource ( action , body = None ) : <EOL> """<STR_LIT>""" <EOL> if body : <EOL> body_set = set ( body . keys ( ) ) <EOL> else : <EOL> return <EOL> if '<STR_LIT>' in action and '<STR_LIT:port>' not in action : <EOL> vmnetwork_set = set ( _resource_metadata [ '<STR_LIT>' ] ) <EOL> if body_set - vmnetwork_set : <EOL> raise c_exc . VSMError ( reason = '<STR_LIT>' ) <EOL> elif '<STR_LIT>' in action : <EOL> network_set = set ( _resource_metadata [ '<STR_LIT>' ] ) <EOL> if body_set - network_set : <EOL> raise c_exc . VSMError ( reason = '<STR_LIT>' ) <EOL> elif '<STR_LIT:port>' in action : <EOL> port_set = set ( _resource_metadata [ '<STR_LIT:port>' ] ) <EOL> if body_set - port_set : <EOL> raise c_exc . VSMError ( reason = '<STR_LIT>' ) <EOL> elif '<STR_LIT>' in action : <EOL> subnet_set = set ( _resource_metadata [ '<STR_LIT>' ] ) <EOL> if body_set - subnet_set : <EOL> raise c_exc . VSMError ( reason = '<STR_LIT>' ) <EOL> else : <EOL> return <EOL> def _validate_shared_net_resource ( action , body = None ) : <EOL> """<STR_LIT>""" <EOL> if body : <EOL> body_set = set ( body . keys ( ) ) <EOL> else : <EOL> return <EOL> if '<STR_LIT>' in action and '<STR_LIT:port>' not in action : <EOL> vmnetwork_set = set ( _resource_metadata [ '<STR_LIT>' ] ) <EOL> if body_set - vmnetwork_set : <EOL> raise c_exc . VSMError ( reason = '<STR_LIT>' ) <EOL> elif '<STR_LIT:port>' in action : <EOL> port_set = set ( _resource_metadata [ '<STR_LIT:port>' ] ) <EOL> if body_set - port_set : <EOL> raise c_exc . VSMError ( reason = '<STR_LIT>' ) <EOL> elif '<STR_LIT>' in action : <EOL> subnet_set = set ( _resource_metadata [ '<STR_LIT>' ] ) <EOL> if body_set - subnet_set : <EOL> raise c_exc . VSMError ( reason = '<STR_LIT>' ) <EOL> elif '<STR_LIT>' in action : <EOL> network_set = set ( _resource_metadata [ '<STR_LIT>' ] ) <EOL> if body_set - network_set : <EOL> raise c_exc . VSMError ( reason = '<STR_LIT>' ) <EOL> if body [ '<STR_LIT>' ] != '<STR_LIT:0>' : <EOL> raise c_exc . VSMError ( reason = '<STR_LIT>' ) <EOL> else : <EOL> return <EOL> def _policy_profile_generator ( total_profiles ) : <EOL> """<STR_LIT>""" <EOL> profiles = { } <EOL> for num in range ( <NUM_LIT:1> , total_profiles + <NUM_LIT:1> ) : <EOL> name = "<STR_LIT>" % num <EOL> profile_id = "<STR_LIT>" % num <EOL> profiles [ name ] = { "<STR_LIT>" : { "<STR_LIT:name>" : name , "<STR_LIT:id>" : profile_id } } <EOL> return profiles <EOL> def _policy_profile_generator_xml ( total_profiles ) : <EOL> """<STR_LIT>""" <EOL> xml = [ """<STR_LIT>""" ] <EOL> template = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> xml . extend ( template % { '<STR_LIT>' : n } for n in range ( <NUM_LIT:1> , total_profiles + <NUM_LIT:1> ) ) <EOL> xml . append ( "<STR_LIT>" ) <EOL> return '<STR_LIT>' . join ( xml ) </s>
