def execute_command_map(self, command_map, send_command_func=None, **kwargs):
     if send_command_func:
         command_template_service.execute_command_map(command_map, send_command_func=send_command_func, **kwargs)
     else:
         command_template_service.execute_command_map(command_map,
                                                      send_command_func=self.cli_service.send_config_command,
                                                      **kwargs)
    def save_configuration(self, destination_host, source_filename, vrf=None):
        system_name = self.context.resource.fullname
        system_name = re.sub(r'[\.\s]', '_', system_name)

        if source_filename and source_filename.lower() == 'running':
            config_type = 'config'
        else:
            raise Exception(self.__class__.__name__,
                            'Device does not support saving \"{}\" configuration type, \"running\" '
                            'is only supported'.format(source_filename or 'None'))

        file_name = "{0}-{1}-{2}".format(system_name, source_filename, time.strftime("%d%m%y-%H%M%S", time.localtime()))
        if not destination_host:
            backup_location = get_attribute_by_name('Backup Location')
            if not backup_location:
                raise Exception('AireOSOperations', "Backup location or path is empty")
        else:
            backup_location = destination_host

        if not backup_location.endswith('/'):
            backup_location += '/'
        destination_dict = UrlParser.parse_url(backup_location)
        if not destination_dict:
            raise Exception('AireOSOperations', 'Incorrect Backup location')
        self.logger.debug('Connection dict: ' + str(destination_dict))

        save_flow = OrderedDict()
        save_flow[save_restore.SAVE_CONFIGURATION_DATATYPE] = config_type
        save_flow[save_restore.SAVE_CONFIGURATION_FILENAME] = file_name

        template_flow = OrderedDict()
        template_flow[save_restore.SAVE_CONFIGURATION_MODE] = UrlParser.SCHEME
        template_flow[save_restore.SAVE_CONFIGURATION_SERVERIP] = UrlParser.HOSTNAME
        template_flow[save_restore.SAVE_CONFIGURATION_PATH] = UrlParser.PATH
        template_flow[save_restore.SAVE_CONFIGURATION_USER] = UrlParser.USERNAME
        template_flow[save_restore.SAVE_CONFIGURATION_PASSWORD] = UrlParser.PASSWORD
        template_flow[save_restore.SAVE_CONFIGURATION_PORT] = UrlParser.PORT
        generated_flow = self._generate_flow(template_flow, destination_dict)
        if save_restore.SAVE_CONFIGURATION_PATH not in generated_flow:
            generated_flow[save_restore.SAVE_CONFIGURATION_PATH] = '/'

        save_flow.update(generated_flow)
        execute_command_map(save_flow, self.cli_service.send_command)

        expected_map = OrderedDict({r'[yY]/[nN]': lambda session: session.send_line('y')})
        error_map = OrderedDict({r'[Ee]rror:': 'Save configuration error, see logs for details'})

        self.cli_service.send_command(save_restore.SAVE_CONFIGURATION_START.get_command(), expected_map=expected_map,
                                      error_map=error_map)

        return file_name
    def restore_configuration(self, source_file, config_type, restore_method='override', vrf=None):

        if not source_file:
            raise Exception('AireOSOperations', 'Configuration URL cannot be empty')

        if not restore_method or restore_method.lower() != 'override':
            raise Exception(self.__class__.__name__, 'Device does not support restoring in \"{}\" method, '
                                                     '"override" is only supported'.format(restore_method or 'None'))

        if not config_type or config_type.lower() != 'running':
            raise Exception(self.__class__.__name__, 'Device does not support restoring in \"{}\" configuration type, '
                                                     '"running" is only supported'.format(config_type or 'None'))

        connection_dict = UrlParser.parse_url(source_file)
        self.logger.debug('Connection dict: ' + str(connection_dict))

        restore_flow = OrderedDict()
        datatype = 'config'
        restore_flow[save_restore.RESTORE_CONFIGURATION_DATATYPE] = datatype

        template_flow = OrderedDict()
        template_flow[save_restore.RESTORE_CONFIGURATION_MODE] = UrlParser.SCHEME
        template_flow[save_restore.RESTORE_CONFIGURATION_USER] = UrlParser.USERNAME
        template_flow[save_restore.RESTORE_CONFIGURATION_PASSWORD] = UrlParser.PASSWORD
        template_flow[save_restore.RESTORE_CONFIGURATION_SERVERIP] = UrlParser.HOSTNAME
        template_flow[save_restore.RESTORE_CONFIGURATION_PORT] = UrlParser.PORT
        template_flow[save_restore.RESTORE_CONFIGURATION_PATH] = UrlParser.PATH
        template_flow[save_restore.RESTORE_CONFIGURATION_FILENAME] = UrlParser.FILENAME

        generated_flow = self._generate_flow(template_flow, connection_dict)
        restore_flow.update(generated_flow)

        execute_command_map(restore_flow, self.cli_service.send_command)

        expected_map = OrderedDict({r'[yY]/[nN]': lambda session: session.send_line('y')})
        error_map = OrderedDict({r'[Ee]rror:': 'Restore configuration error, see logs for details'})

        self.cli_service.send_command(save_restore.RESTORE_CONFIGURATION_START.get_command(),
                                      expected_str=r'System being reset.',
                                      expected_map=expected_map, error_map=error_map)
        session = self.session
        if not session.session_type.lower() == 'console':
            self._wait_session_up(self.session)
    def update_firmware(self, remote_host, file_path, size_of_firmware):
        if not remote_host and not file_path:
            raise Exception('AireOSOperations', 'Configuration URL cannot be empty')
        if remote_host.endswith('/'):
            remote_host = remote_host[:-1]
        if str(file_path).startswith('/'):
            file_path = file_path[1:]

        url = '{0}/{1}'.format(remote_host, file_path)

        flow_template = OrderedDict()
        flow_template[save_restore.RESTORE_CONFIGURATION_MODE] = UrlParser.SCHEME
        flow_template[save_restore.RESTORE_CONFIGURATION_SERVERIP] = UrlParser.HOSTNAME
        flow_template[save_restore.RESTORE_CONFIGURATION_PORT] = UrlParser.PORT
        flow_template[save_restore.RESTORE_CONFIGURATION_PATH] = UrlParser.PATH
        flow_template[save_restore.RESTORE_CONFIGURATION_FILENAME] = UrlParser.FILENAME
        flow_template[save_restore.RESTORE_CONFIGURATION_USER] = UrlParser.USERNAME
        flow_template[save_restore.RESTORE_CONFIGURATION_PASSWORD] = UrlParser.PASSWORD


        # connection_dict = self._parse_url(url)
        #
        restore_flow = OrderedDict()
        datatype = 'code'
        restore_flow[save_restore.RESTORE_CONFIGURATION_DATATYPE] = datatype
        connection_dict = UrlParser.parse_url(url)
        self.logger.debug('Connection dict: ' + str(connection_dict))
        restore_flow.update(self._generate_flow(flow_template, connection_dict))
        self.logger.debug(restore_flow)
        execute_command_map(restore_flow, self.cli_service.send_command)

        expected_map = OrderedDict({r'[yY]/[nN]': lambda session: session.send_line('y')})
        error_map = OrderedDict({r'[Ee]rror:': 'Restore configuration error, see logs for details'})

        self.cli_service.send_command(save_restore.RESTORE_CONFIGURATION_START.get_command(), expected_map=expected_map,
                                      error_map=error_map)
 def execute_command_map(self, command_map):
     command_template_service.execute_command_map(command_map, self.cli_service.send_config_command)