def test_init__multiline_mixed_different_indent(self): LINE = "cfg_line" cfg = " " + LINE + "\n" + " \n" + " " + LINE parser = HTParser(cfg) parser.find_objects("fake_object") res = [(2, ' cfg_line'), (3, ' cfg_line')] self.assertEqual(res, parser._indent_list)
def test_build_indent_based_list__multiline_indent(self): LINE = ' cfg_line' cfg = LINE + '\n' + LINE parser = HTParser(cfg) parser.find_objects("fake_object") res = [(2, LINE), (2, LINE)] self.assertEqual(res, parser._indent_list)
def _get_interfaces_objs(self): """Get a list of interfaces on this hosting device. :return: List of the interfaces' objects """ ios_cfg = self._get_running_config() parse = HTParser(ios_cfg) itfcs_raw = parse.find_children("^interface \w+\d+") LOG.debug("Interfaces on hosting device: %s", itfcs_raw) return itfcs_raw
def _get_interface_obj_children(self, intf): """Get a interface's children on this hosting device. :return: List of the interfaces' objects """ ios_cfg = self._get_running_config() parse = HTParser(ios_cfg) itfcs_raw = parse.find_children("^interface " + intf) LOG.debug("Interface children on hosting device: %s", itfcs_raw) return itfcs_raw
def test_re_search_children_interface(self): linespec = "^interf" child_linespec = "\s*no ip*" exp = ['interface Serial1/0', ' no ip address'] parse = HTParser(self.cfg) act = [ obj for obj in parse.find_objects(linespec) if obj.re_search_children(child_linespec) ] self.assertEqual(1, len(act)) self.assertEqual(exp, act[0].str_list())
def _cfg_exists(self, cfg_str): """Check a partial config string exists in the running config. :param cfg_str: config string to check :return : True or False """ ios_cfg = self._get_running_config() parse = HTParser(ios_cfg) cfg_raw = parse.find_lines("^" + cfg_str) LOG.debug("_cfg_exists(): Found lines %s", cfg_raw) return len(cfg_raw) > 0
def _get_interfaces(self): """Get a list of interfaces on this hosting device. :return: List of the interfaces """ ios_cfg = self._get_running_config() parse = HTParser(ios_cfg) itfcs_raw = parse.find_lines("^interface GigabitEthernet") itfcs = [raw_if.strip().split(' ')[1] for raw_if in itfcs_raw] LOG.debug("Interfaces on hosting device: %s", itfcs) return itfcs
def test_re_match_nat(self): linespec = "^interf" child_linespec = "\s*ip nat inside" match_regex = "^interface (\S+)" exp = 'Port-channel11.2033' parse = HTParser(self.cfg) act = [ obj for obj in parse.find_objects(linespec) if obj.re_search_children(child_linespec) ] self.assertEqual(1, len(act)) self.assertEqual(exp, act[0].re_match(match_regex))
def test_re_search_children_acl(self): linespec = "^ip access" child_linespec = ACL_CHILD_REGEX exp = [ 'ip access-list standard neutron_acl_2033_5c409ae1', ' permit 10.10.0.0 0.0.255.255', ' dummy-input2' ] parse = HTParser(self.cfg) act = [ obj for obj in parse.find_objects(linespec) if obj.re_search_children(child_linespec) ] self.assertEqual(1, len(act)) self.assertEqual(exp, act[0].str_list())
def _get_vrfs(self): """Get the current VRFs configured in the device. :return: A list of vrf names as string """ vrfs = [] ios_cfg = self._get_running_config() parse = HTParser(ios_cfg) vrfs_raw = parse.find_lines("^vrf definition") for line in vrfs_raw: # raw format ['ip vrf <vrf-name>',....] vrf_name = line.strip().split(' ')[2] vrfs.append(vrf_name) LOG.info("VRFs:%s", vrfs) return vrfs
def _get_interface_ip(self, interface_name): """Get the ip address for an interface. :param interface_name: interface_name as a string :return: ip address of interface as a string """ ios_cfg = self._get_running_config() parse = HTParser(ios_cfg) children = parse.find_children("^interface %s" % interface_name) for line in children: if 'ip address' in line: ip_address = line.strip().split(' ')[2] LOG.debug("IP Address:%s", ip_address) return ip_address LOG.warning("Cannot find interface: %s", interface_name) return None
def _check_acl(self, acl_no, network, netmask): """Check a ACL config exists in the running config. :param acl_no: access control list (ACL) number :param network: network which this ACL permits :param netmask: netmask of the network :return: """ exp_cfg_lines = ['ip access-list standard ' + str(acl_no), ' permit ' + str(network) + ' ' + str(netmask)] ios_cfg = self._get_running_config() parse = HTParser(ios_cfg) acls_raw = parse.find_children(exp_cfg_lines[0]) if acls_raw: if exp_cfg_lines[1] in acls_raw: return True LOG.error("Mismatch in ACL configuration for %s", acl_no) return False LOG.debug("%s is not present in config", acl_no) return False
def process_routers_data(self, routers): hd_id = self.hosting_device_info['id'] segment_nat_dict = {} #conn = self.driver._get_connection() #TODO(init ncclient properly) running_cfg = self.get_running_config(self.conn) parsed_cfg = HTParser(running_cfg) self.populate_segment_nat_dict(segment_nat_dict, routers) missing_cfg = [] for router in routers: if 'hosting_device' not in router: continue if router['hosting_device']['id'] != hd_id: continue missing_cfg += self.check_router(router, parsed_cfg, segment_nat_dict) return missing_cfg
def _get_static_route_cfg(self): ios_cfg = self._get_running_config() parse = HTParser(ios_cfg) return parse.find_lines('ip route')
def _get_floating_ip_cfg(self): ios_cfg = self._get_running_config() parse = HTParser(ios_cfg) res = parse.find_lines('ip nat inside source static') return res
def _get_interface_cfg(self, interface): ios_cfg = self._get_running_config() parse = HTParser(ios_cfg) return parse.find_children('interface ' + interface)
def _interface_exists(self, interface): """Check whether interface exists.""" ios_cfg = self._get_running_config() parse = HTParser(ios_cfg) itfcs_raw = parse.find_lines("^interface " + interface) return len(itfcs_raw) > 0
def _get_config_by_regionid(self, regionid): """Get all config lines with the regionid.""" ios_cfg = self._get_running_config() parse = HTParser(ios_cfg) regid_raw = parse.find_lines("^.*" + regionid) return regid_raw
def _execute(self, function_name, linespec): return getattr(HTParser(self.cfg), function_name)(linespec)
def test_build_indent_based_list__singleline_comment(self): LINE = '!' cfg = LINE parser = HTParser(cfg) parser.find_objects("fake_object") self.assertEqual([], parser._indent_list)
def test_init__multiline_blank(self): LINE = "" cfg = LINE + "\n" + LINE + "\n" + LINE parser = HTParser(cfg) parser.find_objects("fake_object") self.assertEqual([], parser._indent_list)