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 #1375

Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add /iot/configGroups API endpoints (as equivalent to /iot/services) (#752)
80 changes: 73 additions & 7 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ Config group is represented by a JSON object with the following fields:

The following actions are available under the config group endpoint:

#### Retrieve config groups `GET /iot/services`
#### Retrieve config groups GET `/iot/configGroups` or `GET /iot/services`

List all the config groups for the given `fiware-service` and `fiware-servicepath`. If the `fiware-servicepath` header
has the wildcard expression, `/*`, all the config groups for that `fiware-service` are returned. The config groups that
Expand All @@ -1197,11 +1197,50 @@ Successful operations return `Content-Type` header with `application/json` value

_**Response payload**_

A JSON object with a services field that contains an array of services that match the request. See the
A JSON object with a configGroups or services field that contains an array of services that match the request. See the
[config group datamodel](#service-group-datamodel) for more information.

Example:

```json
{
"configGroups": [
{
"resource": "/deviceTest",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"type": "Light",
"trust": "8970A9078A803H3BL98PINEQRW8342HBAMS",
"cbHost": "http://orion:1026",
"commands": [{ "name": "wheel1", "type": "Wheel" }],
"attributes": [
{
"name": "luminescence",
"type": "Integer",
"metadata": {
"unitCode": { "type": "Text", "value": "CAL" }
}
}
],
"lazy": [{ "name": "status", "type": "Boolean" }]
},
{
"resource": "/deviceTest2",
"apikey": "A21323ASDG12312ASDN21LWQEJPO2J123",
"type": "Switch",
"trust": "8970A9078A803H3BL98PINEQRW8342HBAMS",
"cbHost": "http://orion:1026",
"commands": [{ "name": "on", "type": "order" }],
"attributes": [
{
"name": "swithc_status",
"type": "boolean"
}
]
}
]
}
```

Copy link
Collaborator

Choose a reason for hiding this comment

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

This should replace the existing example, not adding a new one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 1178705

```json
{
"services": [
Expand Down Expand Up @@ -1241,7 +1280,7 @@ Example:
}
```

#### Create config group `POST /iot/services`
#### Create config group `POST /iot/configGroups` or `POST /iot/services`

Creates a set of config groups for the given service and service path. The service and subservice information will taken
from the headers, overwritting any preexisting values.
Expand All @@ -1255,11 +1294,38 @@ _**Request headers**_

_**Request payload**_

A JSON object with a `services` field. The value is an array of config groups objects to create. See the
A JSON object with a `configGroups` or `services` field. The value is an array of config groups objects to create. See the
[config group datamodel](#service-group-datamodel) for more information.

Example:


```json
{
"configGroups": [
{
"resource": "/deviceTest",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"type": "Light",
"trust": "8970A9078A803H3BL98PINEQRW8342HBAMS",
"cbHost": "http://orion:1026",
"commands": [{ "name": "wheel1", "type": "Wheel" }],
"attributes": [
{
"name": "luminescence",
"type": "Integer",
"metadata": {
"unitCode": { "type": "Text", "value": "CAL" }
}
}
],
"lazy": [{ "name": "status", "type": "Boolean" }]
}
]
}
```
OR

Copy link
Collaborator

Choose a reason for hiding this comment

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

This should replace the existing example, not adding a new one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 1178705

```json
{
"services": [
Expand Down Expand Up @@ -1296,9 +1362,9 @@ _**Response headers**_

Successful operations return `Content-Type` header with `application/json` value.

#### Modify config group `PUT /iot/services`
#### Modify config group `PUT /iot/configGroups` or `PUT /iot/services`

Modifies the information of a config group, identified by the `resource` and `apikey` query parameters. Takes a service
Modifies the information of a config group, identified by the `resource` and `apikey` query parameters. Takes a configuration/service
group body as the payload. The body does not have to be complete: for incomplete bodies, just the attributes included in
the JSON body will be updated. The rest of the attributes will remain unchanged.

Expand Down Expand Up @@ -1336,7 +1402,7 @@ _**Response code**_
- 400 MISSING_HEADERS if any of the mandatory headers is not present.
- 500 SERVER ERROR if there was any error not contemplated above.:

#### Remove config group `DELETE /iot/services`
#### Remove config group `DELETE /iot/configGroups` or `DELETE /iot/services`

Removes a config group, identified by the `resource` and `apikey` query parameters.

Expand Down
2 changes: 2 additions & 0 deletions lib/fiware-iotagent-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ exports.setCommandHandler = contextServer.setCommandHandler;
exports.setMergePatchHandler = contextServer.setMergePatchHandler;
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;
Expand Down
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
});
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is duplicated, same as function listGroups(service, limit, offset, callback) . No needed to duplicate code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 1178705

/**
* 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));
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is duplicated, same as function find(service, subservice, callback). No needed to duplicate code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 1178705

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 @@ -119,6 +119,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)
});
});
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Duplicated code, same as function listGroups(service, limit, offset, callback). It is not required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 1178705

/**
* List all the groups created in the IoT Agent.
*
Expand Down Expand Up @@ -182,6 +221,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)
});
});
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Duplicated code, same as function find(service, subservice, callback) . No required

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 1178705

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

exports.create = alarmsInt(constants.MONGO_ALARM + '_01', intoTrans(context, createGroup));
exports.list = alarmsInt(constants.MONGO_ALARM + '_02', intoTrans(context, listGroups));
exports.listConfigGroups = alarmsInt(constants.MONGO_ALARM + '_12', intoTrans(context, listConfigGroups));
exports.init = alarmsInt(constants.MONGO_ALARM + '_03', intoTrans(context, init));
exports.find = alarmsInt(constants.MONGO_ALARM + '_04', intoTrans(context, find));
exports.findConfigGroups = alarmsInt(constants.MONGO_ALARM + '_13', intoTrans(context, findConfigGroups));
exports.findType = alarmsInt(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Exporting duplicated functions, not required

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 1178705

constants.MONGO_ALARM + '_05',
intoTrans(context, findBy(['service', 'subservice', 'type', 'apikey']))
Expand Down
Loading