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) enabled = True if 'GigabitEthernet' in conf or 'GE' in conf: match = re.search(r'^(\S+)', conf) intf = '' if match: intf = match.group(1) match = re.search(r'(undo lldp enable)', conf) if match: enabled = False if get_interface_type(intf) == 'unknown': return {} config['name'] = normalize_interface(intf) if enabled: config['enabled'] = True elif not enabled: config['enabled'] = False return utils.remove_empties(config)
def _clear_config(self, want, have): # Delete the interface config based on the want and have config commands = [] change_flag = False if want.get('name'): interface_type = get_interface_type(want['name']) interface = 'interface ' + want['name'] else: interface_type = get_interface_type(have['name']) interface = 'interface ' + have['name'] if have.get('description' ) and want.get('description') != have.get('description'): remove_command_from_config_list(interface, 'description', commands) change_flag = True if not have.get( 'enabled') and want.get('enabled') != have.get('enabled'): # if enable is False set enable as True which is the default behavior remove_command_from_config_list(interface, 'shutdown', commands) change_flag = True if have.get('mtu') and want.get('mtu') != have.get('mtu'): remove_command_from_config_list(interface, 'jumboframe enable', commands) change_flag = True if interface_type.lower() == 'gigabitethernet': if have.get('speed') and want.get('speed') != have.get( 'speed') and not have.get('negotiation'): remove_command_from_config_list(interface, 'speed', commands) change_flag = True if have.get('duplex') and want.get('duplex') != have.get( 'duplex') and not have.get('negotiation'): remove_command_from_config_list(interface, 'duplex', commands) change_flag = True if not have.get('negotiation') and want.get( 'negotiation') != have.get('negotiation'): remove_command_from_config_list(interface, 'speed', commands) remove_command_from_config_list(interface, 'duplex', commands) add_command_to_config_list(interface, 'negotiation auto', commands) change_flag = True if change_flag: add_command_to_config_list(interface, 'quit', commands) return commands
def _set_config(self, want, have): # Set the interface config based on the want and have config commands = [] interface = 'interface ' + want['name'] interface_type = get_interface_type(want['name']) # Get the diff b/w want and have want_dict = dict_to_set(want) have_dict = dict_to_set(have) diff = want_dict - have_dict if diff: diff = dict(diff) if diff.get('description'): cmd = 'description {0}'.format(want.get('description')) add_command_to_config_list(interface, cmd, commands) if diff.get('mtu'): cmd = 'jumboframe enable {0}'.format(want.get('mtu')) add_command_to_config_list(interface, cmd, commands) if diff.get('enabled'): add_command_to_config_list(interface, 'undo shutdown', commands) elif diff.get('enabled') is False: add_command_to_config_list(interface, 'shutdown', commands) if diff.get('negotiation'): add_command_to_config_list(interface, 'negotiation auto', commands) if interface_type.lower( ) == 'gigabitethernet' and not diff.get('negotiation'): for item in diff.keys(): if item in ['speed', 'duplex']: cmd = 'undo negotiation auto' add_command_to_config_list(interface, cmd, commands) break if diff.get('speed') == '1000' and have.get( 'duplex') == 'half': cmd = 'duplex full' add_command_to_config_list(interface, cmd, commands) cmd = 'speed {0}'.format(want.get('speed')) add_command_to_config_list(interface, cmd, commands) elif diff.get('speed'): cmd = 'speed {0}'.format(want.get('speed')) add_command_to_config_list(interface, cmd, commands) if diff.get('duplex'): cmd = 'duplex {0}'.format(want.get('duplex')) add_command_to_config_list(interface, cmd, commands) add_command_to_config_list(interface, 'quit', commands) return commands
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 {} # populate the facts from the configuration config['name'] = normalize_interface(intf) ipv4 = [] ipv4_all = re.findall(r"ip [Aa]ddress (\S+.*)", conf) for each in ipv4_all: each_ipv4 = dict() if 'sub' not in each and 'dhcp' not in each: each_ipv4['address'] = each elif 'sub' in each: each_ipv4['address'] = each.split(' sub')[0] each_ipv4['secondary'] = True elif 'dhcp-alloc' in each: each_ipv4['address'] = 'dhcp' ipv4.append(each_ipv4) config['ipv4'] = ipv4 # Get the configured IPV6 details ipv6 = [] ipv6_all = re.findall(r"ipv6 address (\S+)", conf) for each in ipv6_all: each_ipv6 = dict() if 'auto' in each: each_ipv6['autoconfig'] = True if 'dhcp' in each: each_ipv6['dhcp'] = True each_ipv6['address'] = each.lower() ipv6.append(each_ipv6) config['ipv6'] = ipv6 if not config['ipv4'] and not config['ipv6']: return {} 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) intf = conf.split()[0] if get_interface_type(intf) == 'unknown': return {} # populate the facts from the configuration config['name'] = normalize_interface(intf) if conf.split()[1] == 'access': access = dict() access['vlan'] = int(conf.split()[2]) config['access'] = access if conf.split()[1] == 'trunk': trunk = dict() trunk["native_vlan"] = int(conf.split()[2]) trunk["allowed_vlans"] = self.parse_vlan_to_list(conf.split()[3:]) config['trunk'] = trunk if conf.split()[1] == 'hybrid': hybrid = dict() hybrid["native_vlan"] = int(conf.split()[2]) hybrid["allowed_vlans"] = self.parse_vlan_to_list(conf.split()[3:]) config['hybrid'] = hybrid if conf.split()[1] == 'auto': auto = dict() auto["native_vlan"] = int(conf.split()[2]) auto["allowed_vlans"] = self.parse_vlan_to_list(conf.split()[3:]) config['auto'] = auto if conf.split()[1] == 'desirable': desirable = dict() desirable["native_vlan"] = int(conf.split()[2]) desirable["allowed_vlans"] = self.parse_vlan_to_list( conf.split()[3:]) config['desirable'] = desirable 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) intf = '' match = re.search(r'^([Ee]th-[Tt]runk\d+)', conf) if match: intf = match.group(1) if get_interface_type(intf) == 'unknown': return {} member_config = {} config['name'] = intf config['members'] = [] match = re.search(r'WorkingMode:\s*(\S+)', conf) if match: mode = match.group(1) if mode.lower() == 'normal': member_config.update({'mode': 'on'}) if mode.lower() == 'lacp': member_config.update({'mode': 'active'}) match = re.findall( r'\n(\S+)\s+(?:[Uu]p|[Dd]own|[Ss]elected|[Uu]nselect|[Ii]ndepent)', conf) if match: for i in match: memb_config = {} member_config['member'] = i memb_config.update(member_config) config['members'].append(memb_config) 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) port_priority = '' max_bundle = '' intf = '' match = re.search(r'^([Ee]th-[Tt]runk\d+)', conf) if match: intf = match.group(1) if get_interface_type(intf) == 'unknown': return {} config['name'] = normalize_interface(intf) match = re.search(r'System Priority:\s+(\d+)',conf) if match: port_priority = match.group(1) match = re.search(r'Max Active-linknumber:\s+(\d+)', conf) if match: max_bundle = match.group(1) if port_priority: config['port_priority'] = int(port_priority) if max_bundle: config['max_bundle'] = int(max_bundle) 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) if match: intf = match.group(1) if get_interface_type(intf) == 'Vlanif': return {} if get_interface_type(intf) == 'Eth-Trunk': return {} if get_interface_type(intf) == 'LoopBack': return {} if get_interface_type(intf) == 'Nve': return {} if get_interface_type(intf) == 'unknown': return {} else: return{} # populate the facts from the configuration config['name'] = normalize_interface(intf) match = re.search(r'Description\s*:\s*(.*)\nS', conf) if match: config['description'] = match.group(1) #config['description'] = utils.parse_conf_arg(conf, 'description') match = re.search(r'Speed\s*:\s*(\d+)', conf) if match: config['speed'] = match.group(1) #config['speed'] = utils.parse_conf_arg(conf, 'speed') match = re.search(r'The\s+Maximum\s+Frame\s+Length\s+is\s+(\d+)', conf) if match: config['mtu'] = match.group(1) #if utils.parse_conf_arg(conf, 'jumboframe enable'): # config['mtu'] = int(utils.parse_conf_arg(conf, 'jumboframe enable')) match = re.search(r'Duplex\s*:\s*(FULL|HALF)', conf) if match: config['duplex'] = match.group(1).lower() #config['duplex'] = utils.parse_conf_arg(conf, 'duplex') match = re.search(r'Negotiation\s*:\s*(DISABLE|ENABLE)', conf) if match: if match.group(1).lower() == 'enable': config['negotiation'] = True else: config['negotiation'] = False match = re.search(r'\S+\s+current\s+state\s*:\s*(DOWN|UP|Administratively DOWN)', conf) if match: if match.group(1) == 'Administratively DOWN': config['enabled'] = False else: config['enabled'] = True #enabled = utils.parse_conf_cmd_arg(conf, 'shutdown', False) #config['enabled'] = enabled if enabled is not None else True return utils.remove_empties(config)