<s> import socket <EOL> import sys <EOL> from oslo_config import cfg <EOL> from oslo_serialization import jsonutils <EOL> from oslo_utils import importutils <EOL> from neutron . common import config as common_config <EOL> from dragonflow . common import common_params <EOL> from dragonflow . common import exceptions as df_exceptions <EOL> cfg . CONF . register_opts ( common_params . df_opts , '<STR_LIT>' ) <EOL> db_tables = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> usage_str = "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" <EOL> def print_tables ( ) : <EOL> print ( '<STR_LIT:U+0020>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> for table in db_tables : <EOL> print table <EOL> print ( '<STR_LIT:U+0020>' ) <EOL> def print_table ( db_driver , table ) : <EOL> try : <EOL> keys = db_driver . get_all_keys ( table ) <EOL> except df_exceptions . DBKeyNotFound : <EOL> keys = [ ] <EOL> print ( '<STR_LIT:U+0020>' ) <EOL> print ( '<STR_LIT>' + table ) <EOL> print ( '<STR_LIT>' ) <EOL> for key in keys : <EOL> print key <EOL> print ( '<STR_LIT:U+0020>' ) <EOL> def print_whole_table ( db_driver , table ) : <EOL> try : <EOL> keys = db_driver . get_all_keys ( table ) <EOL> except df_exceptions . DBKeyNotFound : <EOL> print ( '<STR_LIT>' + table ) <EOL> return <EOL> print ( '<STR_LIT:U+0020>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' + table ) <EOL> print ( '<STR_LIT>' ) <EOL> for key in keys : <EOL> value = db_driver . get_key ( table , key ) <EOL> if value : <EOL> print ( '<STR_LIT>' + key + '<STR_LIT>' + value ) <EOL> print ( '<STR_LIT:U+0020>' ) <EOL> def print_key ( db_driver , table , key ) : <EOL> try : <EOL> value = db_driver . get_key ( table , key ) <EOL> except df_exceptions . DBKeyNotFound : <EOL> print ( '<STR_LIT>' + table ) <EOL> return <EOL> print ( '<STR_LIT:U+0020>' ) <EOL> print ( '<STR_LIT>' + table + '<STR_LIT>' + key ) <EOL> print ( '<STR_LIT>' ) <EOL> print value <EOL> print ( '<STR_LIT:U+0020>' ) <EOL> def bind_port_to_localhost ( db_driver , port_id ) : <EOL> lport_str = db_driver . get_key ( '<STR_LIT>' , port_id ) <EOL> lport = jsonutils . loads ( lport_str ) <EOL> chassis_name = socket . gethostname ( ) <EOL> lport [ '<STR_LIT>' ] = chassis_name <EOL> lport_json = jsonutils . dumps ( lport ) <EOL> db_driver . set_key ( '<STR_LIT>' , port_id , lport_json ) <EOL> def clean_whole_table ( db_driver , table ) : <EOL> try : <EOL> keys = db_driver . get_all_keys ( table ) <EOL> except df_exceptions . DBKeyNotFound : <EOL> print ( '<STR_LIT>' + table ) <EOL> return <EOL> for key in keys : <EOL> db_driver . delete_key ( table , key ) <EOL> print ( '<STR_LIT>' ) <EOL> def remove_record ( db_driver , table , key ) : <EOL> try : <EOL> db_driver . delete_key ( table , key ) <EOL> except df_exceptions . DBKeyNotFound : <EOL> print ( '<STR_LIT>' % ( key , table ) ) <EOL> def main ( ) : <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> print usage_str <EOL> return <EOL> common_config . init ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> db_driver_class = importutils . import_class ( cfg . CONF . df . nb_db_class ) <EOL> db_driver = db_driver_class ( ) <EOL> db_driver . initialize ( db_ip = cfg . CONF . df . remote_db_ip , <EOL> db_port = cfg . CONF . df . remote_db_port , <EOL> config = cfg . CONF . df ) <EOL> action = sys . argv [ <NUM_LIT:1> ] <EOL> if action == '<STR_LIT>' : <EOL> if len ( sys . argv ) == <NUM_LIT:2> : <EOL> print_tables ( ) <EOL> return <EOL> table = sys . argv [ <NUM_LIT:2> ] <EOL> if table not in db_tables : <EOL> print "<STR_LIT>" <EOL> print db_tables <EOL> return <EOL> print_table ( db_driver , table ) <EOL> return <EOL> if action == '<STR_LIT>' : <EOL> if len ( sys . argv ) < <NUM_LIT:4> : <EOL> print "<STR_LIT>" <EOL> print usage_str <EOL> return <EOL> table = sys . argv [ <NUM_LIT:2> ] <EOL> if table not in db_tables : <EOL> print "<STR_LIT>" <EOL> print db_tables <EOL> return <EOL> key = sys . argv [ <NUM_LIT:3> ] <EOL> print_key ( db_driver , table , key ) <EOL> return <EOL> if action == '<STR_LIT>' : <EOL> for table in db_tables : <EOL> print_whole_table ( db_driver , table ) <EOL> return <EOL> if action == '<STR_LIT>' : <EOL> port_id = sys . argv [ <NUM_LIT:2> ] <EOL> bind_port_to_localhost ( db_driver , port_id ) <EOL> return <EOL> if action == '<STR_LIT>' : <EOL> for table in db_tables : <EOL> clean_whole_table ( db_driver , table ) <EOL> if action == '<STR_LIT>' : <EOL> if len ( sys . argv ) < <NUM_LIT:4> : <EOL> print "<STR_LIT>" <EOL> print usage_str <EOL> return <EOL> table = sys . argv [ <NUM_LIT:2> ] <EOL> if table not in db_tables : <EOL> print "<STR_LIT>" % db_tables <EOL> return <EOL> key = sys . argv [ <NUM_LIT:3> ] <EOL> remove_record ( db_driver , table , key ) <EOL> return <EOL> print usage_str <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
