def get_interface(self, interface_id): commands = [ "show interfaces {}".format(interface_id), "show interfaces {} switchport".format(interface_id) ] try: result = self.node.enable(commands, strict=True) except CommandError: raise UnknownInterface(interface_id) interfaces = parse_interfaces(result[0]['result']['interfaces'], result[1]['result']['switchports']) if len(interfaces) > 0: return interfaces[0] raise UnknownInterface(interface_id)
def has_trunk_allowed_set(self, interface_id): for line in self.ssh.do('show running-config interface {}'.format(interface_id)): if 'Invalid input detected' in line: raise UnknownInterface(interface_id) if 'switchport trunk allowed' in line: return True return False
def get_interface_data(self, interface_id): interface_data = self.shell.do( "show running-config interface {}".format(interface_id)) if len(interface_data) > 0 and regex.match(".*invalid interface.*", interface_data[0]): raise UnknownInterface(interface_id) return interface_data
def set_access_vlan(self, interface_id, vlan): self._get_vlan(vlan) with self.config(), self.vlan(vlan): result = self.shell.do("untagged {}".format(interface_id)) if result: raise UnknownInterface(interface_id)
def remove_trunk_vlan(self, interface_id, vlan): config = self.query(all_interfaces, self.custom_strategies.all_vlans) interface_node = self.get_interface_config(interface_id, config) if interface_node is None: raise UnknownInterface(interface_id) interface = self.node_to_interface(interface_node, config) if interface.port_mode is ACCESS: raise InterfaceInWrongPortMode("access") vlan_node = self.custom_strategies.vlan_node(config, vlan) vlan_name = first(vlan_node.xpath("name")).text modifications = self.custom_strategies.craft_members_modification_to_remove_vlan( interface_node, vlan_name, vlan) if len(modifications) == 0: raise TrunkVlanNotSet(interface_id) update = Update() update.add_interface( self.custom_strategies.interface_update( interface_id, "0", vlan_members=modifications)) self._push(update)
def reset_interface(self, interface_id): result = self.shell.do("show vlan {}".format(interface_id)) if result and ('Invalid input' in result[0] or 'Error' in result[0]) : raise UnknownInterface(interface_id) operations = self._get_vlan_association_removal_operations(result) with self.config(): if len(operations) > 0: for operation in operations: self.shell.do("vlan {}".format(operation[0])) self.shell.do("no {} {}".format(operation[1], interface_id)) self.shell.do("exit") result = self.shell.do("no interface {}".format(interface_id)) if result: raise UnknownInterface(interface_id)
def get_interface(self, interface_id): data = self.ssh.do( "show running-config interface {} | begin interface".format( interface_id)) interface = parse_interface(data) if not interface: raise UnknownInterface(interface_id) return interface
def _push_interface_update(self, interface_id, configuration): try: self._push(configuration) except RPCError as e: if "port value outside range" in e.message \ or "invalid interface type" in e.message \ or "device value outside range" in e.message: raise UnknownInterface(interface_id) raise
def get_interface_config(self, interface_id, config=None): config = config or self.query(one_interface(interface_id)) interface_node = first( config.xpath( "data/configuration/interfaces/interface/name[text()=\"{0:s}\"]/.." .format(interface_id))) if interface_node is None: raise UnknownInterface(interface_id) return interface_node
def set_interface_state(self, interface_id, state): update = Update() update.add_interface(interface_state_update(interface_id, state)) try: self._push(update) except RPCError as e: self.logger.info("actual setting error was {}".format(e)) raise UnknownInterface(interface_id)
def set_trunk_mode(self, interface_id): commands = [ "interface {}".format(interface_id), "switchport mode trunk", "switchport trunk allowed vlan none" ] try: self.node.config(commands) except CommandError: raise UnknownInterface(interface_id)
def get_interface_data(self, interface_id): interface_data = self.page_reader.do( self.shell, "show running-config interface {}".format(interface_id)) if any([ "Invalid input" in line or regex.match("ERROR.*", line) for line in interface_data ]): raise UnknownInterface(interface_id) return interface_data
def add_trunk_vlan(self, interface_id, vlan): self.get_vlan(vlan) commands = [ "interface {}".format(interface_id), "switchport trunk allowed vlan add {}".format(vlan) ] try: self.node.config(commands) except CommandError: raise UnknownInterface(interface_id)
def unset_interface_mtu(self, interface_id): update = Update() update.add_interface( interface_main_update(interface_id, [to_ele("<mtu operation=\"delete\" />")])) try: self._push(update) except RPCError as e: if e.severity != "warning": raise UnknownInterface(interface_id)
def unset_interface_access_vlan(self, interface_id): content = self.shell.do("show vlan brief | include {}".format( _to_short_name(interface_id))) if len(content) == 0: raise UnknownInterface(interface_id) self.logger.debug("show vlan result : \n" + "\n".join(content)) matches = re.compile("^(\d+).*").match(content[0]) with self.config(), self.vlan(int(matches.groups()[0])): self.shell.do("no untagged {}".format(interface_id))
def set_interface_description(self, interface_id, description): update = Update() update.add_interface( interface_main_update( interface_id, [to_ele("<description>{}</description>".format(description))])) try: self._push(update) except RPCError as e: self.logger.info("actual setting error was {}".format(e)) raise UnknownInterface(interface_id)
def set_interface_state(self, interface_id, state): update = Update() update.add_interface(interface_state_update(interface_id, state)) try: self._push(update) except RPCError as e: self.logger.info("actual setting error was {}".format(e)) # When sending a "delete operation" on a nonexistent element <disable />, this is the error that is thrown. # It's ignored because the result of this operation would be the same as if the command was successful. if e.message != "statement not found: ": raise UnknownInterface(interface_id)
def set_access_mode(self, interface_id): result = self.shell.do("show vlan {}".format(interface_id)) if result and 'Invalid input' in result[0]: raise UnknownInterface(interface_id) operations = self._get_vlan_association_removal_operations(result) if len(operations) > 0 and not (len(operations) == 1 and operations[0][1] == "untagged"): with self.config(): for operation in operations: self.shell.do("vlan {}".format(operation[0])) self.shell.do("no {} {}".format(operation[1], interface_id)) self.shell.do("exit")
def set_interface_mtu(self, interface_id, size): update = Update() update.add_interface( interface_main_update(interface_id, [to_ele("<mtu>{}</mtu>".format(size))])) try: self._push(update) except RPCError as e: self.logger.info("actual setting error was {}".format(e)) if "Value {} is not within range".format(size) in str(e): raise InvalidMtuSize(str(e)) raise UnknownInterface(interface_id)
def unset_interface_access_vlan(self, interface_id): config = self.query(one_interface(interface_id)) if len(config.xpath("data/configuration/interfaces/interface")) < 1: raise UnknownInterface(interface_id) if len( config.xpath( "data/configuration/interfaces/interface/unit/family/bridge/vlan-id" )) < 1: raise AccessVlanNotSet(interface_id) update = Update() update.add_interface( self.custom_strategies.interface_update( interface_id, "0", [to_ele('<vlan-id operation="delete" />')])) self._push(update)
def get_interface(self, interface_id): vlans = [] if_data = self.shell.do("show interfaces {}".format(interface_id)) interface = parse_interface(if_data) if not interface: raise UnknownInterface(interface=interface_id) for vlan_data in split_on_bang(self.shell.do("show running-config vlan")): vlans.append(parse_vlan_runningconfig(vlan_data)) interface_vlans = get_interface_vlans_association(interface, vlans) set_vlans_properties(interface_vlans) return interface
def set_access_mode(self, interface_id): result = self.shell.do("show vlan {}".format(interface_id)) if result and 'Invalid input' in result[0]: raise UnknownInterface(interface_id) operations = [] for line in result: if regex.match("VLAN: (\d*) ([^\s]*)", line): vlan, state = regex if int(vlan) > 1: operations.append((vlan, state.lower())) if len(operations) > 0 and not (len(operations) == 1 and operations[0][1] == "untagged"): with self.config(): for operation in operations: self.shell.do("vlan {}".format(operation[0])) self.shell.do("no {} {}".format(operation[1], interface_id)) self.shell.do("exit")
def set_trunk_mode(self, interface_id): result = self.shell.do("show vlan {}".format(interface_id)) if result and 'Invalid input' in result[0]: raise UnknownInterface(interface_id)
def _get_physical_interface(self, interface_id): try: return next(i for i in self._list_physical_interfaces() if i.name == interface_id) except StopIteration: raise UnknownInterface(interface_id)
def reset_interface(self, interface_id): with self.config(): for line in self.ssh.do( 'default interface {}'.format(interface_id)): if 'Invalid input detected' in line: raise UnknownInterface(interface_id)