Example #1
0
def main():
    """
    Main, inits mongo, messenger, create channels read channels for device
    and device-data topics and add callbacks to events related to that subjects
    """
    config = Config()
    auth = Auth(config)
    LOGGER.debug("Initializing persister...")
    persister = Persister()
    persister.init_mongodb()
    persister.create_indexes_for_notifications(auth.get_tenants())
    LOGGER.debug("... persister was successfully initialized.")
    LOGGER.debug("Initializing dojot messenger...")
    messenger = Messenger("Persister", config)
    messenger.init()
    messenger.create_channel(config.dojot['subjects']['devices'], "r")
    messenger.create_channel(config.dojot['subjects']['device_data'], "r")
    # TODO: add notifications to config on dojot-module-python
    messenger.create_channel("dojot.notifications", "r")
    messenger.on(config.dojot['subjects']['devices'], "message",
                 persister.handle_event_devices)
    messenger.on(config.dojot['subjects']['device_data'], "message",
                 persister.handle_event_data)
    messenger.on(config.dojot['subjects']['tenancy'], "message",
                 persister.handle_new_tenant)
    messenger.on("dojot.notifications", "message",
                 persister.handle_notification)
    LOGGER.debug("... dojot messenger was successfully initialized.")
Example #2
0
def main():
    config = Config()
    messenger = Messenger("Dojot-Snoop", config)
    messenger.init()
    messenger.create_channel(config.dojot['subjects']['device_data'], "rw")
    messenger.create_channel(config.dojot['subjects']['tenancy'], "rw")
    messenger.create_channel(config.dojot['subjects']['devices'], "rw")

    messenger.on(config.dojot['subjects']['device_data'], "message", rcv_msg)
    messenger.on(config.dojot['subjects']['tenancy'], "message", rcv_msg)
    messenger.on(config.dojot['subjects']['devices'], "message", rcv_msg)
def test_messenger_init():
    patch_messenger_on = patch("dojot.module.Messenger.on")
    # patchAuth = patch("dojot.module.Auth.__init__", return_value=None)
    patch_messenger_process = patch(
        "dojot.module.Messenger.process_new_tenant")
    patch_consumer = patch("dojot.module.kafka.Consumer.__init__",
                           return_value=None)
    # patchGetTenants = patch("dojot.module.Auth.get_tenants", return_value=["tenant1", "tenant2"])

    config = Mock(dojot={
        "subjects": {
            "tenancy": "sample-tenancy-subject"
        },
        "management": {
            "tenant": "sample-management-tenant"
        }
    },
                  kafka={
                      "dojot": {
                          "poll_timeout": 1,
                          "subscription_holdoff": 1
                      },
                      "consumer": {},
                      "producer": {}
                  })

    def reset_scenario():
        mock_messenger_process.reset_mock()

    mock_self = Mock(
        config=config,
        tenants=[],
        auth=Mock(get_tenants=Mock(return_value=["tenant1", "tenant2"])))
    with patch_messenger_on, patch_consumer as mock_consumer, patch_messenger_process as mock_messenger_process:
        mock_self.process_new_tenant = mock_messenger_process
        Messenger.init(mock_self)
        mock_consumer.assert_called()
        mock_messenger_process.assert_any_call('sample-management-tenant',
                                               '{"tenant": "tenant1"}')
        mock_messenger_process.assert_any_call('sample-management-tenant',
                                               '{"tenant": "tenant2"}')

    reset_scenario()
    # patchGetTenants = patch("dojot.module.Auth.get_tenants", return_value=None)
    mock_self = Mock(config=config,
                     tenants=[],
                     auth=Mock(get_tenants=Mock(return_value=None)))
    with pytest.raises(
            UserWarning
    ), patch_messenger_on, patch_messenger_process as mock_messenger_process:
        mock_self.process_new_tenant = mock_messenger_process
        Messenger.init(mock_self)
        mock_messenger_process.assert_not_called()
Example #4
0
def main():
    """
    Main, inits mongo, messenger, create channels read channels for device
    and device-data topics and add callbacks to events related to that subjects
    """
    config = Config()
    LOGGER.debug("Initializing persister...")
    persister = Persister()
    persister.init_mongodb()
    LOGGER.debug("... persister was successfully initialized.")
    LOGGER.debug("Initializing dojot messenger...")
    messenger = Messenger("Persister", config)
    messenger.init()
    messenger.create_channel(config.dojot['subjects']['devices'], "r")
    messenger.create_channel(config.dojot['subjects']['device_data'], "r")
    messenger.on(config.dojot['subjects']['devices'], "message",
                 persister.handle_event_devices)
    messenger.on(config.dojot['subjects']['device_data'], "message",
                 persister.handle_event_data)
    LOGGER.debug("... dojot messenger was successfully initialized.")
Example #5
0
def start_dojot_messenger(config, persister, dojot_persist_notifications_only):

    messenger = Messenger("Persister", config)
    messenger.init()

    messenger.create_channel("dojot.notifications", "r")
    messenger.on(config.dojot['subjects']['tenancy'], "message",
                 persister.handle_new_tenant)
    LOGGER.info("Listen to tenancy events")
    messenger.on("dojot.notifications", "message",
                 persister.handle_notification)
    LOGGER.info('Listen to notification events')

    if str2_bool(dojot_persist_notifications_only) != True:

        messenger.create_channel(config.dojot['subjects']['devices'], "r")
        messenger.create_channel(config.dojot['subjects']['device_data'], "r")
        messenger.on(config.dojot['subjects']['devices'], "message",
                     persister.handle_event_devices)
        messenger.on(config.dojot['subjects']['device_data'], "message",
                     persister.handle_event_data)
        LOGGER.info("Listen to devices events")
Example #6
0
def test_messenger_init():
    patchMessengerOn = patch("dojot.module.Messenger.on")
    patchAuth = patch("dojot.module.Auth.__init__", return_value=None)
    patchMessengerProcess = patch("dojot.module.Messenger.process_new_tenant")
    patchGetTenants = patch("dojot.module.Auth.get_tenants",
                            return_value=["tenant1", "tenant2"])

    config = Mock(
        dojot={
            "subjects": {
                "tenancy": "sample-tenancy-subject"
            },
            "management": {
                "tenant": "sample-management-tenant"
            }
        })

    def reset_scenario():
        mockMessengerProcess.reset_mock()

    mockSelf = Mock(config=config, tenants=[])
    with patchMessengerOn, patchAuth, patchGetTenants, patchMessengerProcess as mockMessengerProcess:
        mockSelf.process_new_tenant = mockMessengerProcess
        Messenger.init(mockSelf)
        mockMessengerProcess.assert_any_call('sample-management-tenant',
                                             '{"tenant": "tenant1"}')
        mockMessengerProcess.assert_any_call('sample-management-tenant',
                                             '{"tenant": "tenant2"}')

    reset_scenario()
    patchGetTenants = patch("dojot.module.Auth.get_tenants", return_value=None)
    mockSelf = Mock(config=config, tenants=[])
    with pytest.raises(
            UserWarning
    ), patchMessengerOn, patchAuth, patchGetTenants, patchMessengerProcess as mockMessengerProcess:
        mockSelf.process_new_tenant = mockMessengerProcess
        Messenger.init(mockSelf)
        mockMessengerProcess.assert_not_called()
Example #7
0
def main():
    """
    Main, inits mongo, messenger, create channels read channels for device
    and device-data topics and add callbacks to events related to that subjects
    """
    config = Config()
    auth = Auth(config)
    LOGGER.debug("Initializing persister...")
    persister = Persister()
    persister.init_mongodb()
    persister.create_indexes_for_notifications(auth.get_tenants())
    LOGGER.debug("... persister was successfully initialized.")
    LOGGER.debug("Initializing dojot messenger...")
    messenger = Messenger("Persister", config)
    messenger.init()
    messenger.create_channel(config.dojot['subjects']['devices'], "r")
    messenger.create_channel(config.dojot['subjects']['device_data'], "r")
    # TODO: add notifications to config on dojot-module-python
    messenger.create_channel("dojot.notifications", "r")
    messenger.on(config.dojot['subjects']['devices'], "message",
                 persister.handle_event_devices)
    messenger.on(config.dojot['subjects']['device_data'], "message",
                 persister.handle_event_data)
    messenger.on(config.dojot['subjects']['tenancy'], "message",
                 persister.handle_new_tenant)
    messenger.on("dojot.notifications", "message",
                 persister.handle_notification)
    LOGGER.debug("... dojot messenger was successfully initialized.")

    # Create falcon app
    app = falcon.API()
    app.add_route('/persister/log', LoggingInterface())
    httpd = simple_server.make_server('0.0.0.0',
                                      os.environ.get("PERSISTER_PORT", 8057),
                                      app)
    httpd.serve_forever()
Example #8
0
        'status': {
            'data': ret['data'],
            'responseType': ret['responseType']
        }
    }
    return make_response(json.dumps(resp_obj), 200)


if __name__ == '__main__':
    while True:
        try:
            # execute the EJBCA handshake and load SOAP API metadata
            initicalConf()
            break
        except requests.exceptions.RequestException:
            print("Cant connect to EJBCA server for initial configuration")
            print("Chances are the server is not ready yet.")
            print("Will retry in 30sec")
            sleep(30)
    # Configure and initalize the messenger
    config = Config()
    messenger = Messenger("ejbca-rest", config)
    messenger.init()
    # Subscribe to devices topics and register callback to process new events
    messenger.create_channel(config.dojot['subjects']['devices'], "r")
    messenger.on(config.dojot['subjects']['devices'], "message",
                 receiver_kafka)
    # Gets all devices that are already active on dojot
    messenger.generate_device_create_event_for_active_devices()
    app.run(host='0.0.0.0', port=5583, threaded=True)