Skip to content

Commit 825aea0

Browse files
committed
Cherry pick Static attributes not shown in groups
1 parent 22aaaaf commit 825aea0

File tree

8 files changed

+139
-26
lines changed

8 files changed

+139
-26
lines changed

CHANGES_NEXT_RELEASE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
- Expose the singleConfigurationMode attribute using environment variables (#404)
33
- FIX IOTAM Registrations won't send static attributes in group information #430
44
- FIX Update device operation ignores static attributes (#137)
5+
- Static attributes not shown in device group updates (#423)

lib/services/groups/groupRegistryMongoDB.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ function listGroups(service, limit, offset, callback) {
105105
var condition = {},
106106
query;
107107

108+
function toObjectFn(obj) {
109+
return obj.toObject();
110+
}
111+
108112
if (service) {
109113
condition.service = service;
110114
}
@@ -125,7 +129,7 @@ function listGroups(service, limit, offset, callback) {
125129
], function(error, results) {
126130
callback(error, {
127131
count: results[1],
128-
services: results[0]
132+
services: results[0].map(toObjectFn)
129133
});
130134
});
131135
}
@@ -179,7 +183,7 @@ function findBy(fields) {
179183

180184
callback(new errors.InternalDbError(error));
181185
} else if (data) {
182-
callback(null, data);
186+
callback(null, data.toObject());
183187
} else {
184188
logger.debug('Device group for fields [%j] not found: [%j]', fields, queryObj);
185189

@@ -190,6 +194,7 @@ function findBy(fields) {
190194
}
191195

192196
function update(id, body, callback) {
197+
logger.debug("Storing updated values for configuration [%s]:\n%s", id, JSON.stringify(body, null, 4));
193198
getById(id, function(error, group) {
194199
if (error) {
195200
callback(error);

lib/services/northBound/deviceGroupAdministrationServer.js

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,38 @@ var restUtils = require('./restUtils'),
2828
apply = async.apply,
2929
templateGroup = require('../../templates/deviceGroup.json'),
3030
configurationHandler,
31+
_ = require('underscore'),
3132
mandatoryHeaders = [
3233
'fiware-service',
3334
'fiware-servicepath'
3435
],
3536
mandatoryParameters = [
3637
'resource',
3738
'apikey'
38-
];
39+
],
40+
apiToInternal = {
41+
'entity_type': 'type',
42+
'internal_attributes': 'internalAttributes',
43+
'static_attributes': 'staticAttributes'
44+
},
45+
internalToApi = {
46+
'type': 'entity_type',
47+
'internalAttributes': 'internal_attributes',
48+
'staticAttributes': 'static_attributes'
49+
};
50+
51+
function applyMap(translation, body) {
52+
var newBody = _.clone(body);
53+
54+
for (var i in newBody) {
55+
if (newBody.hasOwnProperty(i) && translation[i]) {
56+
newBody[translation[i]] = newBody[i];
57+
delete newBody[i];
58+
}
59+
}
60+
61+
return newBody;
62+
}
3963

4064
/**
4165
* Apply the handler for configuration updates if there is any.
@@ -65,13 +89,9 @@ function handleCreateDeviceGroup(req, res, next) {
6589
/*jshint sub:true */
6690

6791
for (var i = 0; i < req.body.services.length; i++) {
92+
req.body.services[i] = applyMap(apiToInternal, req.body.services[i]);
6893
req.body.services[i].service = req.headers['fiware-service'];
6994
req.body.services[i].subservice = req.headers['fiware-servicepath'];
70-
req.body.services[i].type = req.body.services[i]['entity_type'];
71-
req.body.services[i].internalAttributes = req.body.services[i]['internal_attributes'];
72-
73-
delete req.body.services[i]['entity_type'];
74-
delete req.body.services[i]['internal_attributes'];
7595
}
7696

7797
async.series([
@@ -96,26 +116,30 @@ function handleCreateDeviceGroup(req, res, next) {
96116
* @param {Function} next Invokes the next middleware in the chain.
97117
*/
98118
function handleListDeviceGroups(req, res, next) {
119+
var listHandler = function(error, group) {
120+
if (error) {
121+
next(error);
122+
} else {
123+
var translatedGroup = _.clone(group);
124+
125+
if (group.services) {
126+
translatedGroup.services = group.services.map(applyMap.bind(null, internalToApi));
127+
} else {
128+
translatedGroup = applyMap(internalToApi, group);
129+
}
130+
131+
res.status(200).send(translatedGroup);
132+
}
133+
};
134+
99135
if (req.headers['fiware-servicepath'] === '/*') {
100136
groupService.list(
101137
req.headers['fiware-service'],
102138
req.query.limit,
103139
req.query.offset,
104-
function(error, groupList) {
105-
if (error) {
106-
next(error);
107-
} else {
108-
res.status(200).send(groupList);
109-
}
110-
});
140+
listHandler);
111141
} else {
112-
groupService.find(req.headers['fiware-service'], req.headers['fiware-servicepath'], function(error, group) {
113-
if (error) {
114-
next(error);
115-
} else {
116-
res.status(200).send(group);
117-
}
118-
});
142+
groupService.find(req.headers['fiware-service'], req.headers['fiware-servicepath'], listHandler);
119143
}
120144
}
121145

@@ -128,7 +152,7 @@ function handleListDeviceGroups(req, res, next) {
128152
*/
129153
function handleModifyDeviceGroups(req, res, next) {
130154
/*jshint sub:true */
131-
req.body.type = req.body['entity_type'];
155+
req.body = applyMap(apiToInternal, req.body);
132156

133157
async.series([
134158
apply(groupService.update, req.headers['fiware-service'], req.headers['fiware-servicepath'],
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"contextElements": [
3+
{
4+
"type": "TheLightType",
5+
"isPattern": "false",
6+
"id": "TheFirstLight",
7+
"attributes": [
8+
{
9+
"name": "attr_name",
10+
"type": "string",
11+
"value": " "
12+
},
13+
{
14+
"name": "status",
15+
"type": "Boolean",
16+
"value": " "
17+
},
18+
{
19+
"name": "hardcodedAttr",
20+
"type": "hardcodedType",
21+
"value": "hardcodedValue"
22+
},
23+
{
24+
"name": "bootstrapServer",
25+
"type": "Address",
26+
"value": "127.0.0.1"
27+
},
28+
{
29+
"name": "commandAttr_status",
30+
"type": "commandStatus",
31+
"value": "UNKNOWN"
32+
},
33+
{
34+
"name": "commandAttr_info",
35+
"type": "commandResult",
36+
"value": " "
37+
},
38+
{
39+
"name": "wheel1_status",
40+
"type": "commandStatus",
41+
"value": "UNKNOWN"
42+
},
43+
{
44+
"name": "wheel1_info",
45+
"type": "commandResult",
46+
"value": " "
47+
}
48+
]
49+
}
50+
],
51+
"updateAction": "APPEND"
52+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"contextElements": [
3+
{
4+
"type": "SensorMachine",
5+
"isPattern": "false",
6+
"id": "machine1",
7+
"attributes": [
8+
{
9+
"name": "status",
10+
"type": "String",
11+
"value": "STARTING"
12+
},
13+
{
14+
"name": "bootstrapServer",
15+
"type": "Address",
16+
"value": "127.0.0.1"
17+
}
18+
]
19+
}
20+
],
21+
"updateAction": "UPDATE"
22+
}

test/unit/mongodb/mongodb-group-registry-test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ var iotAgentLib = require('../../../lib/fiware-iotagent-lib'),
146146
name: 'status',
147147
type: 'Boolean'
148148
}
149+
],
150+
static_attributes: [
151+
{
152+
name: 'bootstrapServer',
153+
type: 'Address',
154+
value: '127.0.0.1'
155+
}
149156
]
150157
},
151158
headers: {
@@ -274,6 +281,8 @@ describe('MongoDB Group Registry test', function() {
274281
should.exist(docs);
275282
should.exist(docs[0].cbHost);
276283
docs[0].cbHost.should.equal('http://anotherUnexistentHost:1026');
284+
should.exist(docs[0].staticAttributes);
285+
docs[0].staticAttributes.length.should.equal(1);
277286
done();
278287
});
279288
});
@@ -333,7 +342,7 @@ describe('MongoDB Group Registry test', function() {
333342
});
334343
});
335344

336-
describe('When a device group listing request arrives with limit', function() {
345+
describe('When a device group listing arrives with a limit', function() {
337346
var optionsConstrained = {
338347
url: 'http://localhost:4041/iot/services',
339348
method: 'GET',

test/unit/provisioning/device-group-api-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ describe('Device Group Configuration API', function() {
693693
.matchHeader('fiware-service', 'TestService')
694694
.matchHeader('fiware-servicepath', '/testingPath')
695695
.post('/v1/updateContext',
696-
utils.readExampleFile('./test/unit/examples/contextRequests/updateContext3.json'))
696+
utils.readExampleFile('./test/unit/examples/contextRequests/updateContext3WithStatic.json'))
697697
.reply(200,
698698
utils.readExampleFile('./test/unit/examples/contextResponses/updateContext1Success.json'));
699699

test/unit/provisioning/singleConfigurationMode-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ describe('Provisioning API: Single service mode', function() {
288288
.matchHeader('fiware-service', 'TestService')
289289
.matchHeader('fiware-servicepath', '/testingPath')
290290
.post('/v1/updateContext', utils.readExampleFile(
291-
'./test/unit/examples/contextRequests/createProvisionedDeviceWithGroup.json'))
291+
'./test/unit/examples/contextRequests/createProvisionedDeviceWithGroupAndStatic.json'))
292292
.reply(200, utils.readExampleFile(
293293
'./test/unit/examples/contextResponses/createProvisionedDeviceSuccess.json'));
294294

0 commit comments

Comments
 (0)