def parse_interface(data): if data and (regex.match("interface (\w*Ethernet[^\s]*)", data[0]) or regex.match("interface (Port-channel[^\s]*)", data[0])): i = Interface(name=regex[0], shutdown=False) port_mode = access_vlan = native_vlan = trunk_vlans = None for line in data: if regex.match(" switchport mode (.*)", line): port_mode = regex[0] if regex.match(" switchport access vlan (\d*)", line): access_vlan = int(regex[0]) if regex.match(" switchport trunk native vlan (\d*)", line): native_vlan = int(regex[0]) if regex.match(" switchport trunk allowed vlan (.*)", line): trunk_vlans = regex[0] if regex.match(" shutdown", line): i.shutdown = True if not port_mode: i.port_mode = DYNAMIC i.access_vlan = access_vlan i.trunk_native_vlan = native_vlan i.trunk_vlans = parse_vlan_ranges(trunk_vlans) if trunk_vlans else [] elif port_mode == 'access': i.port_mode = ACCESS i.access_vlan = access_vlan elif port_mode == 'trunk': i.port_mode = TRUNK i.trunk_native_vlan = native_vlan i.trunk_vlans = parse_vlan_ranges(trunk_vlans) if trunk_vlans else [] return i return None
def parse_interface(data): if data and (regex.match("interface (\w*Ethernet[^\s]*)", data[0]) or regex.match("interface (Port-channel[^\s]*)", data[0])): i = Interface(name=regex[0], shutdown=False) port_mode = access_vlan = native_vlan = trunk_vlans = None for line in data: if regex.match(" switchport mode (.*)", line): port_mode = regex[0] if regex.match(" switchport access vlan (\d*)", line): access_vlan = int(regex[0]) if regex.match(" switchport trunk native vlan (\d*)", line): native_vlan = int(regex[0]) if regex.match(" switchport trunk allowed vlan (.*)", line): trunk_vlans = regex[0] if regex.match(" shutdown", line): i.shutdown = True if not port_mode: i.port_mode = DYNAMIC i.access_vlan = access_vlan i.trunk_native_vlan = native_vlan i.trunk_vlans = parse_vlan_ranges( trunk_vlans) if trunk_vlans else [] elif port_mode == 'access': i.port_mode = ACCESS i.access_vlan = access_vlan elif port_mode == 'trunk': i.port_mode = TRUNK i.trunk_native_vlan = native_vlan i.trunk_vlans = parse_vlan_ranges( trunk_vlans) if trunk_vlans else [] return i return None
def node_to_interface(self, interface_node, config): interface = Interface() interface.bond_master = get_bond_master(interface_node) interface.port_mode = self.get_port_mode(interface_node) or ACCESS vlans = list_vlan_members(interface_node, config) if interface.port_mode is ACCESS: interface.access_vlan = first(vlans) else: interface.trunk_vlans = vlans interface.trunk_native_vlan = value_of(interface_node.xpath("unit/family/ethernet-switching/native-vlan-id"), transformer=int) interface.name = value_of(interface_node.xpath("name")) interface.shutdown = first(interface_node.xpath("disable")) is not None return interface
def read_interface(self, interface_name): data = self.get_interface_data(interface_name) interface = Interface(name=interface_name, port_mode=ACCESS, shutdown=False) for line in data: if regex.match("switchport mode \S+", line): interface.port_mode = TRUNK if regex.match("shutdown", line): interface.shutdown = True if regex.match("switchport access vlan (\d+)", line): interface.access_vlan = int(regex[0]) if regex.match("switchport general pvid (\d+)", line): interface.trunk_native_vlan = int(regex[0]) if regex.match("switchport \S* allowed vlan (add )?(\S+)", line): interface.trunk_vlans = parse_vlan_ranges(regex[1]) return interface