Skip to content
Merged
4 changes: 3 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Add IOTA_MONGO_URI env var
- Deprecated: IOTA_MONGO_ different from IOTA_MONGO_URI (use IOTA_MONGO_URI instead)
- Fix: broken notification command mode (it was not working without providing targetEntityId and targetEntityType in the notification, which belongs to another command mode: advancedNotification)
- Fix: use normalized in attrsFormat for commands subscriptions (#1743)
- Add: locale string number jexl function (#1737)
- Add: locale string number jexl function (#1737)
35 changes: 10 additions & 25 deletions doc/admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,8 @@ the `mongob` section (as described bellow). E.g.:

#### `mongodb`

It configures the MongoDB driver for those repositories with 'mongodb' type. If the `host` parameter is a list of
comma-separated IPs, they will be considered to be part of a Replica Set. In that case, the optional property
`replicaSet` should contain the Replica Set name. If the database requires authentication, username (`user`), password
(`password`) and authSource (`authSource`) can be set. If the database requires TLS/SSL connection but any validation of
the certificate chain is not mandatory, all you need is to set the ssl (`ssl`) option as `true` to connect the database.
If you need to add more complex option(s) such as `retryWrites=true` or `w=majority` when connection database, extraArgs
(`extraArgs`) can be used to perform it. For The MongoBD driver will retry the connection at startup time `retries`
times, waiting `retryTime` seconds between attempts, if those attributes are present (default values are 5 and 5
respectively). E.g.:
It configures the MongoDB driver for those repositories with 'mongodb' type, using the `uri` parameter (which format is
available in [this reference](http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html)).

```javascript
{
Expand Down Expand Up @@ -454,12 +447,14 @@ This flag is overwritten by `useCBflowControl` flag in group or device. This fla

#### `cmdMode`

Set command mode for the IoTAgent instance (it can be overriden by the `cmdMode` at group or device level). Possible values are:
Set command mode for the IoTAgent instance (it can be overriden by the `cmdMode` at group or device level). Possible
values are:

* `legacy` (used as default if this setting is not defined): IoTAgent commands will use Context Broker registers
mechanims.
* `notification`: IoTAgent commands will use subscriptions to be notified for Context Broker commands.
* `advancedNotification`: IoTAgent commands will use subscriptions to be notified for Context Broker commands (but in a different way as in `notification` mode)
- `legacy` (used as default if this setting is not defined): IoTAgent commands will use Context Broker registers
mechanims.
- `notification`: IoTAgent commands will use subscriptions to be notified for Context Broker commands.
- `advancedNotification`: IoTAgent commands will use subscriptions to be notified for Context Broker commands (but in
a different way as in `notification` mode)

Have a look to [this document](devel/northboundinteractions.md) for more detail on how this modes work.

Expand Down Expand Up @@ -505,17 +500,7 @@ overrides.
| IOTA_IOTAM_AGENTPATH | `iotManager.agentPath` |
| IOTA_IOTAM_PROTOCOL | `iotManager.protocol` |
| IOTA_IOTAM_DESCRIPTION | `iotManager.description` |
| IOTA_MONGO_HOST | `mongodb.host` |
| IOTA_MONGO_PORT | `mongodb.port` |
| IOTA_MONGO_DB | `mongodb.db` |
| IOTA_MONGO_REPLICASET | `mongodb.replicaSet` |
| IOTA_MONGO_USER | `mongodb.user` |
| IOTA_MONGO_PASSWORD | `mongodb.password` |
| IOTA_MONGO_AUTH_SOURCE | `mongodb.authSource` |
| IOTA_MONGO_RETRIES | `mongodb.retries` |
| IOTA_MONGO_RETRY_TIME | `mongodb.retryTime` |
| IOTA_MONGO_SSL | `mongodb.ssl` |
| IOTA_MONGO_EXTRAARGS | `mongodb.extraArgs` |
| IOTA_MONGO_URI | `mongodb.uri` |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other IOTA_MONGO_X env var should be declared deprecated and removed from the documentation.

Similar to what we did in Orion

https://github.com/telefonicaid/fiware-orion/releases/tag/3.12.0
https://github.com/telefonicaid/fiware-orion/releases/tag/4.0.0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ba0105e

| IOTA_POLLING_EXPIRATION | `pollingExpiration` |
| IOTA_POLLING_DAEMON_FREQ | `pollingDaemonFrequency` |
| IOTA_MULTI_CORE | `multiCore` |
Expand Down
12 changes: 12 additions & 0 deletions doc/deprecated.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ longer. In particular:

A list of deprecated features and the version in which they were deprecated follows:

- Config `mongodb.*` different from `mongodb.uri` (and associated env vars `IOTA_MONGO_*` except `IOTA_MONGO_URI`)
- Support to NGSI v1 (finally removed in 2.18.0)
- Support to Node.js v4 in iotagent-node-lib 2.8.1 (finally removed in 2.9.0)
- Support to Node.js v6 in iotagent-node-lib 2.9.0 (finally removed in 2.10.0)
Expand Down Expand Up @@ -61,3 +62,14 @@ The following table provides information about the last iotagent-node-lib versio
| bidirectional plugin | 3.3.0 | August 24th, 2023 |
| appendMode configuration (`IOTA_APPEND_MODE` env var) | 3.3.0 | August 24th, 2023 |
| push-mode stats | 4.5.0 | June 11th, 2024 |
| config `mongodb.host` (env var `IOTA_MONGO_HOST`) | 4.10.0 | October 6th, 2025 |
| config `mongodb.port` (env var `IOTA_MONGO_PORT`) | 4.10.0 | October 6th, 2025 |
| config `mongodb.db` (env var `IOTA_MONGO_DB`) | 4.10.0 | October 6th, 2025 |
| config `mongodb.replicaSet` (env var `IOTA_MONGO_REPLICASET`) | 4.10.0 | October 6th, 2025 |
| config `mongodb.user` (env var `IOTA_MONGO_USER`) | 4.10.0 | October 6th, 2025 |
| config `mongodb.password` (env var `IOTA_MONGO_PASSWORD`) | 4.10.0 | October 6th, 2025 |
| config `mongodb.authSource` (env var `IOTA_MONGO_AUTH_SOURCE`) | 4.10.0 | October 6th, 2025 |
| config `mongodb.retries` (env var `IOTA_MONGO_RETRIES`) | 4.10.0 | October 6th, 2025 |
| config `mongodb.retryTime` (env var `IOTA_MONGO_RETRY_TIME`) | 4.10.0 | October 6th, 2025 |
| config `mongodb.ssl` (env var `IOTA_MONGO_SSL`) | 4.10.0 | October 6th, 2025 |
| config `mongodb.extraArgs` (env var `IOTA_MONGO_EXTRAARGS`) | 4.10.0 | October 6th, 2025 |
7 changes: 7 additions & 0 deletions lib/commonConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ function processEnvironmentVariables() {
'IOTA_IOTAM_AGENTPATH'
];
const mongoVariables = [
'IOTA_MONGO_URI',
// FIXME: the following IOTA_MONGO_ env vars are deprecated and will
// be eventually removed (use IOTA_MONGO_URI)
'IOTA_MONGO_HOST',
'IOTA_MONGO_PORT',
'IOTA_MONGO_DB',
Expand Down Expand Up @@ -409,6 +412,10 @@ function processEnvironmentVariables() {
config.mongodb = {};
}

if (process.env.IOTA_MONGO_URI) {
config.mongodb.uri = process.env.IOTA_MONGO_URI;
}

if (process.env.IOTA_MONGO_HOST) {
config.mongodb.host = process.env.IOTA_MONGO_HOST;
}
Expand Down
115 changes: 57 additions & 58 deletions lib/model/dbConn.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* please contact with::[email protected]
*/

/**
/*
* This module sets up the connection with the mongodb through mongoose. This connection will be used
* in mongoose schemas to persist objects.
*/
Expand Down Expand Up @@ -49,8 +49,7 @@ function loadModels() {
*
* @this Reference to the dbConn module itself.
*/

function init(host, db, port, options, callback) {
function init(host, db, port, options, callback, fullUri = null) {
let url;
let retries = 0;
let lastError;
Expand All @@ -60,21 +59,25 @@ function init(host, db, port, options, callback) {
return `${item}:${port}`;
}

url = 'mongodb://';
if (fullUri) {
url = fullUri;
} else {
url = 'mongodb://';

if (options.auth) {
url += `${encodeURIComponent(options.auth.user)}:${encodeURIComponent(options.auth.password)}@`;
}
if (options.auth) {
url += `${encodeURIComponent(options.auth.user)}:${encodeURIComponent(options.auth.password)}@`;
}

const hosts = host.split(',').map(addPort).join(',');
url += `${hosts}/${db}`;
const hosts = host.split(',').map(addPort).join(',');
url += `${hosts}/${db}`;

if (options.extraArgs) {
const query = new URLSearchParams(options.extraArgs).toString();
if (query) {
url += `?${query}`;
if (options.extraArgs) {
const query = new URLSearchParams(options.extraArgs).toString();
if (query) {
url += `?${query}`;
}
delete options.extraArgs;
}
delete options.extraArgs;
}

function connectionAttempt(callback) {
Expand All @@ -91,32 +94,17 @@ function init(host, db, port, options, callback) {
lastError = error;
alarms.raise(constants.MONGO_ALARM, error);
});
/* eslint-disable-next-line no-unused-vars */
defaultDb.on('connecting', function (error) {
logger.debug(context, 'Mongo Driver connecting');
});
defaultDb.on('connected', function () {
logger.debug(context, 'Mongo Driver connected');
});
defaultDb.on('reconnected', function () {
logger.debug(context, 'Mongo Driver reconnected');
});
defaultDb.on('disconnected', function () {
logger.debug(context, 'Mongo Driver disconnected');
});
defaultDb.on('reconnectFailed', function () {
defaultDb.on('connecting', () => logger.debug(context, 'Mongo Driver connecting'));
defaultDb.on('connected', () => logger.debug(context, 'Mongo Driver connected'));
defaultDb.on('reconnected', () => logger.debug(context, 'Mongo Driver reconnected'));
defaultDb.on('disconnected', () => logger.debug(context, 'Mongo Driver disconnected'));
defaultDb.on('reconnectFailed', () => {
logger.error(context, 'MONGODB-004: MongoDB connection was lost');
process.exit(1);
});
defaultDb.on('disconnecting', function () {
logger.debug(context, 'Mongo Driver disconnecting');
});
defaultDb.on('open', function () {
logger.debug(context, 'Mongo Driver open');
});
defaultDb.on('close', function () {
logger.debug(context, 'Mongo Driver close');
});
defaultDb.on('disconnecting', () => logger.debug(context, 'Mongo Driver disconnecting'));
defaultDb.on('open', () => logger.debug(context, 'Mongo Driver open'));
defaultDb.on('close', () => logger.debug(context, 'Mongo Driver close'));
callback();
})
.catch((err) => {
Expand Down Expand Up @@ -144,34 +132,45 @@ function init(host, db, port, options, callback) {

function configureDb(callback) {
const currentConfig = config.getConfig();

if (currentConfig.deviceRegistry?.type === 'mongodb') {
if (!currentConfig.mongodb?.host) {
logger.fatal(context, 'MONGODB-003: No host found for MongoDB driver.');
callback(new errors.BadConfiguration('No host found for MongoDB driver'));
} else {
const dbName = currentConfig.mongodb.db || DEFAULT_DB_NAME;
const port = currentConfig.mongodb.port || 27017;
const mongoCfg = currentConfig.mongodb;

if (mongoCfg?.uri) {
const options = {};
logger.info(context, `Using full MongoDB URI from configuration`);
init(null, null, null, options, callback, mongoCfg.uri);
return;
}

if (currentConfig.mongodb.replicaSet) options.replicaSet = currentConfig.mongodb.replicaSet;
if (currentConfig.mongodb.ssl) options.ssl = currentConfig.mongodb.ssl;
if (currentConfig.mongodb.extraArgs) options.extraArgs = currentConfig.mongodb.extraArgs;
if (!mongoCfg?.host) {
logger.fatal(context, 'MONGODB-003: No host found for MongoDB driver.');
callback(new errors.BadConfiguration('No host found for MongoDB driver'));
return;
}

if (currentConfig.mongodb.user && currentConfig.mongodb.password) {
options.auth = {
user: currentConfig.mongodb.user,
password: currentConfig.mongodb.password
const dbName = mongoCfg.db || DEFAULT_DB_NAME;
const port = mongoCfg.port || 27017;
const options = {};

if (mongoCfg.replicaSet) options.replicaSet = mongoCfg.replicaSet;
if (mongoCfg.ssl) options.ssl = mongoCfg.ssl;
if (mongoCfg.extraArgs) options.extraArgs = mongoCfg.extraArgs;

if (mongoCfg.user && mongoCfg.password) {
options.auth = {
user: mongoCfg.user,
password: mongoCfg.password
};
if (mongoCfg.authSource) {
options.extraArgs = {
...options.extraArgs,
authSource: mongoCfg.authSource
};
if (currentConfig.mongodb.authSource) {
options.extraArgs = {
...options.extraArgs,
authSource: currentConfig.mongodb.authSource
};
}
}

init(currentConfig.mongodb.host, dbName, port, options, callback);
}

init(mongoCfg.host, dbName, port, options, callback);
} else {
callback();
}
Expand Down