Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Feature/config groups #1197

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 76 additions & 64 deletions doc/api.md

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions lib/fiware-iotagent-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const iotManager = require('./services/common/iotManagerService');
const contextServer = require('./services/northBound/northboundServer');
const errors = require('./errors');
const constants = require('./constants');
const request = require('./request-shim');
const logger = require('logops');
const config = require('./commonConfig');
const cluster = require('cluster');
Expand Down Expand Up @@ -324,9 +323,10 @@ exports.setDataUpdateHandler = contextServer.setUpdateHandler;
exports.setCommandHandler = contextServer.setCommandHandler;
exports.setDataQueryHandler = contextServer.setQueryHandler;
exports.setConfigurationHandler = contextServer.setConfigurationHandler;
exports.setGroupConfigurationHandler = contextServer.setGroupConfigurationHandler;
exports.setRemoveConfigurationHandler = contextServer.setRemoveConfigurationHandler;
exports.setRemoveGroupConfigurationHandler = contextServer.setRemoveGroupConfigurationHandler;
exports.setProvisioningHandler = contextServer.setProvisioningHandler;
exports.setUpdatingHandler = contextServer.setUpdatingHandler;
exports.setRemoveDeviceHandler = contextServer.setRemoveDeviceHandler;
exports.setNotificationHandler = contextServer.setNotificationHandler;
exports.addUpdateMiddleware = ngsi.addUpdateMiddleware;
Expand All @@ -351,7 +351,6 @@ exports.ensureSouthboundDomain = domainUtils.ensureSouthboundDomain;
exports.finishSouthBoundTransaction = domainUtils.finishSouthBoundTransaction;
exports.requestDomain = domainUtils.requestDomain;
exports.regenerateTransid = domainUtils.regenerateTransid;
exports.fillService = domainUtils.fillService;

exports.middlewares = middlewares;

Expand All @@ -374,4 +373,3 @@ exports.constants = constants;
exports.logModule = logger;
exports.configModule = config;
exports.startServer = startServer;
exports.request = request;
62 changes: 62 additions & 0 deletions lib/services/groups/groupRegistryMemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,40 @@ function getConfigurationsByService(service) {
});
}

/**
* List all the configGroups created in the IoT Agent. The result, passed as a parameter in the callback
* will be an object with two attributes: the services array with the list of groups; and a count
* attribute with the total number of groups in the collection.
*
* @param {Number} service Service for wich all the configurations want to be retrieved.
* @param {Number} limit Maximum number of entries to return.
* @param {Number} offset Number of entries to skip for pagination.
*/
function listConfigGroups(service, limit, offset, callback) {
const result = [];
let skipped = 0;
const filteredGroups = getConfigurationsByService(service);

for (const i in filteredGroups) {
if (registeredGroups.hasOwnProperty(filteredGroups[i])) {
if (offset && skipped < parseInt(offset, 10)) {
skipped++;
} else {
result.push(registeredGroups[filteredGroups[i]]);
}

if (limit && result.length === parseInt(limit, 10)) {
break;
}
}
}

callback(null, {
count: filteredGroups.length,
configGroups: result
});
}

/**
* List all the groups created in the IoT Agent. The result, passed as a parameter in the callback
* will be an object with two attributes: the services array with the list of groups; and a count
Expand Down Expand Up @@ -152,6 +186,32 @@ function findSingleConfigurationMode(service, subservice, callback) {
}
}

function findConfigGroups(service, subservice, callback) {
if (config.getConfig().singleConfigurationMode === true) {
return findSingleConfigurationMode(service, subservice, callback);
}
const result = [];

for (const i in registeredGroups) {
if (
registeredGroups.hasOwnProperty(i) &&
registeredGroups[i].service === service &&
registeredGroups[i].subservice === subservice
) {
result.push(registeredGroups[i]);
}
}

if (result.length > 0) {
return callback(null, {
count: result.length,
configGroups: result
});
} else {
return callback(new errors.DeviceGroupNotFound(service, subservice));
}
}

function find(service, subservice, callback) {
if (config.getConfig().singleConfigurationMode === true) {
return findSingleConfigurationMode(service, subservice, callback);
Expand Down Expand Up @@ -284,8 +344,10 @@ function remove(id, callback) {

exports.create = intoTrans(context, createGroup);
exports.list = intoTrans(context, listGroups);
exports.listConfigGroups = intoTrans(context, listConfigGroups);
exports.init = intoTrans(context, init);
exports.find = intoTrans(context, find);
exports.findConfigGroups = intoTrans(context, findConfigGroups);
exports.findBy = intoTrans(context, findBy);
exports.findType = intoTrans(context, findBy(['service', 'subservice', 'type']));
exports.findTypeSilently = intoTrans(context, findBy(['service', 'subservice', 'type']));
Expand Down
71 changes: 71 additions & 0 deletions lib/services/groups/groupRegistryMongoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,45 @@ function createGroup(group, callback) {
});
}

/**
* List all the configGroups created in the IoT Agent.
*
* @param {Number} service Service for wich all the configurations want to be retrieved.
* @param {Number} limit Maximum number of entries to return.
* @param {Number} offset Number of entries to skip for pagination.
*/
function listConfigGroups(service, limit, offset, callback) {
const condition = {};

function toObjectFn(obj) {
return obj.toObject();
}

if (service) {
condition.service = service;
}

const query = Group.model.find(condition).sort();

if (limit) {
query.limit(parseInt(limit, 10));
}

if (offset) {
query.skip(parseInt(offset, 10));
}

async.series([query.exec.bind(query), Group.model.countDocuments.bind(Group.model, condition)], function (
error,
results
) {
callback(error, {
count: results[1],
configGroups: results[0].map(toObjectFn)
});
});
}

/**
* List all the groups created in the IoT Agent.
*
Expand Down Expand Up @@ -181,6 +220,36 @@ function getById(id, callback) {
});
}

/**
* List all the configGroups created in the IoT Agent for a service and a subservice.
*
* @param {String} service Service used to filter the groups.
* @param {String} subservice Subservice used to filter the groups.
* @param {Function} callback The callback function.
*/
function findConfigGroups(service, subservice, callback) {
const condition = {};

function toObjectFn(obj) {
return obj.toObject();
}

condition.service = service;
condition.subservice = subservice;

const query = Group.model.find(condition).sort();

async.series([query.exec.bind(query), Group.model.countDocuments.bind(Group.model, condition)], function (
error,
results
) {
callback(error, {
count: results[1],
configGroups: results[0].map(toObjectFn)
});
});
}

/**
* List all the groups created in the IoT Agent for a service and a subservice.
*
Expand Down Expand Up @@ -301,8 +370,10 @@ function clear(callback) {

exports.create = alarmsInt(constants.MONGO_ALARM, intoTrans(context, createGroup));
exports.list = alarmsInt(constants.MONGO_ALARM, intoTrans(context, listGroups));
exports.listConfigGroups = alarmsInt(constants.MONGO_ALARM, intoTrans(context, listConfigGroups));
exports.init = alarmsInt(constants.MONGO_ALARM, intoTrans(context, init));
exports.find = alarmsInt(constants.MONGO_ALARM, intoTrans(context, find));
exports.findConfigGroups = alarmsInt(constants.MONGO_ALARM, intoTrans(context, findConfigGroups));
exports.findType = alarmsInt(
constants.MONGO_ALARM,
intoTrans(context, findBy(['service', 'subservice', 'type', 'apikey']))
Expand Down
44 changes: 33 additions & 11 deletions lib/services/groups/groupService.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const context = {
*
* @param {Object} group Validate the device group
*/
function validateGroup(group, callback) {
function validateGroup(endpoint, group, callback) {
const validations = [];
logger.debug(context, 'validateGroup %j', group);

Expand All @@ -60,7 +60,13 @@ function validateGroup(group, callback) {
}

function checkServiceAndSubservice(innerCb) {
config.getGroupRegistry().find(group.service, group.subservice, generateDuplicateHandler(innerCb));
if (endpoint.includes('configGroups')) {
config
.getGroupRegistry()
.findConfigGroups(group.service, group.subservice, generateDuplicateHandler(innerCb));
} else {
config.getGroupRegistry().find(group.service, group.subservice, generateDuplicateHandler(innerCb));
}
}

function checkMandatoryParams(innerCb) {
Expand Down Expand Up @@ -105,12 +111,22 @@ function createGroup(groupSet, callback) {
const insertions = [];
const insertedGroups = [];

logger.debug(context, 'Creating new set of %d services', groupSet.services.length);
if (groupSet.configGroups) {
logger.debug(context, 'Creating new set of %d configGroups', groupSet.configGroups.length);

for (let i = 0; i < groupSet.configGroups.length; i++) {
insertions.push(async.apply(validateGroup, 'configGroups', groupSet.configGroups[i]));
insertions.push(async.apply(config.getGroupRegistry().create, groupSet.configGroups[i]));
insertedGroups.push(groupSet.configGroups[i]);
}
} else {
logger.debug(context, 'Creating new set of %d services', groupSet.services.length);

for (let i = 0; i < groupSet.services.length; i++) {
insertions.push(async.apply(validateGroup, groupSet.services[i]));
insertions.push(async.apply(config.getGroupRegistry().create, groupSet.services[i]));
insertedGroups.push(groupSet.services[i]);
for (let i = 0; i < groupSet.services.length; i++) {
insertions.push(async.apply(validateGroup, 'services', groupSet.services[i]));
insertions.push(async.apply(config.getGroupRegistry().create, groupSet.services[i]));
insertedGroups.push(groupSet.services[i]);
}
}

insertions.push(iotManagerService.register);
Expand All @@ -125,12 +141,16 @@ function createGroup(groupSet, callback) {
* @param {Number} limit Maximum number of entries to return.
* @param {Number} offset Number of entries to skip for pagination.
*/
function listGroups(service, limit, offset, callback) {
function listGroups(endpoint, service, limit, offset, callback) {
if (!callback) {
callback = service;
}

config.getGroupRegistry().list(service, limit, offset, callback);
if (endpoint.includes('configGroups')) {
config.getGroupRegistry().listConfigGroups(service, limit, offset, callback);
} else {
config.getGroupRegistry().list(service, limit, offset, callback);
}
}

function checkServiceIdentity(service, subservice, deviceGroup, callback) {
Expand Down Expand Up @@ -242,9 +262,11 @@ function update(service, subservice, resource, apikey, body, callback) {
* @param {String} subservice Group subservice name.
* @param {String} type Group type (optional).
*/
function find(service, subservice, type, callback) {
function find(endpoint, service, subservice, type, callback) {
if (type) {
config.getGroupRegistry().findType(service, subservice, type, callback);
} else if (endpoint.includes('configGroups')) {
config.getGroupRegistry().findConfigGroups(service, subservice, callback);
} else {
config.getGroupRegistry().find(service, subservice, callback);
}
Expand Down Expand Up @@ -307,7 +329,7 @@ function getEffectiveApiKey(service, subservice, type, callback) {
}
}

find(service, subservice, type, handleFindGroup);
find('configGroups', service, subservice, type, handleFindGroup);
}

exports.create = intoTrans(context, createGroup);
Expand Down
Loading