def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^(\S+)', conf) intf = match.group(1) if get_interface_type(intf) == 'unknown': return {} config['name'] = intf # 'bfd'/'bfd echo' do not nvgen when enabled thus set to 'enable' when None. # 'bfd' is not supported on some platforms config['bfd'] = utils.parse_conf_cmd_arg(conf, 'bfd', 'enable', 'disable') or 'enable' config['echo'] = utils.parse_conf_cmd_arg(conf, 'bfd echo', 'enable', 'disable') or 'enable' return utils.remove_empties(config)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^(\S+)', conf) intf = match.group(1) if get_interface_type(intf) == 'unknown': return {} config['name'] = intf config['dot1q'] = utils.parse_conf_arg(conf, 'encapsulation dot1[qQ]') config['redirects'] = utils.parse_conf_cmd_arg(conf, 'no ip redirects', False, True) config['unreachables'] = utils.parse_conf_cmd_arg( conf, 'ip unreachables', True, False) ipv4_match = re.compile(r'\n ip address (.*)') matches = ipv4_match.findall(conf) if matches: if matches[0]: config['ipv4'] = [] for m in matches: ipv4_conf = m.split() addr = ipv4_conf[0] if addr: config_dict = {'address': addr} if len(ipv4_conf) > 1: d = ipv4_conf[1] if d == 'secondary': config_dict.update({'secondary': True}) if len(ipv4_conf) == 4: if ipv4_conf[2] == 'tag': config_dict.update( {'tag': int(ipv4_conf[-1])}) elif d == 'tag': config_dict.update({'tag': int(ipv4_conf[-1])}) config['ipv4'].append(config_dict) ipv6_match = re.compile(r'\n ipv6 address (.*)') matches = ipv6_match.findall(conf) if matches: if matches[0]: config['ipv6'] = [] for m in matches: ipv6_conf = m.split() addr = ipv6_conf[0] if addr: config_dict = {'address': addr} if len(ipv6_conf) > 1: d = ipv6_conf[1] if d == 'tag': config_dict.update({'tag': int(ipv6_conf[-1])}) config['ipv6'].append(config_dict) return utils.remove_empties(config)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^(\S+)', conf) intf = match.group(1) if match.group(1).lower() == "preconfigure": match = re.search(r'^(\S+) (.*)', conf) if match: intf = match.group(2) if get_interface_type(intf) == 'unknown': return {} # populate the facts from the configuration config['name'] = intf config['description'] = utils.parse_conf_arg(conf, 'description') if utils.parse_conf_arg(conf, 'speed'): config['speed'] = int(utils.parse_conf_arg(conf, 'speed')) if utils.parse_conf_arg(conf, 'mtu'): config['mtu'] = int(utils.parse_conf_arg(conf, 'mtu')) config['duplex'] = utils.parse_conf_arg(conf, 'duplex') enabled = utils.parse_conf_cmd_arg(conf, 'shutdown', False) config['enabled'] = enabled if enabled is not None else True return utils.remove_empties(config)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^(\S+)', conf) intf = match.group(1) if match.group(1).lower() == "preconfigure": match = re.search(r'^(\S+) (.*)', conf) if match: intf = match.group(2) if get_interface_type(intf) == 'unknown': return {} if intf.lower().startswith('gi'): config['name'] = intf # populate the facts from the configuration native_vlan = re.search(r"dot1q native vlan (\d+)", conf) if native_vlan: config["native_vlan"] = int(native_vlan.group(1)) dot1q = utils.parse_conf_arg(conf, 'encapsulation dot1q') config['q_vlan'] = [] if dot1q: config['q_vlan'].append(int(dot1q.split(' ')[0])) if len(dot1q.split(' ')) > 1: config['q_vlan'].append(int(dot1q.split(' ')[2])) if utils.parse_conf_cmd_arg(conf, 'l2transport', True): config['l2transport'] = True if utils.parse_conf_arg(conf, 'propagate'): config['propagate'] = True config['l2protocol'] = [] cdp = utils.parse_conf_arg(conf, 'l2protocol cdp') pvst = utils.parse_conf_arg(conf, 'l2protocol pvst') stp = utils.parse_conf_arg(conf, 'l2protocol stp') vtp = utils.parse_conf_arg(conf, 'l2protocol vtp') if cdp: config['l2protocol'].append({'cdp': cdp}) if pvst: config['l2protocol'].append({'pvst': pvst}) if stp: config['l2protocol'].append({'stp': stp}) if vtp: config['l2protocol'].append({'vtp': vtp}) return utils.remove_empties(config)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^(\S+)', conf) intf = match.group(1) if get_interface_type(intf) == 'unknown': return {} config['name'] = intf config['bfd'] = utils.parse_conf_cmd_arg(conf, 'hsrp bfd', 'enable', 'disable') return utils.remove_empties(config)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) # populate the facts from the configuration config['name'] = re.match(r'(\S+)', conf).group(1) description = utils.parse_conf_arg(conf, 'description') if description is not None: config['description'] = description.replace('"', '') shutdown = utils.parse_conf_cmd_arg(conf, 'shutdown', False) config['enabled'] = shutdown if shutdown is False else True config['mtu'] = utils.parse_conf_arg(conf, 'mtu') speed_pair = utils.parse_conf_arg(conf, 'speed') if speed_pair: state = speed_pair.split() if state[0] == 'forced': state = state[1] else: state = state[0] if state == 'auto': config['duplex'] = state else: # remaining options are all e.g., 10half or 40gfull config['speed'] = state[:-4] config['duplex'] = state[-4:] return utils.remove_empties(config)