Exemple #1
0
class GetBGPParameters(BaseOperation):
    REGEX = [r'asn', r'router id']

    def __init__(self, utterance):
        super(GetBGPParameters, self).__init__(utterance)
        self.bgp_config = []

    def run(self, *args, **kwargs):
        if not self.attributes:
            return failed('No BGP parameters given')

        try:
            for device in self.devices:
                bgp_config = tasks.get_bgp_config(device)
                self.bgp_config.append(bgp_config)
        except Exception, e:
            return failed(str(e))

        output = ''
        for index, bgp_config in enumerate(self.bgp_config):
            output += 'hostname: {} '.format(self.devices[index].upper())
            output += '\n'
            output += ' '.join(
                '{}: {}'.format(attribute, bgp_config[attribute])
                for attribute in self.attributes)
            output += '\n'

        return passed(output)
Exemple #2
0
    def shift_back(self, check_traffic_level=False):
        commands = self._get_commands()
        tasks.apply_config(self.devices[0], commands)

        if not check_traffic_level:
            return passed('Traffic returned to device {}', self.devices[0]) 

        count = 1
        while count <= 3:
            time.sleep(5)
            pps = tasks.get_device_traffic(self.devices[0])
            if pps < THRESHOLD:
                count += 1
            else:
                return passed('Traffic restored to device {} - currently {} packets/sec.', self.devices[0], pps) 
        return failed('Traffic not restored to device {} - currently {} packets/sec.', self.devices[0], pps)
Exemple #3
0
class GetBGPNeighbor(BaseOperation):
    REGEX = [
        r'advertised', r'received', r'state', r'remote as(n)?', r'description',
        r'holdtime', r'keepalive', r'neighbor'
    ]

    def __init__(self, utterance):
        super(GetBGPNeighbor, self).__init__(utterance)
        self.neighbors = re.findall(IPADDRESS_RE, self.utterance)
        self.bgp_data = []

    def run(self, *args, **kwargs):
        if not self.attributes:
            return failed('No BGP parameters given')

        try:
            for device in self.devices:
                bgp_neighbors = tasks.get_bgp_neighbors_state(
                    device, self.neighbors)
                self.bgp_data.append(bgp_neighbors)
        except Exception, e:
            return failed(str(e))

        output = ''
        for index, bgp_data in enumerate(self.bgp_data):
            output += 'hostname: {} '.format(self.devices[index].upper())
            output += '\n'
            for item in bgp_data:
                output += ' '.join('{}: {}'.format(attribute, item[attribute])
                                   for attribute in self.attributes)
                output += '\n'

        return passed(output)
Exemple #4
0
class GetFacts(BaseOperation):
    REGEX = [
        r'hostname', r'uptime', r'image', r'version', r'model',
        r'serial number', r'chassis'
    ]

    def __init__(self, utterance):
        super(GetFacts, self).__init__(utterance)
        self.facts = []

    def run(self, *args, **kwargs):
        try:
            for device in self.devices:
                device_facts = tasks.get_facts(device)
                self.facts.append(device_facts)
        except Exception, e:
            return failed(str(e))

        output = ''
        for fact in self.facts:
            output += 'hostname: {} '.format(fact['hostname'])
            output += '\n'
            if self.attributes:
                output += ' '.join('{}: {}'.format(attribute, fact[attribute])
                                   for attribute in self.attributes)
            else:
                output = ' '.join('{}: {} '.format(item, value)
                                  for item, value in fact.iteritems())
            output += '\n'
        return passed(indent(output))
Exemple #5
0
    def run(self, *args, **kwargs):
        if len(self.devices) != 1:
            return failed('You entered {} devices, required 1.',
                          len(self.devices))

        output = ''
        output += 'Hostname: {}'.format(self.devices[0])
        output += '\n'
        for destination in self.destinations:
            response = tasks.check_connectivity(self.devices[0], destination)
            output += 'Destination: {} '.format(destination)
            output += response
            output += '\n'

        return passed(output)
Exemple #6
0
class InterfaceShutdown(BaseOperation):
    REGEX = [INTERFACE_RE]

    def __init__(self, utterance):
        super(InterfaceShutdown, self).__init__(utterance)
        self.interfaces = re.findall('|'.join(InterfaceShutdown.REGEX),
                                     self.utterance)

    def run(self, *args, **kwargs):
        if not len(self.devices) == 2:
            return failed('Incorrect number of devices entered: {}',
                          len(self.devices))

        intent = re.search(r'enable|up', self.utterance)
        return self.link_status(intent)

    @staticmethod
    def _get_neighbors(device, interfaces):
        return tasks.get_interface_neighbors(device, interfaces)

    @staticmethod
    def _find_neighbor_match(neighbor_device, x):
        if x.neighbor.lower() == neighbor_device:
            return x.local_interface

    def link_status(self, intent):
        if intent:
            return self._link_enable()
        else:
            return self._link_disable()

    @staticmethod
    def _prepare_commands(intent, intf):
        return [
            'interface {}'.format(intf),
            '{}shutdown'.format('' if intent else 'no ')
        ]

    def _link_disable(self):
        if len(self.interfaces) > 1:
            return failed('Incorrect number of interfaces entered: {}',
                          len(self.interfaces))

        a_device_neighbor = filter(
            partial(self._find_neighbor_match, self.devices[1]),
            self._get_neighbors(self.devices[0], self.interfaces))
        b_device_neighbor = filter(
            partial(self._find_neighbor_match, self.devices[0]),
            self._get_neighbors(self.devices[1], self.interfaces))

        if not a_device_neighbor or not b_device_neighbor:
            return failed('Devices {} are not neighbors on any interfaces',
                          ', '.join(self.devices))

        if len(a_device_neighbor) > 1 or len(b_device_neighbor) > 1:
            return failed(
                'Devices {} are neighbors on more than one interface',
                ', '.join(self.devices))

        a_end_interface = a_device_neighbor[0].local_interface
        b_end_interface = b_device_neighbor[0].local_interface
        a_commands = self._prepare_commands(True, a_end_interface)
        b_commands = self._prepare_commands(True, b_end_interface)

        try:
            tasks.apply_config(self.devices[0], a_commands)
            tasks.apply_config(self.devices[1], b_commands)
        except Exception, e:
            return failed(str(e))

        if any([
                self._get_neighbors(self.devices[0], [a_end_interface]),
                self._get_neighbors(self.devices[1], [b_end_interface])
        ]):
            return failed('Failed to disable interfaces {}, {} on {}',
                          a_end_interface, b_end_interface,
                          ', '.join(self.devices))

        return passed(
            'Interface {} on {} and interface {} on {} were successfully disabled',
            a_end_interface, self.devices[0], b_end_interface, self.devices[1])
Exemple #7
0
    def _link_enable(self):
        if not len(self.interfaces) == 2:
            return failed('Incorrect number of interfaces entered: {}',
                          len(self.interfaces))

        a_end_interface, b_end_interface = self.interfaces[0], self.interfaces[
            1]

        a_commands = self._prepare_commands(False, a_end_interface)
        b_commands = self._prepare_commands(False, b_end_interface)

        try:
            tasks.apply_config(self.devices[0], a_commands)
            tasks.apply_config(self.devices[1], b_commands)
        except Exception, e:
            return failed(str(e))

        if all([
                self._get_neighbors(self.devices[0], [a_end_interface]),
                self._get_neighbors(self.devices[1], [b_end_interface])
        ]):
            return passed(
                'Interface {} on {} and interface {} on {} were successfully enabled',
                a_end_interface, self.devices[0], b_end_interface,
                self.devices[1])

        return failed('Failed to enable interfaces {}, {} on {}',
                      a_end_interface, b_end_interface,
                      ', '.join(self.devices))