Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
introspection
#!/usr/bin/env python3
import inspect
import pprint
import re
from inspect import Parameter
from dp import DP
from vlan import VLAN
from port import Port
from valve import valve_factory
from valve_acl import build_acl_entry
from valve_of import build_match_dict
pp = pprint.PrettyPrinter(indent=4)
pp.pprint ("Top level items: ")
pp.pprint ("['version', 'include', 'include-optional', 'acls', 'dps', 'routers', 'vlans']")
print("\nSupported Hardware config options:")
pattern_sup_hw = re.compile("SUPPORTED_HARDWARE\s*=\s*{(.*?)}",re.M|re.DOTALL)
hw_code = inspect.getsourcelines(valve_factory)
hw_list = list(hw_code[0])
hw_data = ''.join(hw_list)
supported_hardware = pattern_sup_hw.findall(hw_data)
for item in supported_hardware:
item=item.replace("\n","") # remove new line
item=item.replace(" ","") # remove extra white space
## pattern = [num of word chars followed by any number of (single hypen and word chars)] OR [word chars]
m = re.findall(r"'(\w+(?:-\w+)|\w+)'",item)
print (m)
dp_members = dict(inspect.getmembers(DP))
vlan_members = dict(inspect.getmembers(VLAN))
port_members = dict(inspect.getmembers(Port))
valve_acl_members = dict(inspect.getmembers(build_acl_entry))
print("\nACL config attributes related to VLAN options:")
valve_acl_code = inspect.getsourcelines(build_acl_entry)
pattern_valve_acl_attrib = re.compile("attrib\s*==\s*(.*?):",re.M|re.DOTALL)
pattern_valve_acl_attrib_value = re.compile("output_dict\[\'(.*?)\'\]",re.M|re.DOTALL)
valve_acl_attrib_list = list(valve_acl_code[0])
valve_acl_attrib_data = ''.join(valve_acl_attrib_list)
supported_acl_attrib = pattern_valve_acl_attrib.findall(valve_acl_attrib_data)
supported_acl_output_dict = pattern_valve_acl_attrib_value.findall(valve_acl_attrib_data)
for item in supported_acl_attrib:
item=item.replace("\n","") # remove new line
item=item.replace(" ","") # remove extra white space
## pattern = [num of word chars followed by any number of (single hypen and word chars)] OR [word chars]
m = re.findall(r"'(\w+(?:-\w+)|\w+)'",item)
print (m)
print("\nACL config related to VLAN output action options:")
#pp.pprint(valve_acl_members)
for item in supported_acl_output_dict:
item=item.replace("\n","") # remove new line
item=item.replace(" ","") # remove extra white space
print (item)
print("\nVLAN config options:")
pp.pprint(vlan_members['defaults'])
print("\nDP config options:")
pp.pprint(dp_members['defaults'])
print("\nPort config options:")
pp.pprint(port_members['defaults'])
print("\nValve in_match config options:")
valve_in_match_members = inspect.signature(build_match_dict)
pp.pprint(valve_in_match_members)
---------------
Output:
$ python introspection.py
'Top level items: '
"['version', 'include', 'include-optional', 'acls', 'dps', 'routers', 'vlans']"
Supported Hardware config options:
['Allied-Telesis', 'Aruba', 'Netronome', 'NoviFlow', 'OpenvSwitch', 'ZodiacFX', 'Lagopus']
ACL config attributes related to VLAN options:
['in_port']
['actions']
ACL config related to VLAN output action options:
dl_dst
pop_vlans
vlan_vid
vlan_vids
port
VLAN config options:
{ 'acl_in': None,
'bgp_as': 0,
'bgp_local_address': None,
'bgp_neighbor_addresses': [],
'bgp_neighbor_as': None,
'bgp_neighbour_addresses': [],
'bgp_neighbour_as': 0,
'bgp_port': 9179,
'bgp_routerid': '',
'description': None,
'faucet_vips': None,
'max_hosts': None,
'name': None,
'routes': None,
'unicast_flood': True}
DP config options:
{ 'arp_neighbor_timeout': 500,
'cookie': 1524372928,
'description': None,
'dp_id': None,
'drop_bpdu': True,
'drop_broadcast_source_address': True,
'drop_lldp': True,
'drop_spoofed_faucet_mac': True,
'eth_dst_table': None,
'eth_src_table': None,
'flood_table': None,
'group_table': False,
'hardware': 'Open vSwitch',
'high_priority': None,
'highest_priority': None,
'ignore_learn_ins': 3,
'ipv4_fib_table': None,
'ipv6_fib_table': None,
'low_priority': None,
'lowest_priority': None,
'max_host_fib_retry_count': 10,
'max_hosts_per_resolve_cycle': 5,
'max_resolve_backoff_time': 32,
'name': None,
'ofchannel_log': None,
'packetin_pps': 0,
'port_acl_table': None,
'priority_offset': 0,
'stack': None,
'table_offset': 0,
'timeout': 300,
'vlan_acl_table': None,
'vlan_table': None}
Port config options:
{ 'acl_in': None,
'description': None,
'enabled': True,
'mirror': None,
'mirror_destination': False,
'name': None,
'native_vlan': None,
'number': None,
'permanent_learn': False,
'stack': None,
'tagged_vlans': None,
'unicast_flood': True}
Valve in_match config options:
<Signature (in_port=None, vlan=None, eth_type=None, eth_src=None, eth_dst=None, eth_dst_mask=None, ipv6_nd_target=None, icmpv6_type=None, nw_proto=None, nw_src=None, nw_dst=None)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment