<s> from midonetclient . api import MidonetApi <EOL> from midonetclient . router import Router <EOL> from midonetclient import resource_base <EOL> from midonetclient import vendor_media_type <EOL> from midonetclient . bridge import Bridge <EOL> from midonetclient . chain import Chain <EOL> from midonetclient . host import Host <EOL> from midonetclient . host_interface_port import HostInterfacePort <EOL> from midonetclient . host_interface import HostInterface <EOL> from midonetclient . ip_addr_group import IpAddrGroup <EOL> from eucaops import Eucaops <EOL> from eutester import WaitForResultException <EOL> from eutester . sshconnection import SshConnection <EOL> from eutester . euinstance import EuInstance <EOL> from eutester . eulogger import Eulogger <EOL> from boto . ec2 . group import Group as BotoGroup <EOL> from boto . ec2 . instance import Instance <EOL> from boto . ec2 . securitygroup import SecurityGroup , IPPermissions <EOL> from prettytable import PrettyTable <EOL> import requests <EOL> import socket <EOL> import time <EOL> import re <EOL> import copy <EOL> class ArpTable ( resource_base . ResourceBase ) : <EOL> def __init__ ( self , uri , dto , auth ) : <EOL> super ( ArpTable , self ) . __init__ ( uri , dto , auth ) <EOL> def get_ip ( self ) : <EOL> return self . dto . get ( '<STR_LIT>' ) <EOL> def get_mac ( self ) : <EOL> return self . dto . get ( '<STR_LIT>' ) <EOL> def get_macaddr ( self ) : <EOL> return self . dto . get ( '<STR_LIT>' ) <EOL> class MacTable ( resource_base . ResourceBase ) : <EOL> def __init__ ( self , uri , dto , auth ) : <EOL> super ( MacTable , self ) . __init__ ( uri , dto , auth ) <EOL> def get_port_id ( self ) : <EOL> return self . dto . get ( '<STR_LIT>' ) <EOL> def get_bridge_id ( self ) : <EOL> return self . dto . get ( '<STR_LIT>' ) <EOL> def get_vlan_id ( self ) : <EOL> return self . dto . get ( '<STR_LIT>' ) <EOL> def get_macaddr ( self ) : <EOL> return self . dto . get ( '<STR_LIT>' ) <EOL> class Midget ( object ) : <EOL> '''<STR_LIT>''' <EOL> _CHAIN_JUMP = <NUM_LIT> <EOL> _ADDR_SPACING = <NUM_LIT:16> <EOL> def __init__ ( self , midonet_api_host , midonet_api_port = '<STR_LIT>' , midonet_username = None , <EOL> midonet_password = None , eutester_config = None , eutester_password = None , tester = None ) : <EOL> self . midonet_api_host = midonet_api_host <EOL> self . midonet_api_port = midonet_api_port <EOL> self . midonet_username = midonet_username <EOL> self . midonet_password = midonet_password <EOL> self . mapi = MidonetApi ( base_uri = '<STR_LIT>' <EOL> . format ( self . midonet_api_host , self . midonet_api_port ) , <EOL> username = self . midonet_username , password = self . midonet_password ) <EOL> self . tester = tester <EOL> if not self . tester : <EOL> self . tester = Eucaops ( config_file = eutester_config , password = eutester_password ) <EOL> self . logger = Eulogger ( identifier = '<STR_LIT>' . format ( self . midonet_api_host ) ) <EOL> self . default_indent = "<STR_LIT>" <EOL> self . _euca_instances = { } <EOL> self . _protocols = { } <EOL> def debug ( self , msg ) : <EOL> self . logger . log . debug ( msg ) <EOL> def _indent_table_buf ( self , table , indent = None ) : <EOL> if indent is None : <EOL> indent = self . default_indent <EOL> buf = str ( table ) <EOL> ret_buf = "<STR_LIT>" <EOL> for line in buf . splitlines ( ) : <EOL> ret_buf += '<STR_LIT>' . format ( indent , line ) <EOL> return ret_buf <EOL> def _link_table_buf ( self , table , indent = <NUM_LIT:4> ) : <EOL> if not table : <EOL> return None <EOL> if indent < <NUM_LIT:2> : <EOL> indent = <NUM_LIT:2> <EOL> preline = "<STR_LIT>" <EOL> linkpoint = "<STR_LIT>" <EOL> linkline = "<STR_LIT>" . format ( self . _bold ( "<STR_LIT:|>" , <NUM_LIT> ) ) <EOL> for x in xrange ( <NUM_LIT:0> , indent - <NUM_LIT:1> ) : <EOL> linkpoint += "<STR_LIT:->" <EOL> for x in xrange ( <NUM_LIT:0> , indent + <NUM_LIT:1> ) : <EOL> preline += "<STR_LIT:U+0020>" <EOL> linkline += self . _bold ( "<STR_LIT>" . format ( linkpoint ) , <NUM_LIT> ) <EOL> lines = str ( table ) . splitlines ( ) <EOL> ret_buf = "<STR_LIT>" . format ( linkline , lines [ <NUM_LIT:0> ] ) <EOL> for line in lines [ <NUM_LIT:1> : ] : <EOL> ret_buf += '<STR_LIT>' . format ( preline , line ) <EOL> return ret_buf <EOL> def _errmsg ( self , text ) : <EOL> self . debug ( self . _bold ( text ) , <NUM_LIT> ) <EOL> def _header ( self , text ) : <EOL> return self . tester . markup ( text = text , markups = [ <NUM_LIT:1> , <NUM_LIT> ] ) <EOL> def _bold ( self , text , value = <NUM_LIT:1> ) : <EOL> return self . tester . markup ( text = text , markups = [ value ] ) <EOL> def _highlight_buf_for_instance ( self , buf , instance ) : <EOL> ret_buf = "<STR_LIT>" <EOL> for line in str ( buf ) . splitlines ( ) : <EOL> searchstring = "<STR_LIT>" . format ( instance . id , <EOL> instance . private_ip_address , <EOL> instance . ip_address ) <EOL> try : <EOL> searchstring = "<STR_LIT>" . format ( <EOL> searchstring , <EOL> self . tester . ec2 . get_all_subnets ( instance . subnet_id ) [ <NUM_LIT:0> ] . cidr_block ) <EOL> except : pass <EOL> for match in re . findall ( searchstring , line ) : <EOL> line = line . replace ( match , self . _bold ( match , <NUM_LIT> ) ) <EOL> ret_buf += line + "<STR_LIT:\n>" <EOL> return ret_buf <EOL> @ property <EOL> def protocols ( self ) : <EOL> if not self . _protocols : <EOL> proto_dict = { } <EOL> for attr in dir ( socket ) : <EOL> if attr . startswith ( '<STR_LIT>' ) : <EOL> proto_dict [ str ( getattr ( socket , attr ) ) ] = attr . replace ( '<STR_LIT>' , '<STR_LIT>' ) . upper ( ) <EOL> self . _protocols = proto_dict <EOL> return self . _protocols <EOL> def _get_protocol_name_by_number ( self , number ) : <EOL> return self . protocols . get ( str ( number ) , str ( number ) ) <EOL> def _get_instance ( self , instance ) : <EOL> fetched_ins = None <EOL> if not isinstance ( instance , Instance ) : <EOL> if isinstance ( instance , str ) : <EOL> fetched_ins = self . _get_instance_by_id ( id = instance ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( instance , type ( instance ) ) ) <EOL> if not fetched_ins : <EOL> raise ValueError ( '<STR_LIT>' . format ( instance ) ) <EOL> instance = fetched_ins <EOL> return instance <EOL> def _ping_instance_private_ip_from_euca_internal ( self , <EOL> instance , <EOL> proxy_machine , <EOL> net_namespace = None ) : <EOL> instance = self . _get_instance ( instance ) <EOL> try : <EOL> proxy_machine . machine . ping_check ( instance . private_ip_address , <EOL> net_namespace = net_namespace ) <EOL> return True <EOL> except Exception , PE : <EOL> self . debug ( '<STR_LIT>' . format ( PE ) ) <EOL> self . debug ( '<STR_LIT>' <EOL> . format ( instance . id , <EOL> instance . private_ip_address , <EOL> proxy_machine . hostname ) ) <EOL> return False <EOL> def ping_instance_private_ip_from_euca_internal ( self , <EOL> instance , <EOL> proxy_machine = None , <EOL> net_namespace = None , <EOL> ping_timeout = <NUM_LIT:5> ) : <EOL> instance = self . _get_instance ( instance ) <EOL> if not proxy_machine : <EOL> clc_service = self . tester . service_manager . get_all_cloud_controllers ( ) [ <NUM_LIT:0> ] <EOL> proxy_machine = copy . copy ( clc_service ) <EOL> net_namespace = net_namespace or instance . vpc_id <EOL> try : <EOL> self . tester . wait_for_result ( self . _ping_instance_private_ip_from_euca_internal , <EOL> result = True , <EOL> timeout = ping_timeout , <EOL> instance = instance , <EOL> proxy_machine = proxy_machine , <EOL> net_namespace = net_namespace ) <EOL> except WaitForResultException : <EOL> self . errormsg ( '<STR_LIT>' <EOL> . format ( instance . id , <EOL> instance . private_ip_address , <EOL> proxy_machine . hostname ) ) <EOL> self . errormsg ( '<STR_LIT>' ) <EOL> proxy_machine . machine . dump_netfail_info ( ip = instance . private_ip_address , <EOL> net_namespace = net_namespace ) <EOL> self . errormsg ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( instance . id , <EOL> instance . private_ip_address , <EOL> proxy_machine . hostname ) ) <EOL> raise <EOL> self . debug ( '<STR_LIT>' <EOL> . format ( instance . id , <EOL> instance . private_ip_address , <EOL> proxy_machine . hostname ) ) <EOL> def get_all_routers ( self , search_dict = { } , eval_op = re . search , query = None ) : <EOL> """<STR_LIT>""" <EOL> routers = self . mapi . get_routers ( query = None ) <EOL> remove_list = [ ] <EOL> for key in search_dict : <EOL> for router in routers : <EOL> if hasattr ( router , key ) : <EOL> try : <EOL> if eval_op ( str ( search_dict [ key ] ) , router . dto . get ( key ) ) : <EOL> continue <EOL> except : <EOL> self . debug ( '<STR_LIT>' <EOL> . format ( "<STR_LIT>" . format ( getattr ( eval_op , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> getattr ( eval_op , "<STR_LIT>" , "<STR_LIT>" ) ) , <EOL> str ( search_dict [ key ] ) , <EOL> str ( getattr ( router , key ) ) ) ) <EOL> raise <EOL> remove_list . append ( router ) <EOL> for router in remove_list : <EOL> if router in routers : <EOL> routers . remove ( router ) <EOL> return routers <EOL> def get_router_for_instance ( self , instance ) : <EOL> instance = self . _get_instance ( instance ) <EOL> self . debug ( '<STR_LIT>' . format ( instance . id , instance . vpc_id ) ) <EOL> routers = self . get_all_routers ( search_dict = { '<STR_LIT:name>' : instance . vpc_id } ) <EOL> if len ( routers ) != <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> . format ( instance . id , routers ) ) <EOL> router = routers [ <NUM_LIT:0> ] <EOL> self . debug ( '<STR_LIT>' . format ( router . get_name ( ) , instance . id ) ) <EOL> return router <EOL> def get_router_by_name ( self , name ) : <EOL> assert name <EOL> search_string = "<STR_LIT>" . format ( name ) <EOL> self . debug ( '<STR_LIT>' . format ( search_string ) ) <EOL> routers = self . get_all_routers ( search_dict = { '<STR_LIT:name>' : search_string } , eval_op = re . match ) <EOL> if routers : <EOL> return routers [ <NUM_LIT:0> ] <EOL> return None <EOL> def show_routers_brief ( self , routers = None , showchains = False , printme = True ) : <EOL> """<STR_LIT>""" <EOL> if routers is None : <EOL> routers = self . get_all_routers ( ) <EOL> if not isinstance ( routers , list ) : <EOL> routers = [ routers ] <EOL> headers = [ '<STR_LIT:Name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if showchains : <EOL> headers . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> pt = PrettyTable ( headers ) <EOL> for router in routers : <EOL> row = [ router . get_name ( ) , router . get_admin_state_up ( ) , router . get_id ( ) , <EOL> router . get_tenant_id ( ) ] <EOL> if showchains : <EOL> row . extend ( [ router . get_inbound_filter_id ( ) , router . get_outbound_filter_id ( ) ] ) <EOL> pt . add_row ( row ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( pt ) ) <EOL> else : <EOL> return pt <EOL> def show_routes ( self , routes , printme = True ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( routes , list ) : <EOL> routes = [ routes ] <EOL> pt = PrettyTable ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for route in routes : <EOL> pt . add_row ( [ '<STR_LIT>' . format ( route . get_dst_network_addr ( ) , <EOL> route . get_dst_network_length ( ) ) , <EOL> '<STR_LIT>' . format ( route . get_src_network_addr ( ) , <EOL> route . get_src_network_length ( ) ) , <EOL> route . get_next_hop_gateway ( ) , <EOL> route . get_next_hop_port ( ) , <EOL> route . get_weight ( ) , <EOL> route . get_id ( ) <EOL> ] ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( pt ) ) <EOL> else : <EOL> return pt <EOL> def show_routers ( self , routers = None , printme = True ) : <EOL> '''<STR_LIT>''' <EOL> buf = "<STR_LIT>" <EOL> if routers is None : <EOL> routers = self . get_all_routers ( ) <EOL> if not isinstance ( routers , list ) : <EOL> routers = [ routers ] <EOL> for router in routers : <EOL> buf += "<STR_LIT:\n>" + str ( self . show_router_summary ( router , showchains = False , printme = False ) ) + "<STR_LIT:\n>" <EOL> self . debug ( buf ) <EOL> def show_router_summary ( self , router , showchains = True , indent = None , printme = True ) : <EOL> """<STR_LIT>""" <EOL> if indent is None : <EOL> indent = self . default_indent <EOL> title = self . _header ( "<STR_LIT>" . format ( router . get_name ( ) ) ) <EOL> pt = PrettyTable ( [ title ] ) <EOL> pt . align [ title ] = '<STR_LIT:l>' <EOL> pt . padding_width = <NUM_LIT:0> <EOL> buf = self . _bold ( "<STR_LIT>" . format ( indent ) , <NUM_LIT:4> ) <EOL> buf += self . _indent_table_buf ( self . show_routers_brief ( routers = [ router ] , printme = False ) ) <EOL> buf += self . _bold ( "<STR_LIT>" . format ( indent ) , <NUM_LIT:4> ) <EOL> buf += self . _indent_table_buf ( self . show_routes ( routes = router . get_routes ( ) , printme = False ) ) <EOL> buf += self . _bold ( "<STR_LIT>" . format ( indent ) , <NUM_LIT:4> ) <EOL> buf += self . _indent_table_buf ( self . show_ports ( ports = router . get_ports ( ) , printme = False ) ) <EOL> if showchains : <EOL> if router . get_inbound_filter_id ( ) : <EOL> in_filter_id = str ( router . get_inbound_filter_id ( ) ) <EOL> in_filter = self . mapi . get_chain ( in_filter_id ) <EOL> buf += "<STR_LIT:\n>" + self . _bold ( "<STR_LIT>" <EOL> . format ( indent , in_filter_id ) , <NUM_LIT:4> ) <EOL> buf += self . _indent_table_buf ( self . show_chain ( chain = in_filter , printme = False ) ) <EOL> if router . get_outbound_filter_id ( ) : <EOL> out_filter_id = str ( router . get_outbound_filter_id ( ) ) <EOL> out_filter = self . mapi . get_chain ( out_filter_id ) <EOL> buf += "<STR_LIT:\n>" + self . _bold ( "<STR_LIT>" <EOL> . format ( indent , out_filter_id ) , <NUM_LIT:4> ) <EOL> buf += self . _indent_table_buf ( self . show_chain ( chain = out_filter , printme = False ) ) <EOL> pt . add_row ( [ buf ] ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( pt ) ) <EOL> else : <EOL> return pt <EOL> def get_device_by_peer_id ( self , peerid ) : <EOL> device = None <EOL> port = self . mapi . get_port ( peerid ) <EOL> type = str ( port . get_type ( ) ) . upper ( ) <EOL> if type == '<STR_LIT>' : <EOL> device = self . mapi . get_bridge ( port . get_device_id ( ) ) <EOL> if type == '<STR_LIT>' : <EOL> device = self . mapi . get_router ( port . get_device_id ( ) ) <EOL> if not device : <EOL> raise ValueError ( '<STR_LIT>' <EOL> . format ( peerid , port . get_id ( ) , port . get_type ( ) ) ) <EOL> return device <EOL> def get_router_port_for_subnet ( self , router , cidr ) : <EOL> assert cidr <EOL> for port in router . get_ports ( ) : <EOL> network = "<STR_LIT>" . format ( port . get_network_address ( ) , port . get_network_length ( ) ) <EOL> if str ( network ) == str ( cidr ) : <EOL> return port <EOL> return None <EOL> def get_bridge_for_instance ( self , instance ) : <EOL> instance = self . _get_instance ( instance ) <EOL> router = self . get_router_for_instance ( instance ) <EOL> if not router : <EOL> raise ValueError ( '<STR_LIT>' . format ( instance . id ) ) <EOL> subnet = self . tester . ec2 . get_all_subnets ( subnet_ids = [ '<STR_LIT>' , instance . subnet_id ] ) [ <NUM_LIT:0> ] <EOL> if not subnet : <EOL> raise ValueError ( '<STR_LIT>' <EOL> . format ( instance . id , instance . subnet_id ) ) <EOL> port = self . get_router_port_for_subnet ( router , subnet . cidr_block ) <EOL> if not port : <EOL> raise ValueError ( '<STR_LIT>' <EOL> . format ( instance . id , subnet . cidr_block ) ) <EOL> bridge = self . get_device_by_peer_id ( port . get_peer_id ( ) ) <EOL> if not isinstance ( bridge , Bridge ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return bridge <EOL> def show_port_summary ( self , port , showchains = True , showbgp = True , indent = None , printme = True ) : <EOL> if indent is None : <EOL> indent = self . default_indent <EOL> title = self . _bold ( "<STR_LIT>" . format ( port . get_id ( ) ) , <NUM_LIT> ) <EOL> titlept = PrettyTable ( [ title ] ) <EOL> titlept . align [ title ] = '<STR_LIT:l>' <EOL> buf = self . _bold ( "<STR_LIT>" . format ( indent ) , <NUM_LIT:4> ) <EOL> pt = PrettyTable ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> pt . align [ '<STR_LIT>' ] = '<STR_LIT:l>' <EOL> pt . max_width [ '<STR_LIT>' ] = <NUM_LIT:20> <EOL> bgps = <NUM_LIT:0> <EOL> try : <EOL> if port . dto . get ( '<STR_LIT>' ) : <EOL> bgps = port . get_bgps ( ) <EOL> if bgps : <EOL> bgps = len ( bgps ) <EOL> else : <EOL> bgps = <NUM_LIT:0> <EOL> except Exception , E : <EOL> bgps = '<STR_LIT>' <EOL> self . debug ( '<STR_LIT>' . format ( port . get_id ( ) , E ) ) <EOL> pt . add_row ( [ port . get_id ( ) , <EOL> bgps , <EOL> port . get_port_address ( ) , <EOL> "<STR_LIT>" . format ( port . get_network_address ( ) , port . get_network_length ( ) ) , <EOL> port . get_port_mac ( ) , <EOL> port . get_type ( ) , <EOL> port . get_admin_state_up ( ) , <EOL> port . get_peer_id ( ) ] ) <EOL> buf += self . _indent_table_buf ( str ( pt ) ) <EOL> if showbgp and bgps : <EOL> buf += self . _bold ( "<STR_LIT>" . format ( indent ) , <NUM_LIT:4> ) <EOL> buf += self . _indent_table_buf ( str ( self . show_bgps ( port . get_bgps ( ) or [ ] ) ) ) <EOL> if showchains : <EOL> if port . get_inbound_filter_id ( ) : <EOL> in_filter = self . mapi . get_chain ( str ( port . get_inbound_filter_id ( ) ) ) <EOL> buf += self . _bold ( "<STR_LIT>" . format ( indent ) , <NUM_LIT:4> ) <EOL> buf += "<STR_LIT:\n>" <EOL> buf += self . _indent_table_buf ( self . show_chain ( chain = in_filter , printme = False ) ) <EOL> if port . get_outbound_filter_id ( ) : <EOL> out_filter = self . mapi . get_chain ( str ( port . get_outbound_filter_id ( ) ) ) <EOL> buf += self . _bold ( "<STR_LIT>" . format ( indent ) , <NUM_LIT:4> ) <EOL> buf += "<STR_LIT:\n>" <EOL> buf += self . _indent_table_buf ( self . show_chain ( chain = out_filter , printme = False ) ) <EOL> titlept . add_row ( [ buf ] ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( titlept ) ) <EOL> else : <EOL> return titlept <EOL> def show_ports ( self , ports , printme = True ) : <EOL> """<STR_LIT>""" <EOL> buf = "<STR_LIT>" <EOL> if not isinstance ( ports , list ) : <EOL> ports = [ ports ] <EOL> pt = None <EOL> for port in ports : <EOL> pt = PrettyTable ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> pt . padding_width = <NUM_LIT:0> <EOL> bgps = <NUM_LIT:0> <EOL> try : <EOL> if port . dto . get ( '<STR_LIT>' ) : <EOL> bgps = port . get_bgps ( ) <EOL> if bgps : <EOL> bgps = len ( bgps ) <EOL> else : <EOL> bgps = <NUM_LIT:0> <EOL> except Exception , E : <EOL> bgps = '<STR_LIT>' <EOL> self . debug ( '<STR_LIT>' . format ( port . get_id ( ) , E ) ) <EOL> pt . add_row ( [ port . get_id ( ) , <EOL> bgps , <EOL> port . get_port_address ( ) , <EOL> "<STR_LIT>" . format ( port . get_network_address ( ) , port . get_network_length ( ) ) , <EOL> port . get_port_mac ( ) , <EOL> port . get_type ( ) , <EOL> port . get_admin_state_up ( ) , <EOL> port . get_peer_id ( ) ] ) <EOL> if bgps and bgps != "<STR_LIT>" : <EOL> lines = [ ] <EOL> for line in str ( pt ) . splitlines ( ) : <EOL> line = line . strip ( ) <EOL> if line : <EOL> lines . append ( line ) <EOL> footer = lines [ - <NUM_LIT:1> ] <EOL> buf += "<STR_LIT:\n>" . join ( lines ) + '<STR_LIT:\n>' <EOL> pt = None <EOL> buf += self . _link_table_buf ( self . show_bgps ( port . get_bgps ( ) , printme = False ) ) <EOL> buf += footer + '<STR_LIT:\n>' <EOL> if pt : <EOL> buf += str ( pt ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( pt ) ) <EOL> else : <EOL> return buf <EOL> def show_bgps ( self , bgps , printme = True ) : <EOL> buf = "<STR_LIT>" <EOL> if not isinstance ( bgps , list ) : <EOL> bgps = [ bgps ] <EOL> port = None <EOL> pt = None <EOL> for bgp in bgps : <EOL> if bgp . dto . get ( '<STR_LIT>' ) != port : <EOL> port = bgp . dto . get ( '<STR_LIT>' ) <EOL> if pt is not None : <EOL> buf += str ( pt ) <EOL> port_header = '<STR_LIT>' . format ( port ) <EOL> pt = PrettyTable ( [ port_header , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> pt . max_width [ port_header ] = len ( port ) or len ( '<STR_LIT>' ) <EOL> pt . align [ port_header ] = '<STR_LIT:l>' <EOL> pt . add_row ( [ port , <EOL> bgp . get_id ( ) , <EOL> bgp . get_peer_addr ( ) , <EOL> bgp . get_local_as ( ) , <EOL> bgp . get_peer_as ( ) , <EOL> self . _format_ad_routes ( bgp . get_ad_routes ( ) ) ] ) <EOL> buf += str ( pt ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( buf ) ) <EOL> else : <EOL> return buf <EOL> def _format_ad_routes ( self , ad_routes ) : <EOL> adrs = [ ] <EOL> if not isinstance ( ad_routes , list ) : <EOL> ad_routes = [ ad_routes ] <EOL> for adr in ad_routes : <EOL> adrs . append ( '<STR_LIT>' . format ( adr . get_nw_prefix ( ) , adr . get_prefix_length ( ) ) ) <EOL> return adrs <EOL> def _update_euca_instances ( self ) : <EOL> instances = self . tester . get_instances ( idstring = [ '<STR_LIT>' ] ) <EOL> now = time . time ( ) <EOL> self . _euca_instances = { '<STR_LIT>' : now , '<STR_LIT>' : instances } <EOL> @ property <EOL> def euca_instances ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _euca_instances : <EOL> if time . time ( ) - self . _euca_instances . get ( '<STR_LIT>' , <NUM_LIT:0> ) < <NUM_LIT:5> : <EOL> return self . _euca_instances [ '<STR_LIT>' ] <EOL> self . _update_euca_instances ( ) <EOL> return self . _euca_instances [ '<STR_LIT>' ] <EOL> def _get_instance_by_id ( self , id ) : <EOL> for x in xrange ( <NUM_LIT:0> , <NUM_LIT:2> ) : <EOL> for instance in self . euca_instances : <EOL> if instance . id == id : <EOL> return instance <EOL> self . _update_euca_instances ( ) <EOL> return None <EOL> def _get_instance_by_private_ip ( self , private_ip ) : <EOL> for instance in self . euca_instances : <EOL> if instance . private_ip_address == private_ip : <EOL> return instance <EOL> return None <EOL> def _get_instance_by_public_ip ( self , public_ip ) : <EOL> for instance in self . euca_instances : <EOL> if instance . ip_address == public_ip : <EOL> return instance <EOL> return None <EOL> def show_bridges ( self , bridges = None , indent = None , printme = True ) : <EOL> if indent is None : <EOL> indent = self . default_indent <EOL> if bridges : <EOL> if not isinstance ( bridges , list ) : <EOL> bridges = [ bridges ] <EOL> else : <EOL> bridges = self . mapi . get_bridges ( query = None ) <EOL> printbuf = "<STR_LIT>" <EOL> for bridge in bridges : <EOL> buf = "<STR_LIT>" <EOL> pt = PrettyTable ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> pt . add_row ( [ bridge . get_name ( ) , bridge . get_id ( ) , bridge . get_tenant_id ( ) , <EOL> bridge . get_vxlan_port ( ) ] ) <EOL> title = self . _header ( '<STR_LIT>' . format ( bridge . get_name ( ) ) ) <EOL> box = PrettyTable ( [ title ] ) <EOL> box . align [ title ] = '<STR_LIT:l>' <EOL> buf += self . _bold ( "<STR_LIT>" . format ( indent ) , <NUM_LIT:4> ) <EOL> buf += self . _indent_table_buf ( str ( pt ) ) <EOL> buf += self . _bold ( "<STR_LIT>" . format ( indent ) , <NUM_LIT:4> ) <EOL> buf += self . _indent_table_buf ( self . show_ports ( bridge . get_ports ( ) , printme = False ) ) <EOL> buf += self . _bold ( "<STR_LIT>" . format ( indent ) , <NUM_LIT:4> ) <EOL> buf += self . _indent_table_buf ( self . show_bridge_arp_table ( bridge = bridge , printme = False ) ) <EOL> buf += self . _bold ( "<STR_LIT>" . format ( indent ) ) <EOL> buf += self . _indent_table_buf ( self . show_bridge_dhcp_subnets ( bridge , printme = False ) ) <EOL> buf += self . _bold ( "<STR_LIT>" . format ( indent ) ) <EOL> buf += self . _indent_table_buf ( self . show_bridge_mac_table ( bridge = bridge , printme = False ) ) <EOL> box . add_row ( [ buf ] ) <EOL> printbuf += str ( box ) + "<STR_LIT:\n>" <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( printbuf ) ) <EOL> else : <EOL> return printbuf <EOL> def show_bridge_dhcp_subnets ( self , bridge , printme = True ) : <EOL> pt = PrettyTable ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for subnet in bridge . get_dhcp_subnets ( ) : <EOL> pt . add_row ( [ "<STR_LIT>" . format ( subnet . get_subnet_prefix ( ) , subnet . get_subnet_length ( ) ) , <EOL> subnet . get_server_addr ( ) , <EOL> subnet . get_default_gateway ( ) , <EOL> "<STR_LIT:U+002C>" . join ( str ( dns ) for dns in subnet . get_dns_server_addrs ( ) ) , <EOL> subnet . dto . get ( '<STR_LIT>' ) ] ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( pt ) ) <EOL> else : <EOL> return pt <EOL> def get_bridge_arp_table ( self , bridge ) : <EOL> table = bridge . get_children ( bridge . dto [ '<STR_LIT>' ] , <EOL> query = None , <EOL> headers = { "<STR_LIT>" : "<STR_LIT>" } , <EOL> clazz = ArpTable ) <EOL> return table <EOL> def get_bridge_mac_table ( self , bridge ) : <EOL> table = bridge . get_children ( bridge . dto [ '<STR_LIT>' ] , <EOL> query = None , <EOL> headers = { "<STR_LIT>" : "<STR_LIT>" } , <EOL> clazz = MacTable ) <EOL> return table <EOL> def show_bridge_mac_table ( self , bridge , printme = True ) : <EOL> pt = PrettyTable ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> mac_table = self . get_bridge_mac_table ( bridge ) <EOL> for entry in mac_table : <EOL> assert isinstance ( entry , MacTable ) <EOL> pt . add_row ( [ entry . get_bridge_id ( ) , entry . get_macaddr ( ) , entry . get_port_id ( ) , <EOL> entry . get_vlan_id ( ) ] ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( pt ) ) <EOL> else : <EOL> return pt <EOL> def show_bridge_arp_table ( self , bridge , printme = True ) : <EOL> pt = PrettyTable ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> table = self . get_bridge_arp_table ( bridge ) <EOL> mac_table = self . get_bridge_mac_table ( bridge ) <EOL> for entry in table : <EOL> instance_id = None <EOL> vm_host = None <EOL> entry_ip = entry . get_ip ( ) <EOL> port = "<STR_LIT>" <EOL> for mac in mac_table : <EOL> if mac . get_macaddr ( ) == entry . get_mac ( ) : <EOL> port = mac . get_port_id ( ) <EOL> if self . tester : <EOL> try : <EOL> euca_instance = self . _get_instance_by_private_ip ( private_ip = entry_ip ) <EOL> if euca_instance : <EOL> instance_id = self . _bold ( euca_instance . id ) <EOL> vm_host = euca_instance . tags . get ( '<STR_LIT>' , None ) <EOL> except : <EOL> raise <EOL> pt . add_row ( [ entry_ip , entry . get_mac ( ) , entry . get_macaddr ( ) , instance_id , vm_host , port ] ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( pt ) ) <EOL> else : <EOL> return pt <EOL> def get_bridge_port_for_instance_learned ( self , instance ) : <EOL> bridge = self . get_bridge_for_instance ( instance ) <EOL> arp_table = self . get_bridge_arp_table ( bridge ) <EOL> mac_table = self . get_bridge_mac_table ( bridge ) <EOL> arp_entry = None <EOL> for a_entry in arp_table : <EOL> if a_entry . get_ip ( ) == instance . private_ip_address : <EOL> arp_entry = a_entry <EOL> break <EOL> if arp_entry : <EOL> for m_entry in mac_table : <EOL> if m_entry . get_macaddr ( ) == arp_entry . get_mac ( ) : <EOL> portid = m_entry . get_port_id ( ) <EOL> return self . mapi . get_port ( portid ) <EOL> self . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return None <EOL> def get_bridge_port_for_instance_by_port_name ( self , instance ) : <EOL> host = self . get_host_for_instance ( instance ) <EOL> assert isinstance ( host , Host ) <EOL> for port in host . get_ports ( ) : <EOL> iname = port . get_interface_name ( ) <EOL> lookfor_name = '<STR_LIT>' + str ( instance . id ) <EOL> if re . search ( lookfor_name , iname ) : <EOL> return self . mapi . get_port ( port . get_port_id ( ) ) <EOL> return None <EOL> def show_bridge_port_for_instance ( self , instance , showchains = True , indent = None , printme = True ) : <EOL> if indent is None : <EOL> indent = self . default_indent <EOL> bridge = self . get_bridge_for_instance ( instance ) <EOL> learned = "<STR_LIT>" <EOL> port = self . get_bridge_port_for_instance_learned ( instance ) <EOL> if not port : <EOL> learned = "<STR_LIT>" <EOL> port = self . get_bridge_port_for_instance_by_port_name ( instance ) <EOL> title = self . _bold ( '<STR_LIT>' . format ( instance . id , <EOL> bridge . get_name ( ) or <EOL> bridge . get_id ( ) , <EOL> learned ) , <NUM_LIT> ) <EOL> pt = PrettyTable ( [ title ] ) <EOL> pt . align [ title ] = '<STR_LIT:l>' <EOL> buf = "<STR_LIT>" <EOL> if port : <EOL> buf += str ( self . show_port_summary ( port , showchains = showchains , printme = False ) ) <EOL> pt . add_row ( [ buf ] ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( pt ) ) <EOL> else : <EOL> return pt <EOL> def get_chain_by_name ( self , name ) : <EOL> chains = self . mapi . get_chains ( query = None ) <EOL> for chain in chains : <EOL> if str ( chain . get_name ( ) ) . strip ( ) == str ( name ) : <EOL> return chain <EOL> return None <EOL> def get_chain_by_id ( self , id ) : <EOL> chains = self . mapi . get_chains ( query = None ) <EOL> for chain in chains : <EOL> if str ( chain . get_id ( ) ) . strip ( ) == str ( id ) : <EOL> return chain <EOL> return None <EOL> def show_chain ( self , chain , printme = True ) : <EOL> if chain and isinstance ( chain , unicode ) or isinstance ( chain , str ) : <EOL> if re . match ( '<STR_LIT>' , chain ) : <EOL> chain = self . get_chain_by_id ( chain ) <EOL> else : <EOL> chain = self . get_chain_by_name ( chain ) <EOL> raise ValueError ( '<STR_LIT>' . format ( chain ) ) <EOL> if not isinstance ( chain , Chain ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> . format ( chain , type ( chain ) ) ) <EOL> title = '<STR_LIT>' . format ( chain . dto . get ( '<STR_LIT:name>' , "<STR_LIT>" ) , <EOL> self . _bold ( chain . get_id ( ) , <EOL> self . _CHAIN_JUMP ) , <EOL> chain . dto . get ( '<STR_LIT>' , "<STR_LIT>" ) ) <EOL> pt = PrettyTable ( [ title ] ) <EOL> pt . align [ title ] = '<STR_LIT:l>' <EOL> rules = chain . get_rules ( ) <EOL> if not rules : <EOL> pt . add_row ( [ '<STR_LIT>' ] ) <EOL> else : <EOL> rulesbuf = str ( self . show_rules ( rules = chain . get_rules ( ) , jump = True , printme = False ) ) <EOL> pt . add_row ( [ rulesbuf ] ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( pt ) ) <EOL> else : <EOL> return pt <EOL> def show_chains_for_instance_security_groups ( self , instance , printme = True ) : <EOL> if not instance . groups : <EOL> self . debug ( '<STR_LIT>' ) <EOL> mainbuf = self . _bold ( "<STR_LIT>" <EOL> . format ( instance . id ) ) <EOL> for group in instance . groups : <EOL> buf = "<STR_LIT>" <EOL> title = '<STR_LIT>' . format ( group . id , group . name ) <EOL> pt = PrettyTable ( [ title ] ) <EOL> pt . align [ title ] = '<STR_LIT:l>' <EOL> buf += "<STR_LIT:\n>" + str ( self . tester . show_security_group ( group , printme = False ) ) <EOL> buf += "<STR_LIT:\n>" + str ( self . show_chain_for_security_group ( group , printme = False ) ) <EOL> pt . add_row ( [ buf ] ) <EOL> mainbuf += "<STR_LIT:\n>" + str ( pt ) <EOL> if printme : <EOL> self . debug ( mainbuf ) <EOL> else : <EOL> return mainbuf <EOL> def get_chain_for_security_group ( self , group ) : <EOL> group_id = None <EOL> if isinstance ( group , SecurityGroup ) or isinstance ( group , BotoGroup ) : <EOL> group_id = str ( group . id ) <EOL> elif group : <EOL> if isinstance ( group , str ) or isinstance ( group , unicode ) : <EOL> if re . match ( '<STR_LIT>' , group ) : <EOL> group = self . tester . get_security_group ( id = group ) <EOL> self . _errmsg ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( group ) ) <EOL> group_id = str ( group ) <EOL> else : <EOL> group = self . tester . get_security_group ( name = group ) <EOL> if not group : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( group ) ) <EOL> if not group_id : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> . format ( group ) ) <EOL> chain_name = "<STR_LIT>" + str ( group_id ) <EOL> chain = self . get_chain_by_name ( name = chain_name ) <EOL> if not chain : <EOL> self . _errmsg ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> return chain <EOL> def show_chain_for_security_group ( self , group , printme = True ) : <EOL> chain = self . get_chain_for_security_group ( group ) <EOL> return self . show_chain ( chain , printme = printme ) <EOL> def does_chain_allow ( self , chain , src_addr , protocol , port ) : <EOL> if not isinstance ( chain , Chain ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> . format ( chain , type ( chain ) ) ) <EOL> if not src_addr or not protocol or not port : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( src_addr , protocol , port ) ) <EOL> protocol = str ( protocol ) . upper ( ) . strip ( ) <EOL> port = int ( port ) <EOL> for rule in chain . get_rules ( ) : <EOL> src_ip = str ( rule . get_nw_src_address ( ) or '<STR_LIT>' ) <EOL> src_mask = str ( rule . get_nw_src_length ( ) or <NUM_LIT:0> ) <EOL> rule_cidr = src_ip + "<STR_LIT:/>" + src_mask <EOL> rule_protocol = self . _get_protocol_name_by_number ( rule . get_nw_proto ( ) ) <EOL> port_dict = rule . get_tp_dst ( ) <EOL> start_port = port_dict . get ( '<STR_LIT:start>' ) <EOL> end_port = port_dict . get ( '<STR_LIT:end>' ) <EOL> if protocol == str ( rule_protocol ) . upper ( ) . strip ( ) : <EOL> if port >= start_port and port <= end_port : <EOL> if rule_cidr == "<STR_LIT>" or self . tester . is_address_in_network ( src_addr , <EOL> rule_cidr ) : <EOL> self . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( src_addr , protocol , port ) ) <EOL> self . show_rules ( rules = [ rule ] ) <EOL> return True <EOL> self . debug ( '<STR_LIT>' <EOL> . format ( src_addr , protocol , port ) ) <EOL> return False <EOL> def get_unsynced_rules_for_security_group ( self , group , show_rules = True ) : <EOL> chain = self . get_chain_for_security_group ( group ) <EOL> unsynced_rules = [ ] <EOL> for rule in group . rules : <EOL> if not self . get_security_group_rule_mapping_from_backend ( group , rule ) : <EOL> unsynced_rules . append ( rule ) <EOL> self . debug ( '<STR_LIT>' <EOL> . format ( len ( unsynced_rules ) , len ( group . rules ) , group . name ) ) <EOL> if unsynced_rules and show_rules : <EOL> title = self . tester . markup ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( group . name ) , [ <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:7> ] ) <EOL> main_pt = PrettyTable ( [ title ] ) <EOL> pt = PrettyTable ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for rule in unsynced_rules : <EOL> for grant in rule . grants : <EOL> pt . add_row ( [ grant . cidr_ip , grant . name , grant . group_id , rule . ip_protocol , <EOL> "<STR_LIT>" . format ( rule . from_port , rule . to_port ) ] ) <EOL> main_pt . add_row ( [ str ( pt ) ] ) <EOL> self . debug ( "<STR_LIT:\n>" + str ( main_pt ) + "<STR_LIT:\n>" ) <EOL> return unsynced_rules <EOL> def get_unsynced_rules_for_instance ( self , instance , show_rules = True ) : <EOL> unsynced_rules = [ ] <EOL> for group in instance . groups : <EOL> group = self . tester . get_security_group ( id = group . id ) <EOL> unsynced_rules . extend ( self . get_unsynced_rules_for_security_group ( group , <EOL> show_rules = show_rules ) ) <EOL> self . debug ( '<STR_LIT>' <EOL> . format ( instance . id , len ( unsynced_rules ) ) ) <EOL> return unsynced_rules <EOL> def get_security_group_rule_mapping_from_backend ( self , group , security_group_rule ) : <EOL> """<STR_LIT>""" <EOL> ret_rules = [ ] <EOL> assert isinstance ( security_group_rule , IPPermissions ) , '<STR_LIT>' . format ( security_group_rule , type ( security_group_rule ) ) <EOL> chain = self . get_chain_for_security_group ( group ) <EOL> ip_grants = copy . copy ( security_group_rule . grants ) <EOL> protocol = security_group_rule . ip_protocol and str ( security_group_rule . ip_protocol ) . upper ( ) or None <EOL> from_port = security_group_rule . from_port <EOL> to_port = security_group_rule . to_port <EOL> if from_port is not None : <EOL> from_port = int ( from_port ) <EOL> if to_port is not None : <EOL> to_port = int ( to_port ) <EOL> if from_port == - <NUM_LIT:1> : <EOL> from_port = None <EOL> if to_port == - <NUM_LIT:1> : <EOL> to_port = None <EOL> for grant in security_group_rule . grants : <EOL> self . debug ( self . _bold ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( grant . cidr_ip , grant . group_id , <EOL> security_group_rule . ip_protocol , <EOL> security_group_rule . from_port , <EOL> security_group_rule . to_port ) ) ) <EOL> for rule in chain . get_rules ( ) : <EOL> match = False <EOL> protocol_number = rule . get_nw_proto ( ) <EOL> r_protocol = None <EOL> if protocol_number is not None : <EOL> r_protocol = str ( self . _get_protocol_name_by_number ( protocol_number ) ) . upper ( ) <EOL> self . debug ( '<STR_LIT>' . format ( protocol , <EOL> r_protocol ) ) <EOL> if not ( protocol == r_protocol ) : <EOL> continue <EOL> port_dict = rule . get_tp_dst ( ) or { } <EOL> start_port = port_dict . get ( '<STR_LIT:start>' ) <EOL> end_port = port_dict . get ( '<STR_LIT:end>' ) <EOL> self . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( from_port , start_port , to_port , end_port ) ) <EOL> if not ( from_port == start_port and to_port == end_port ) : <EOL> continue <EOL> self . debug ( '<STR_LIT>' ) <EOL> src_ip = str ( rule . get_nw_src_address ( ) or '<STR_LIT>' ) <EOL> src_mask = str ( rule . get_nw_src_length ( ) or <NUM_LIT:0> ) <EOL> rule_cidr = src_ip + "<STR_LIT:/>" + src_mask <EOL> ip_addr_grp_id = rule . get_ip_addr_group_src ( ) <EOL> ip_addr_grp_name = "<STR_LIT>" <EOL> self . debug ( '<STR_LIT>' . format ( ip_addr_grp_id ) ) <EOL> if ip_addr_grp_id : <EOL> ip_addr_grp = self . mapi . get_ip_addr_group ( ip_addr_grp_id ) <EOL> if ip_addr_grp : <EOL> ip_addr_grp_name = str ( ip_addr_grp . get_name ( ) ) <EOL> self . debug ( '<STR_LIT>' <EOL> . format ( ip_addr_grp_name ) ) <EOL> self . debug ( '<STR_LIT>' <EOL> . format ( grant . cidr_ip , rule_cidr ) ) <EOL> if grant . cidr_ip and ( str ( grant . cidr_ip ) == rule_cidr ) : <EOL> match = True <EOL> elif grant . group_id and str ( grant . group_id ) in ip_addr_grp_name : <EOL> match = True <EOL> if match : <EOL> self . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( grant . cidr_ip , grant . group_id , protocol , <EOL> from_port , to_port ) ) <EOL> if not rule in ret_rules : <EOL> ret_rules . append ( rule ) <EOL> if grant in ip_grants : <EOL> ip_grants . remove ( grant ) <EOL> break <EOL> self . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( len ( ret_rules ) , group , security_group_rule . grants , protocol , <EOL> from_port , to_port ) ) <EOL> if ret_rules : <EOL> self . show_rules ( rules = ret_rules ) <EOL> return ret_rules <EOL> def do_instance_rules_allow ( self , instance , src_addr , protocol , port ) : <EOL> for group in instance . groups : <EOL> chain = self . get_chain_for_security_group ( group ) <EOL> self . debug ( '<STR_LIT>' <EOL> . format ( chain . get_name ( ) , instance . id , group . name ) ) <EOL> if self . does_chain_allow ( chain = chain , src_addr = src_addr , protocol = protocol , port = port ) : <EOL> return True <EOL> return False <EOL> def show_rules ( self , rules , jump = False , printme = True ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( rules , list ) : <EOL> rules = [ rules ] <EOL> buf = "<STR_LIT>" <EOL> pt = None <EOL> for rule in rules : <EOL> if pt is None : <EOL> chain_id = rule . get_chain_id ( ) <EOL> title = "<STR_LIT>" . format ( "<STR_LIT>" . format ( chain_id [ - <NUM_LIT:5> : - <NUM_LIT:1> ] , <EOL> chain_id [ - <NUM_LIT:1> ] ) ) <EOL> title_width = len ( title ) <EOL> pt = PrettyTable ( [ title , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> pt . padding_width = <NUM_LIT:0> <EOL> pt . max_width [ title ] = title_width <EOL> pt . align [ title ] = '<STR_LIT:l>' <EOL> pt . max_width [ '<STR_LIT>' ] = self . _ADDR_SPACING <EOL> pt . align [ '<STR_LIT>' ] = '<STR_LIT:l>' <EOL> pt . max_width [ '<STR_LIT>' ] = self . _ADDR_SPACING <EOL> pt . align [ '<STR_LIT>' ] = '<STR_LIT:l>' <EOL> pt . hrules = <NUM_LIT:1> <EOL> jump_chain = None <EOL> action = rule . dto . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> targets = [ ] <EOL> nattargets = rule . dto . get ( '<STR_LIT>' ) or [ ] <EOL> ports = None <EOL> tpdst = rule . dto . get ( '<STR_LIT>' , None ) <EOL> if tpdst : <EOL> ports = "<STR_LIT>" . format ( tpdst . get ( '<STR_LIT:start>' , "<STR_LIT>" ) , tpdst . get ( '<STR_LIT:end>' , "<STR_LIT>" ) ) <EOL> rule_type = self . _bold ( rule . get_type ( ) ) <EOL> for nattarget in nattargets : <EOL> targets . append ( nattarget . get ( '<STR_LIT>' ) ) <EOL> targetstring = self . _bold ( "<STR_LIT:U+002C>" . join ( targets ) ) <EOL> if rule . get_type ( ) . upper ( ) == '<STR_LIT>' : <EOL> jump_chain_id = rule . get_jump_chain_id ( ) <EOL> jump_chain = self . mapi . get_chain ( jump_chain_id ) <EOL> rule_type = self . _bold ( rule . get_type ( ) , self . _CHAIN_JUMP ) <EOL> action = self . _bold ( '<STR_LIT>' , self . _CHAIN_JUMP ) <EOL> targetstring = jump_chain_id <EOL> ip_addr_group = rule . get_ip_addr_group_src ( ) <EOL> if ip_addr_group : <EOL> ip_addr_group = self . show_ip_addr_group_addrs ( ipgroup = ip_addr_group , printme = False ) <EOL> pt . add_row ( [ '<STR_LIT>' . format ( self . _bold ( "<STR_LIT>" + <EOL> str ( rules . index ( rule ) + <NUM_LIT:1> ) ) . ljust ( title_width ) , <EOL> rule . get_id ( ) ) , <EOL> "<STR_LIT>" . format ( rule . get_nw_src_address ( ) , rule . get_nw_src_length ( ) ) , <EOL> "<STR_LIT>" . format ( rule . get_nw_dst_address ( ) , rule . get_nw_dst_length ( ) ) , <EOL> self . _get_protocol_name_by_number ( rule . get_nw_proto ( ) ) , <EOL> ports , <EOL> rule . get_nw_tos ( ) , <EOL> ip_addr_group , <EOL> rule . get_fragment_policy ( ) , <EOL> rule . get_position ( ) , <EOL> rule_type , <EOL> action , <EOL> targetstring ] ) <EOL> if jump_chain and jump : <EOL> buf += str ( pt ) + "<STR_LIT:\n>" <EOL> pt = None <EOL> buf += self . _link_table_buf ( self . show_chain ( jump_chain , printme = False ) ) <EOL> buf += str ( pt ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( buf ) ) <EOL> else : <EOL> return buf <EOL> def show_router_for_instance ( self , instance , printme = True ) : <EOL> ret_buf = self . _highlight_buf_for_instance ( <EOL> buf = self . show_router_summary ( router = self . get_router_for_instance ( instance = instance ) , <EOL> printme = False ) , <EOL> instance = instance ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( ret_buf ) ) <EOL> return None <EOL> else : <EOL> return ret_buf <EOL> def show_bridge_for_instance ( self , instance , printme = True ) : <EOL> ret_buf = self . _highlight_buf_for_instance ( <EOL> buf = self . show_bridges ( bridges = self . get_bridge_for_instance ( instance = instance ) , <EOL> printme = False ) , <EOL> instance = instance ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( ret_buf ) ) <EOL> return None <EOL> else : <EOL> return ret_buf <EOL> def show_instance_network_summary ( self , instance , printme = True ) : <EOL> instance = self . _get_instance ( instance ) <EOL> self . debug ( '<STR_LIT>' ) <EOL> title = ( "<STR_LIT>" <EOL> . format ( instance . id , instance . private_ip_address , instance . ip_address ) ) <EOL> pt = PrettyTable ( [ title ] ) <EOL> pt . align [ title ] = '<STR_LIT:l>' <EOL> buf = str ( self . show_router_for_instance ( instance = instance , printme = False ) ) <EOL> buf += str ( self . show_bridge_for_instance ( instance = instance , printme = False ) ) <EOL> buf += str ( self . show_bridge_port_for_instance ( instance = instance , printme = False ) ) <EOL> buf += str ( self . show_host_for_instance ( instance = instance , printme = False ) ) <EOL> buf += "<STR_LIT:\n>" <EOL> eucatitle = self . _bold ( '<STR_LIT>' . format ( instance . id ) , <NUM_LIT> ) <EOL> ept = PrettyTable ( [ eucatitle ] ) <EOL> ept . align [ eucatitle ] = '<STR_LIT:l>' <EOL> ebuf = "<STR_LIT:\n>" + str ( self . tester . show_instance ( instance , printme = False ) ) + "<STR_LIT:\n>" <EOL> ept . add_row ( [ ebuf ] ) <EOL> buf += str ( ept ) <EOL> pt . add_row ( [ buf ] ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( pt ) ) <EOL> else : <EOL> return pt <EOL> def show_security_groups_for_instance ( self , instance , printme = True ) : <EOL> buf = "<STR_LIT>" <EOL> instance = self . _get_instance ( instance ) <EOL> return self . tester . show_security_groups_for_instance ( instance = instance , <EOL> printmethod = self . debug , <EOL> printme = printme ) <EOL> def show_ip_addr_group_addrs ( self , ipgroup , printme = True ) : <EOL> if not isinstance ( ipgroup , IpAddrGroup ) : <EOL> ipgroup = self . mapi . get_ip_addr_group ( ipgroup ) <EOL> if not ipgroup : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> addrs = [ ] <EOL> grpaddrs = ipgroup . get_addrs ( ) <EOL> for ga in grpaddrs : <EOL> addr = ga . get_addr ( ) <EOL> if addr : <EOL> addrs . append ( str ( addr ) . ljust ( self . _ADDR_SPACING ) ) <EOL> ret_buf = "<STR_LIT:U+0020>" . join ( addrs ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( ret_buf ) ) <EOL> else : <EOL> return ret_buf <EOL> def show_hosts_summary ( self , hosts = None , printme = True ) : <EOL> if hosts and not isinstance ( hosts , list ) : <EOL> assert isinstance ( hosts , Host ) <EOL> hosts = [ hosts ] <EOL> if hosts is None : <EOL> hosts = self . mapi . get_hosts ( query = None ) <EOL> pt = PrettyTable ( [ "<STR_LIT>" , '<STR_LIT>' , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> for host in hosts : <EOL> ip_addrs = '<STR_LIT>' <EOL> try : <EOL> name , aliaslist , addresslist = socket . gethostbyaddr ( host . get_name ( ) ) <EOL> ip_addrs = "<STR_LIT:U+002CU+0020>" . join ( addresslist ) <EOL> except : pass <EOL> pt . add_row ( [ host . get_id ( ) , host . get_name ( ) , host . dto . get ( '<STR_LIT>' ) , ip_addrs ] ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( pt ) ) <EOL> else : <EOL> return pt <EOL> def show_hosts ( self , hosts = None , printme = True ) : <EOL> if hosts and not isinstance ( hosts , list ) : <EOL> assert isinstance ( hosts , Host ) <EOL> hosts = [ hosts ] <EOL> if hosts is None : <EOL> hosts = self . mapi . get_hosts ( query = None ) <EOL> buf = "<STR_LIT:\n>" <EOL> for host in hosts : <EOL> title = self . _bold ( "<STR_LIT>" . format ( host . get_name ( ) , host . get_id ( ) ) , <NUM_LIT> ) <EOL> pt = PrettyTable ( [ title ] ) <EOL> pt . align [ title ] = "<STR_LIT:l>" <EOL> hostbuf = self . _bold ( "<STR_LIT>" , <NUM_LIT:4> ) <EOL> hostbuf += self . _indent_table_buf ( str ( self . show_hosts_summary ( hosts = host , <EOL> printme = False ) ) ) <EOL> hostbuf += self . _bold ( "<STR_LIT>" , <NUM_LIT:4> ) <EOL> hostbuf += self . _indent_table_buf ( str ( self . show_host_ports ( host , printme = False ) ) ) <EOL> hostbuf += self . _bold ( "<STR_LIT>" , <NUM_LIT:4> ) <EOL> hostbuf += self . _indent_table_buf ( str ( self . show_host_interfaces ( host , printme = False ) ) ) <EOL> pt . add_row ( [ hostbuf ] ) <EOL> buf += "<STR_LIT:\n>" + str ( pt ) + "<STR_LIT:\n>" <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( buf ) ) <EOL> else : <EOL> return buf <EOL> def get_ip_for_host ( self , host ) : <EOL> assert isinstance ( host , Host ) <EOL> name , aliaslist , addresslist = socket . gethostbyaddr ( host . get_name ( ) ) <EOL> if addresslist : <EOL> return addresslist [ <NUM_LIT:0> ] <EOL> return None <EOL> def restart_backend ( self , hosts = None ) : <EOL> """<STR_LIT>""" <EOL> return self . reset_midolman_service_on_hosts ( hosts = hosts ) <EOL> def reset_midolman_service_on_hosts ( self , hosts = None ) : <EOL> if hosts and not isinstance ( hosts , list ) : <EOL> assert isinstance ( hosts , Host ) <EOL> hosts = [ hosts ] <EOL> if hosts is None : <EOL> hosts = self . mapi . get_hosts ( query = None ) or [ ] <EOL> self . debug ( '<STR_LIT>' ) <EOL> self . debug ( '<STR_LIT>' . format ( "<STR_LIT:U+002C>" . join ( str ( x . get_name ( ) ) for x in hosts ) ) ) <EOL> for status in [ '<STR_LIT>' , '<STR_LIT:start>' ] : <EOL> for host in hosts : <EOL> ip = self . get_ip_for_host ( host ) <EOL> username = self . tester . username or '<STR_LIT:root>' <EOL> password = self . tester . password <EOL> keypath = self . tester . keypath <EOL> ssh = SshConnection ( host = ip , username = username , <EOL> password = password , keypath = keypath ) <EOL> self . debug ( "<STR_LIT>" . format ( status , host . get_name ( ) , ip ) ) <EOL> ssh . sys ( '<STR_LIT>' . format ( status ) , code = <NUM_LIT:0> ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> self . debug ( '<STR_LIT>' ) <EOL> def show_host_ports ( self , host , printme = True ) : <EOL> '''<STR_LIT>''' <EOL> assert isinstance ( host , Host ) <EOL> ports = host . get_ports ( ) <EOL> porttable = PrettyTable ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> for port in ports : <EOL> assert isinstance ( port , HostInterfacePort ) <EOL> porttable . add_row ( [ port . get_interface_name ( ) , port . get_port_id ( ) ] ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( porttable ) ) <EOL> else : <EOL> return porttable <EOL> def show_host_interfaces ( self , host , printme = True ) : <EOL> '''<STR_LIT>''' <EOL> assert isinstance ( host , Host ) , '<STR_LIT>' . format ( type ( host ) ) <EOL> interfaces = host . get_interfaces ( ) <EOL> pt = PrettyTable ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for hi in interfaces : <EOL> assert isinstance ( hi , HostInterface ) , "<STR_LIT>" "<STR_LIT>" . format ( type ( hi ) ) <EOL> pt . add_row ( [ hi . get_name ( ) , hi . get_type ( ) , hi . get_mac ( ) , hi . get_status ( ) , hi . get_mtu ( ) , <EOL> hi . get_endpoint ( ) , "<STR_LIT:U+002CU+0020>" . join ( hi . get_addresses ( ) or [ ] ) ] ) <EOL> if printme : <EOL> self . debug ( '<STR_LIT>' . format ( pt ) ) <EOL> else : <EOL> return pt <EOL> def show_host_for_instance ( self , instance , printme = True ) : <EOL> instance = self . _get_instance ( instance ) <EOL> host = self . get_host_for_instance ( instance ) <EOL> return self . show_hosts ( hosts = host , printme = printme ) <EOL> def get_host_for_instance ( self , instance ) : <EOL> instance = self . _get_instance ( instance ) <EOL> node = instance . tags . get ( '<STR_LIT>' , None ) <EOL> if instance . state == '<STR_LIT>' and not node : <EOL> raise ValueError ( '<STR_LIT>' . format ( instance . id ) ) <EOL> host = self . get_host_by_hostname ( node ) <EOL> if not host : <EOL> raise ValueError ( '<STR_LIT>' . format ( instance . id ) ) <EOL> return host <EOL> def get_host_by_hostname ( self , name ) : <EOL> """<STR_LIT>""" <EOL> name , aliaslist , addresslist = socket . gethostbyaddr ( name ) <EOL> self . debug ( '<STR_LIT>' . format ( name ) ) <EOL> for host in self . mapi . get_hosts ( ) : <EOL> if host . get_name ( ) == name : <EOL> return host <EOL> return None <EOL> def get_instance_learned_port_by_ping ( self , instance ) : <EOL> port = self . get_bridge_port_for_instance_learned ( instance ) <EOL> if not port : <EOL> try : <EOL> self . debug ( self . _bold ( "<STR_LIT>" , <NUM_LIT> ) ) <EOL> self . debug ( self . _bold ( "<STR_LIT>" <EOL> . format ( instance . private_ip_address ) , <NUM_LIT> ) ) <EOL> self . ping_instance_private_ip_from_euca_internal ( instance ) <EOL> except WaitForResultException : pass <EOL> port = self . get_bridge_port_for_instance_learned ( instance ) <EOL> return port </s>
