예제 #1
0
class TestCallLogsManager(TestCase):
    def setUp(self):
        self.cel_fetcher = Mock(CELFetcher)
        self.generator = Mock(CallLogsGenerator)
        self.writer = Mock(CallLogsWriter)
        self.publisher = Mock(BusPublisher)
        self.manager = CallLogsManager(self.cel_fetcher, self.generator, self.writer, self.publisher)

    def tearDown(self):
        pass

    def test_generate_from_count(self):
        cel_count = 132456
        cels = self.cel_fetcher.fetch_last_unprocessed.return_value = [Mock(), Mock()]
        call_logs = self.generator.from_cel.return_value = Mock(new_call_logs=[])

        self.manager.generate_from_count(cel_count=cel_count)

        self.cel_fetcher.fetch_last_unprocessed.assert_called_once_with(cel_count)
        self.generator.from_cel.assert_called_once_with(cels)
        self.writer.write.assert_called_once_with(call_logs)

    def test_generate_from_linked_id(self):
        linked_id = '666'
        cels = self.cel_fetcher.fetch_from_linked_id.return_value = [Mock()]
        call_logs = self.generator.from_cel.return_value = Mock(new_call_logs=[])

        self.manager.generate_from_linked_id(linked_id=linked_id)

        self.cel_fetcher.fetch_from_linked_id.assert_called_once_with(linked_id)
        self.generator.from_cel.assert_called_once_with(cels)
        self.writer.write.assert_called_once_with(call_logs)
예제 #2
0
def _generate_call_logs():
    parser = argparse.ArgumentParser(description='Call logs generator')
    options = parse_args(parser)
    key_config = load_key_file(DEFAULT_CONFIG)
    config = ChainMap(key_config, DEFAULT_CONFIG)

    auth_client = AuthClient(**config['auth'])
    confd_client = ConfdClient(**config['confd'])
    token_renewer = TokenRenewer(auth_client)
    token_renewer.subscribe_to_token_change(confd_client.set_token)

    cel_fetcher = CELFetcher()
    generator = CallLogsGenerator([
        LocalOriginateCELInterpretor(confd_client),
        DispatchCELInterpretor(CallerCELInterpretor(confd_client),
                               CalleeCELInterpretor(confd_client))
    ])
    writer = CallLogsWriter()
    publisher = BusPublisher(config)
    manager = CallLogsManager(cel_fetcher, generator, writer, publisher)

    options = vars(options)
    with token_renewer:
        if options.get('action') == 'delete':
            if options.get('all'):
                manager.delete_all()
            elif options.get('days'):
                manager.delete_from_days(options['days'])
        else:
            if options.get('days'):
                manager.generate_from_days(days=options['days'])
            else:
                manager.generate_from_count(cel_count=options['cel_count'])
예제 #3
0
 def setUp(self):
     self.cel_fetcher = Mock(CELFetcher)
     self.generator = Mock(CallLogsGenerator)
     self.writer = Mock(CallLogsWriter)
     self.publisher = Mock(BusPublisher)
     self.manager = CallLogsManager(self.cel_fetcher, self.generator,
                                    self.writer, self.publisher)
예제 #4
0
 def setUp(self):
     self.dao = Mock()
     self.generator = Mock(CallLogsGenerator)
     self.writer = Mock(CallLogsWriter)
     self.publisher = Mock(BusPublisher)
     self.manager = CallLogsManager(
         self.dao,
         self.generator,
         self.writer,
         self.publisher,
     )
예제 #5
0
def _generate_call_logs():
    parser = argparse.ArgumentParser(description='Call logs generator')
    options = parse_args(parser)

    file_config = {
        key: value
        for key, value in read_config_file_hierarchy(DEFAULT_CONFIG).items()
        if key in ('confd', 'bus', 'auth', 'db_uri', 'cel_db_uri')
    }
    key_config = {}
    auth_username = file_config['auth'].get('username')
    auth_password = file_config['auth'].get('password')
    if not (auth_username and auth_password):
        key_config = load_key_file(ChainMap(file_config, DEFAULT_CONFIG))
    config = ChainMap(key_config, file_config, DEFAULT_CONFIG)
    set_xivo_uuid(config, logger)
    init_db_from_config({'db_uri': config['cel_db_uri']})
    DBSession = new_db_session(config['db_uri'])
    CELDBSession = new_db_session(config['cel_db_uri'])
    dao = DAO(DBSession, CELDBSession)

    auth_client = AuthClient(**config['auth'])
    confd_client = ConfdClient(**config['confd'])
    token_renewer = TokenRenewer(auth_client)
    token_renewer.subscribe_to_token_change(confd_client.set_token)

    generator = CallLogsGenerator(
        confd_client,
        [
            LocalOriginateCELInterpretor(),
            DispatchCELInterpretor(CallerCELInterpretor(),
                                   CalleeCELInterpretor()),
        ],
    )
    token_renewer.subscribe_to_next_token_details_change(
        generator.set_default_tenant_uuid)
    writer = CallLogsWriter(dao)
    publisher = BusPublisher(service_uuid=config['uuid'], **config['bus'])
    manager = CallLogsManager(dao, generator, writer, publisher)

    options = vars(options)
    with token_renewer:
        if options.get('action') == 'delete':
            if options.get('all'):
                manager.delete_all()
            elif options.get('days'):
                manager.delete_from_days(options['days'])
        else:
            if options.get('days'):
                manager.generate_from_days(days=options['days'])
            else:
                manager.generate_from_count(cel_count=options['cel_count'])
예제 #6
0
def _generate_call_logs():
    parser = argparse.ArgumentParser(description='Call logs generator')
    options = parse_args(parser)

    file_config = {
        key: value
        for key, value in read_config_file_hierarchy(DEFAULT_CONFIG).items()
        if key in ('confd', 'bus', 'auth', 'db_uri')
    }
    key_config = load_key_file(ChainMap(file_config, DEFAULT_CONFIG))
    config = ChainMap(key_config, file_config, DEFAULT_CONFIG)
    init_db_from_config(config)

    auth_client = AuthClient(**config['auth'])
    confd_client = ConfdClient(**config['confd'])
    token_renewer = TokenRenewer(auth_client)
    token_renewer.subscribe_to_token_change(confd_client.set_token)

    cel_fetcher = CELFetcher()
    generator = CallLogsGenerator(
        confd_client,
        [
            LocalOriginateCELInterpretor(confd_client),
            DispatchCELInterpretor(
                CallerCELInterpretor(confd_client), CalleeCELInterpretor(confd_client)
            ),
        ],
    )
    token_renewer.subscribe_to_next_token_details_change(
        generator.set_default_tenant_uuid
    )
    writer = CallLogsWriter()
    publisher = BusPublisher(config)
    manager = CallLogsManager(cel_fetcher, generator, writer, publisher)

    options = vars(options)
    with token_renewer:
        if options.get('action') == 'delete':
            if options.get('all'):
                manager.delete_all()
            elif options.get('days'):
                manager.delete_from_days(options['days'])
        else:
            if options.get('days'):
                manager.generate_from_days(days=options['days'])
            else:
                manager.generate_from_count(cel_count=options['cel_count'])
예제 #7
0
 def __init__(self, config):
     auth_config = dict(config['auth'])
     auth_config.pop('key_file', None)
     auth_client = AuthClient(**auth_config)
     cel_fetcher = CELFetcher()
     confd_client = ConfdClient(**config['confd'])
     generator = CallLogsGenerator([
         LocalOriginateCELInterpretor(confd_client),
         DispatchCELInterpretor(CallerCELInterpretor(confd_client),
                                CalleeCELInterpretor(confd_client))
     ])
     writer = CallLogsWriter()
     self._publisher = BusPublisher(config)
     self.manager = CallLogsManager(cel_fetcher, generator, writer,
                                    self._publisher)
     self.bus_client = BusClient(config)
     self.rest_api = CoreRestApi(config)
     self.token_renewer = TokenRenewer(auth_client)
     self.token_renewer.subscribe_to_token_change(confd_client.set_token)
     self._load_plugins(config)
예제 #8
0
 def __init__(self, config):
     auth_client = AuthClient(**config['auth'])
     cel_fetcher = CELFetcher()
     confd_client = ConfdClient(**config['confd'])
     generator = CallLogsGenerator(
         confd_client,
         [
             LocalOriginateCELInterpretor(confd_client),
             DispatchCELInterpretor(
                 CallerCELInterpretor(confd_client),
                 CalleeCELInterpretor(confd_client),
             ),
         ],
     )
     writer = CallLogsWriter()
     self.token_renewer = TokenRenewer(auth_client)
     self.token_renewer.subscribe_to_token_change(confd_client.set_token)
     self.token_renewer.subscribe_to_next_token_details_change(
         generator.set_default_tenant_uuid)
     self._publisher = BusPublisher(config)
     self.manager = CallLogsManager(cel_fetcher, generator, writer,
                                    self._publisher)
     self.bus_client = BusClient(config)
     self.rest_api = CoreRestApi(config)
     self.status_aggregator = StatusAggregator()
     self.token_status = TokenStatus()
     plugin_helpers.load(
         namespace='wazo_call_logd.plugins',
         names=config['enabled_plugins'],
         dependencies={
             'api': api,
             'config': config,
             'token_renewer': self.token_renewer,
             'status_aggregator': self.status_aggregator,
         },
     )
예제 #9
0
    def __init__(self, config):
        self.config = config
        DBSession = new_db_session(config['db_uri'])
        CELDBSession = new_db_session(config['cel_db_uri'])
        self.dao = DAO(DBSession, CELDBSession)
        writer = CallLogsWriter(self.dao)

        # NOTE(afournier): it is important to load the tasks before configuring the Celery app
        self.celery_task_manager = plugin_helpers.load(
            namespace='wazo_call_logd.celery_tasks',
            names=config['enabled_celery_tasks'],
            dependencies={
                'config': self.config,
                'dao': self.dao,
                'app': celery.app,
            },
        )
        celery.configure(config)
        self._celery_process = celery.spawn_workers(config)

        auth_client = AuthClient(**config['auth'])
        confd_client = ConfdClient(**config['confd'])
        generator = CallLogsGenerator(
            confd_client,
            [
                LocalOriginateCELInterpretor(),
                DispatchCELInterpretor(
                    CallerCELInterpretor(),
                    CalleeCELInterpretor(),
                ),
            ],
        )
        self.token_renewer = TokenRenewer(auth_client)
        self.token_renewer.subscribe_to_token_change(confd_client.set_token)
        self.token_renewer.subscribe_to_next_token_details_change(
            generator.set_default_tenant_uuid)

        self.bus_publisher = BusPublisher(service_uuid=config['uuid'],
                                          **config['bus'])
        self.bus_consumer = BusConsumer(**config['bus'])
        self.manager = CallLogsManager(self.dao, generator, writer,
                                       self.bus_publisher)

        self._bus_subscribe()

        self.http_server = HTTPServer(config)
        if not app.config['auth'].get('master_tenant_uuid'):
            self.token_renewer.subscribe_to_next_token_details_change(
                init_master_tenant)

        self.status_aggregator = StatusAggregator()
        self.token_status = TokenStatus()
        plugin_helpers.load(
            namespace='wazo_call_logd.plugins',
            names=config['enabled_plugins'],
            dependencies={
                'api': api,
                'config': config,
                'dao': self.dao,
                'token_renewer': self.token_renewer,
                'status_aggregator': self.status_aggregator,
                'bus_publisher': self.bus_publisher,
            },
        )
예제 #10
0
class Controller:
    def __init__(self, config):
        self.config = config
        DBSession = new_db_session(config['db_uri'])
        CELDBSession = new_db_session(config['cel_db_uri'])
        self.dao = DAO(DBSession, CELDBSession)
        writer = CallLogsWriter(self.dao)

        # NOTE(afournier): it is important to load the tasks before configuring the Celery app
        self.celery_task_manager = plugin_helpers.load(
            namespace='wazo_call_logd.celery_tasks',
            names=config['enabled_celery_tasks'],
            dependencies={
                'config': self.config,
                'dao': self.dao,
                'app': celery.app,
            },
        )
        celery.configure(config)
        self._celery_process = celery.spawn_workers(config)

        auth_client = AuthClient(**config['auth'])
        confd_client = ConfdClient(**config['confd'])
        generator = CallLogsGenerator(
            confd_client,
            [
                LocalOriginateCELInterpretor(),
                DispatchCELInterpretor(
                    CallerCELInterpretor(),
                    CalleeCELInterpretor(),
                ),
            ],
        )
        self.token_renewer = TokenRenewer(auth_client)
        self.token_renewer.subscribe_to_token_change(confd_client.set_token)
        self.token_renewer.subscribe_to_next_token_details_change(
            generator.set_default_tenant_uuid)

        self.bus_publisher = BusPublisher(service_uuid=config['uuid'],
                                          **config['bus'])
        self.bus_consumer = BusConsumer(**config['bus'])
        self.manager = CallLogsManager(self.dao, generator, writer,
                                       self.bus_publisher)

        self._bus_subscribe()

        self.http_server = HTTPServer(config)
        if not app.config['auth'].get('master_tenant_uuid'):
            self.token_renewer.subscribe_to_next_token_details_change(
                init_master_tenant)

        self.status_aggregator = StatusAggregator()
        self.token_status = TokenStatus()
        plugin_helpers.load(
            namespace='wazo_call_logd.plugins',
            names=config['enabled_plugins'],
            dependencies={
                'api': api,
                'config': config,
                'dao': self.dao,
                'token_renewer': self.token_renewer,
                'status_aggregator': self.status_aggregator,
                'bus_publisher': self.bus_publisher,
            },
        )

    def run(self):
        logger.info('Starting wazo-call-logd')
        self.token_renewer.subscribe_to_token_change(
            self.token_status.token_change_callback)
        self.status_aggregator.add_provider(self.bus_consumer.provide_status)
        self.status_aggregator.add_provider(self.token_status.provide_status)
        self.status_aggregator.add_provider(celery.provide_status)
        signal.signal(signal.SIGTERM, partial(_sigterm_handler, self))

        self._update_db_from_config_file()

        try:
            with self.bus_consumer:
                with self.token_renewer:
                    self.http_server.run()
        finally:
            logger.info('Stopping wazo-call-logd')
            self._celery_process.terminate()
            self._celery_process.join()

    def stop(self, reason):
        logger.warning('Stopping wazo-call-logd: %s', reason)
        self.http_server.stop()

    def _update_db_from_config_file(self):
        with self.dao.helper.db_ready():
            config = self.dao.config.find_or_create()
            cdr_days = self.config['retention']['cdr_days']
            if cdr_days is not None:
                config.retention_cdr_days = cdr_days
                config.retention_cdr_days_from_file = True
            else:
                config.retention_cdr_days_from_file = False
            export_days = self.config['retention']['export_days']
            if export_days is not None:
                config.retention_export_days = export_days
                config.retention_export_days_from_file = True
            else:
                config.retention_export_days_from_file = False
            recording_days = self.config['retention']['recording_days']
            if recording_days is not None:
                config.retention_recording_days = recording_days
                config.retention_recording_days_from_file = True
            else:
                config.retention_recording_days_from_file = False
            self.dao.config.update(config)

    def _bus_subscribe(self):
        self.bus_consumer.subscribe('CEL', self._handle_linked_id_end)

    def _handle_linked_id_end(self, payload):
        if payload['EventName'] != 'LINKEDID_END':
            return

        linked_id = payload['LinkedID']
        try:
            self.manager.generate_from_linked_id(linked_id)
        except Exception:
            logger.exception(
                'Failed to genereate call log for linked id=\"%s\"', linked_id)