Skip to content

Commit

Permalink
Merge pull request #327 from telefonicaid/task/remove_domain
Browse files Browse the repository at this point in the history
Task/remove domain
  • Loading branch information
fgalan authored Mar 12, 2024
2 parents a4ba779 + 886a994 commit 8cb2f09
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 177 deletions.
2 changes: 2 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Remove: dependency on deprecated domain node module (#268)

3.2.0 (February 27th, 2024)

- Add: support `POST /iot/op/delete` operation at iotAgent API to delete multiple devices at once (iota-node-lib#1578)
Expand Down
51 changes: 25 additions & 26 deletions lib/iotagent-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,50 @@ const protocols = require('./services/protocols');
const configurations = require('./services/configurations');
const iotaRedirector = require('./services/iotaRedirector');
const dbConn = require('./model/dbConn');
const iotaLib = require('iotagent-node-lib');
const async = require('async');
const apply = async.apply;
let northboundServer;
const logger = require('logops');
const context = {
op: 'IoTAManager.NorthboundServer'
};
const loggingMiddleware = require('./utils/logging').requestLogger('IotAgent-Manager');

/**
* Express middleware for tracing the complete request arriving to the IoTA in debug mode.
*/
function traceRequest(req, res, next) {
const logger = req.logger;
logger.debug('Request for path [%s] from [%s]', req.path, req.get('host'));
logger.debug('Headers:\n%j\n', req.headers);
logger.debug('Request for path [%s] query [%j] from [%s]', req.path, req.query, req.get('host'));

if (req.is('json') || req.is('application/ld+json')) {
logger.debug('Body:\n\n%s\n\n', JSON.stringify(req.body, null, 4));
}

next();
}

function startServer(newConfig, callback) {
let baseRoot = '/';

logger.format = logger.formatters.pipe;

logger.getContext = function domainContext() {
var domainObj = require('domain').active || {};
return {
corr: domainObj.corr,
trans: domainObj.trans,
op: domainObj.op,
from: domainObj.from,
srv: domainObj.service,
subsrv: domainObj.subservice,
msg: domainObj.msg,
comp: config.componentName || 'IoTAgentManager'
};
};

northboundServer = {
server: null,
app: express(),
router: express.Router()
};

logger.info(context, 'Starting IoT Agent Manager listening on port [%s]', newConfig.server.port);
logger.debug(context, 'Using config:\n\n%s\n', JSON.stringify(newConfig, null, 4));
logger.info('Starting IoT Agent Manager listening on port [%s]', newConfig.server.port);
logger.debug('Using config:\n\n%s\n', JSON.stringify(newConfig, null, 4));

northboundServer.app.set('port', newConfig.server.port);
northboundServer.app.set('host', newConfig.server.host || '0.0.0.0');
northboundServer.app.set('etag', false);
northboundServer.app.use(iotaLib.requestDomain);
northboundServer.app.use(loggingMiddleware);
northboundServer.app.use(bodyParser.json({ limit: newConfig.bodyParserLimit }));

if (newConfig.logLevel && newConfig.logLevel === 'DEBUG') {
northboundServer.app.use(middleware.traceRequest);
northboundServer.app.use(traceRequest);
}

if (newConfig.server.baseRoot) {
Expand Down Expand Up @@ -120,15 +119,15 @@ function startServer(newConfig, callback) {
function start(newConfig, callback) {
config.setConfig(newConfig);

async.series([apply(startServer, newConfig), dbConn.configureDb], callback);
async.series([apply(startServer, newConfig), apply(dbConn.configureDb, logger)], callback);
}

/**
* Stops the current IoT Manager
*
*/
function stop(callback) {
logger.info(context, 'Stopping IoTA Manager');
logger.info('Stopping IoTA Manager');

if (northboundServer) {
northboundServer.server.close(callback);
Expand All @@ -142,10 +141,10 @@ function stop(callback) {
*
*/
function handleShutdown(signal) {
logger.info(context, 'Received %s, starting shutdown processs', signal);
logger.info('Received %s, starting shutdown processs', signal);
stop((err) => {
if (err) {
logger.error(context, err);
logger.error(err);
return process.exit(1);
}
return process.exit(0);
Expand Down
40 changes: 18 additions & 22 deletions lib/model/dbConn.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@
const mongoose = require('mongoose');
const config = require('../utils/commonConfig');
const constants = require('../utils/constants');
const logger = require('logops');
const async = require('async');
const errors = require('../errors');
let defaultDb;
const DEFAULT_DB_NAME = 'iotagent-manager';
const context = {
op: 'IoTAManager.DbConn'
};

mongoose.Promise = global.Promise; // not including this causes DeprecationWarning

Expand All @@ -50,7 +46,7 @@ function loadModels() {
*
* @this Reference to the dbConn module itself.
*/
function init(host, db, port, username, password, options, callback) {
function init(logger, host, db, port, username, password, options, callback) {
let url;
let credentials = '';
let retries = 0;
Expand Down Expand Up @@ -87,11 +83,11 @@ function init(host, db, port, username, password, options, callback) {
/* eslint-disable-next-line no-unused-vars */
function createConnectionHandler(error, results) {
if (defaultDb) {
logger.info(context, 'Successfully connected to MongoDB.');
logger.info('Successfully connected to MongoDB.');
module.exports.db = defaultDb;
loadModels();
} else {
logger.error(context, 'MONGODB-002: Error found after [%d] attempts: %s', retries, error || lastError);
logger.error('MONGODB-002: Error found after [%d] attempts: %s', retries, error || lastError);
}

callback(error);
Expand All @@ -103,7 +99,6 @@ function init(host, db, port, username, password, options, callback) {

function connectionAttempt(url, options, callback) {
logger.info(
context,
'Attempting to connect to MongoDB instance with url %j and options %j. Attempt %d',
url,
options,
Expand All @@ -116,39 +111,39 @@ function init(host, db, port, username, password, options, callback) {
/* eslint-disable-next-line no-unused-vars */
const candidateDb = mongoose.createConnection(url, options, function (error, result) {
if (error) {
logger.error(context, 'MONGODB-001: Error trying to connect to MongoDB: %s', error);
logger.error('MONGODB-001: Error trying to connect to MongoDB: %s', error);
lastError = error;
} else {
defaultDb = candidateDb;

defaultDb.on('error', function (error) {
logger.error(context, 'Mongo Driver error: %j', error);
logger.error('Mongo Driver error: %j', error);
});
/* eslint-disable-next-line no-unused-vars */
defaultDb.on('connecting', function (error) {
logger.debug(context, 'Mongo Driver connecting');
logger.debug('Mongo Driver connecting');
});
defaultDb.on('connected', function () {
logger.debug(context, 'Mongo Driver connected');
logger.debug('Mongo Driver connected');
});
defaultDb.on('reconnected', function () {
logger.debug(context, 'Mongo Driver reconnected');
logger.debug('Mongo Driver reconnected');
});
defaultDb.on('disconnected', function () {
logger.debug(context, 'Mongo Driver disconnected');
logger.debug('Mongo Driver disconnected');
});
defaultDb.on('reconnectFailed', function () {
logger.error(context, 'MONGODB-004: MongoDB connection was lost');
logger.error('MONGODB-004: MongoDB connection was lost');
process.exit(1);
});
defaultDb.on('disconnecting', function () {
logger.debug(context, 'Mongo Driver disconnecting');
logger.debug('Mongo Driver disconnecting');
});
defaultDb.on('open', function () {
logger.debug(context, 'Mongo Driver open');
logger.debug('Mongo Driver open');
});
defaultDb.on('close', function () {
logger.debug(context, 'Mongo Driver close');
logger.debug('Mongo Driver close');
});
}

Expand All @@ -165,10 +160,10 @@ function init(host, db, port, username, password, options, callback) {
retries++;

if (retries === 1) {
logger.info(context, 'First connection attempt');
logger.info('First connection attempt');
attempt();
} else {
logger.info(context, 'Waiting %d seconds before attempting again.', seconds);
logger.info('Waiting %d seconds before attempting again.', seconds);
setTimeout(attempt, seconds * 1000);
}
}
Expand All @@ -177,11 +172,11 @@ function init(host, db, port, username, password, options, callback) {
async.whilst(retryCheck, tryCreateConnection, createConnectionHandler);
}

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

if (!currentConfig.mongodb || !currentConfig.mongodb.host) {
logger.fatal(context, 'No host found for MongoDB driver.');
logger.fatal('No host found for MongoDB driver.');
callback(new errors.BadConfiguration('No host found for MongoDB driver'));
} else {
let dbName = currentConfig.mongodb.db;
Expand All @@ -197,6 +192,7 @@ function configureDb(callback) {
}

init(
logger,
config.getConfig().mongodb.host,
dbName,
port,
Expand Down
12 changes: 5 additions & 7 deletions lib/services/configurationData.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ const iotagentLib = require('iotagent-node-lib');
const errors = require('../errors');
const logger = require('logops');
const async = require('async');
const context = {
op: 'IoTAManager.ConfigurationDB'
};

const provisioningAPITranslation = {
name: 'id',
service: 'service',
Expand Down Expand Up @@ -68,7 +66,7 @@ function createGetWithFields(fields) {

const callback = arguments[i];

logger.debug(context, 'Looking for configuration with params %j and query %j', fields, queryObj);
logger.debug('Looking for configuration with params %j and query %j', fields, queryObj);

const query = Configuration.model.find(queryObj);

Expand Down Expand Up @@ -98,7 +96,7 @@ function createGetWithFields(fields) {
};
}

function save(protocol, description, iotagent, resource, configuration, oldConfiguration, callback) {
function save(theLogger, protocol, description, iotagent, resource, configuration, oldConfiguration, callback) {
/* eslint-disable-next-line new-cap */
const configurationObj = oldConfiguration || new Configuration.model();
const attributeList = [
Expand All @@ -125,7 +123,7 @@ function save(protocol, description, iotagent, resource, configuration, oldConfi
'payloadType'
];

logger.debug(context, 'Saving Configuration [%s][%s][%s]', protocol, iotagent, resource);
theLogger.debug('Saving Configuration [%s][%s][%s]', protocol, iotagent, resource);

configurationObj.protocol = protocol;
configurationObj.description = description;
Expand All @@ -142,7 +140,7 @@ function save(protocol, description, iotagent, resource, configuration, oldConfi
configurationObj[provisioningAPITranslation[attributeList[i]] || attributeList[i]] = description;
}
}
logger.debug(context, 'Saving Configuration %j translated to %j ', configuration, configurationObj);
theLogger.debug('Saving Configuration %j translated to %j ', configuration, configurationObj);
configurationObj.save(callback);
}

Expand Down
19 changes: 5 additions & 14 deletions lib/services/configurations.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@

const errors = require('../errors');
const configurationData = require('./configurationData');
const logger = require('logops');
const domain = require('../utils/domain');
const fillService = domain.fillService;
var context = {
op: 'IoTAManager.Configurations'
};
const retrievingAPITranslation = {
subservice: 'service_path',
type: 'entity_type',
Expand Down Expand Up @@ -59,7 +53,7 @@ function validateListParameters(req, res, next) {
}
}

function translateToApi(configurations) {
function translateToApi(logger, configurations) {
const services = [];
const attributeList = [
'_id',
Expand Down Expand Up @@ -90,15 +84,15 @@ function translateToApi(configurations) {
'transport'
];

logger.debug(context, 'configurations %j', configurations);
logger.debug('configurations %j', configurations);
for (let j = 0; j < configurations.services.length; j++) {
const service = {};

for (let i = 0; i < attributeList.length; i++) {
service[retrievingAPITranslation[attributeList[i]] || attributeList[i]] =
configurations.services[j][attributeList[i]];
}
logger.debug(context, 'translated to %j', service);
logger.debug('translated to %j', service);
services.push(service);
}

Expand All @@ -109,10 +103,7 @@ function translateToApi(configurations) {
}

function handleListRequest(req, res, next) {
context = fillService(context, {
service: req.headers['fiware-service'],
subservice: req.headers['fiware-servicepath']
});
const logger = req.logger;
configurationData.list(
req.headers['fiware-service'],
req.headers['fiware-servicepath'],
Expand All @@ -125,7 +116,7 @@ function handleListRequest(req, res, next) {
if (error) {
next(error);
} else {
res.status(200).json(translateToApi(configurations));
res.status(200).json(translateToApi(logger, configurations));
}
}
);
Expand Down
Loading

0 comments on commit 8cb2f09

Please sign in to comment.