Skip to content

Commit

Permalink
Merge 1208373 into 0970e38
Browse files Browse the repository at this point in the history
  • Loading branch information
pratid committed Jul 3, 2014
2 parents 0970e38 + 1208373 commit 360b180
Show file tree
Hide file tree
Showing 17 changed files with 334 additions and 218 deletions.
12 changes: 7 additions & 5 deletions ngsi_adapter/src/lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ var http = require('http'),
function doPost(request, callback) {
try {
logger.info('<< Body: %s', request.body);
var remoteUrlData = url.parse(opts.brokerUrl);
var updateReqType = request.parser.getRequestContentType();
var updateReqBody = request.parser.updateContextRequest();
var remoteUrl = url.parse(opts.brokerUrl);
var dataParser = request.parser;
var updateReqType = dataParser.getContentType();
var updateReqBody = dataParser.updateContextRequest(request);
var updateReqOpts = {
hostname: remoteUrlData.hostname,
port: remoteUrlData.port,
hostname: remoteUrl.hostname,
port: remoteUrl.port,
path: '/NGSI10/updateContext',
method: 'POST',
headers: {
Expand Down Expand Up @@ -125,6 +126,7 @@ function asyncRequestListener(request, response) {
try {
status = 200; // ok
request.parser = parser.getParser(request);
request.timestamp = Date.now();
request.body = '';
request.on('data', function(chunk) {
request.body += chunk;
Expand Down
6 changes: 3 additions & 3 deletions ngsi_adapter/src/lib/parsers/check_disk.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* Context attributes to be calculated:
*
* - freeSpacePct = percentage of free space at given group/single partition (NO MULTIPLE PARTITIONS SUPPORTED UNLESS GROUPED!)
* - freeSpacePct = percentage of free space at given group/single partition (NO MULTIPLE PARTITIONS SUPPORTED!)
*
* @module check_disk
* @see https://www.nagios-plugins.org/doc/man/check_disk.html
Expand Down Expand Up @@ -86,8 +86,8 @@ var nagios = require('./common/nagios');
// Filesystem #n
//
var parser = Object.create(nagios.parser);
parser.getContextAttrs = function(multilineData, multilinePerfData) {
var data = multilineData.split('\n')[0]; // only consider first line of data, discard perfData
parser.getContextAttrs = function(probeEntityData) {
var data = probeEntityData.data.split('\n')[0]; // only consider first line of probe data, discard perfData
var attrs = { freeSpacePct: NaN };

var items = data.split(':');
Expand Down
4 changes: 2 additions & 2 deletions ngsi_adapter/src/lib/parsers/check_load.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ var nagios = require('./common/nagios');
// reserved -----------------------------------------------------------------------------------------------------+
//
var parser = Object.create(nagios.parser);
parser.getContextAttrs = function(multilineData, multilinePerfData) {
var data = multilineData.split('\n')[0]; // only consider first line of data, discard perfData
parser.getContextAttrs = function(probeEntityData) {
var data = probeEntityData.data.split('\n')[0]; // only consider first line of probe data, discard perfData
var attrs = { cpuLoadPct: NaN };

var items = data.split(':');
Expand Down
4 changes: 2 additions & 2 deletions ngsi_adapter/src/lib/parsers/check_mem.sh.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ var nagios = require('./common/nagios');
// Buffers memory (bytes) --------------------------------------------------------------------------------------------------------------+
//
var parser = Object.create(nagios.parser);
parser.getContextAttrs = function(multilineData, multilinePerfData) {
var data = multilineData.split('\n')[0]; // only consider first line of data, discard perfData
parser.getContextAttrs = function(probeEntityData) {
var data = probeEntityData.data.split('\n')[0]; // only consider first line of probe data, discard perfData
var attrs = { usedMemPct: NaN };

var items = data.split('-');
Expand Down
4 changes: 2 additions & 2 deletions ngsi_adapter/src/lib/parsers/check_procs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ var nagios = require('./common/nagios');
// # of procs ----------+
//
var parser = Object.create(nagios.parser);
parser.getContextAttrs = function(multilineData, multilinePerfData) {
var data = multilineData.split('\n')[0]; // only consider first line of data, discard perfData
parser.getContextAttrs = function(probeEntityData) {
var data = probeEntityData.data.split('\n')[0]; // only consider first line of probe data, discard perfData
var attrs = { procs: NaN };

var items = data.split(':');
Expand Down
4 changes: 2 additions & 2 deletions ngsi_adapter/src/lib/parsers/check_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ var nagios = require('./common/nagios');
// Reserved -------------------------------------------------------+
//
var parser = Object.create(nagios.parser);
parser.getContextAttrs = function(multilineData, multilinePerfData) {
var data = multilineData.split('\n')[0]; // only consider first line of data, discard perfData
parser.getContextAttrs = function(probeEntityData) {
var data = probeEntityData.data.split('\n')[0]; // only consider first line of probe data, discard perfData
var attrs = { users: NaN };

var items = data.split('-');
Expand Down
44 changes: 23 additions & 21 deletions ngsi_adapter/src/lib/parsers/common/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,22 @@ var baseParser = Object.create(null);


/**
* Sets the HTTP request the parser will process.
* Name of the timestamp attribute added automatically to the resulting entity context attributes.
*
* @function setRequest
* @constant {String}
* @memberof baseParser
* @param {http.IncomingMessage} request The request to this server.
*/
baseParser.setRequest = function(request) {
this.request = request;
};
baseParser.timestampAttrName = '_timestamp';


/**
* Gets the content type for Context Broker requests.
*
* @function getRequestContentType
* @function getContentType
* @memberof baseParser
* @returns {String} The content type (the format) for Context Broker requests.
*/
baseParser.getRequestContentType = function() {
baseParser.getContentType = function() {
return 'application/xml';
};

Expand All @@ -65,21 +62,26 @@ baseParser.getRequestContentType = function() {
*
* @function updateContextRequest
* @memberof baseParser
* @param {http.IncomingMessage} request The HTTP request to this server.
* @returns {String} The request body, either in XML or JSON format.
*/
baseParser.updateContextRequest = function() {
var query = url.parse(this.request.url, true).query;
baseParser.updateContextRequest = function(request) {
var query = url.parse(request.url, true).query;
var entityId = query.id;
var entityType = query.type;
if (!entityId || !entityType) {
throw new Error('Missing entityId and/or entityType');
} else {
var entityData = this.parseRequest();
var entityAttrs = this.getContextAttrs(entityData.data, entityData.perfData);
return (this.getRequestContentType() === 'application/xml')
? this.getUpdateContextXML(entityId, entityType, entityAttrs)
: this.getUpdateContextJSON(entityId, entityType, entityAttrs);
}
var entityData = this.parseRequest(request);
var entityAttrs = this.getContextAttrs(entityData);
if (Object.keys(entityAttrs).length === 0) {
throw new Error('Missing entity context attributes');
}
// feature #4: automatically add request timestamp to entity attributes
entityAttrs[this.timestampAttrName] = request.timestamp;
return (this.getContentType() === 'application/xml')
? this.getUpdateContextXML(entityId, entityType, entityAttrs)
: this.getUpdateContextJSON(entityId, entityType, entityAttrs);
};


Expand All @@ -89,9 +91,10 @@ baseParser.updateContextRequest = function() {
* @abstract
* @function paserRequest
* @memberof baseParser
* @returns {EntityData} An object with `data` (and optional `perfData`) members.
* @param {http.IncomingMessage} request The HTTP request to this server.
* @returns {EntityData} An object holding entity data taken from request body.
*/
baseParser.parseRequest = function() {
baseParser.parseRequest = function(request) {
throw new Error('Must implement');
};

Expand All @@ -102,11 +105,10 @@ baseParser.parseRequest = function() {
* @abstract
* @function getContextAttrs
* @memberof baseParser
* @param {Object} data The probe data included in input request.
* @param {Object} [optionalPerfData] The perfomance/extra data in input request.
* @param {EntityData} data Object holding raw entity data.
* @returns {Object} Context attributes.
*/
baseParser.getContextAttrs = function(data, optionalPerfData) {
baseParser.getContextAttrs = function(data) {
throw new Error('Must implement');
};

Expand Down
1 change: 0 additions & 1 deletion ngsi_adapter/src/lib/parsers/common/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function getParser(request) {
var probeName = url.parse(request.url).pathname.slice(1);
try {
var parser = require(util.format('../%s', probeName)).parser;
parser.setRequest(request);
return parser;
} catch (err) {
logger.debug(err.message);
Expand Down
5 changes: 3 additions & 2 deletions ngsi_adapter/src/lib/parsers/common/nagios.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ var nagiosParser = Object.create(baseParser);
*
* @function paserRequest
* @memberof nagiosParser
* @param {http.IncomingMessage} request The HTTP request to this server.
* @returns {EntityData} An object with `data` (and optional `perfData`) members.
*/
nagiosParser.parseRequest = function() {
nagiosParser.parseRequest = function(request) {
var entityData = {};
var lines = this.request.body.split('\n');
var lines = request.body.split('\n');
var isMultilinePerf = false;
lines.forEach(function(item) {
var isFirst = !entityData.data;
Expand Down
5 changes: 4 additions & 1 deletion ngsi_adapter/src/test/unit/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@


var assert = require('assert'),
xml2js = require('xml2js');
xml2js = require('xml2js'),
timestamp = require('../../lib/parsers/common/base').parser.timestampAttrName;


function assertIsNumber(value) {
Expand All @@ -40,6 +41,8 @@ function assertValidUpdateXML(updateXML, test) {
assert.ok(updateXML);
assert.ok(test.entityType);
assert.ok(test.entityData);
// feature #4: automatically add request timestamp to entity attributes
assert.ok(test.entityData[timestamp]);
assertIsNumber(test.entityId);
var entityAttrList = Object.keys(test.entityData);
xml2js.parseString(updateXML, function(err, result) {
Expand Down
135 changes: 135 additions & 0 deletions ngsi_adapter/src/test/unit/test_base_parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright 2013 Telefónica I+D
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/


/**
* Module that defines unit tests for the base parser.
*
* @module test_base_parser
*/


'use strict';


var sinon = require('sinon'),
assert = require('assert'),
common = require('./common'),
parser = require('../../lib/parsers/common/base').parser;


/* jshint multistr: true */
suite('base_parser', function() {

suiteSetup(function() {
this.baseurl = 'http://hostname:1234/path';
this.entityId = '1';
this.entityType = 'host';
this.entityData = {
attr: 4321
};
});

suiteTeardown(function() {
});

setup(function() {
this.request = {
url: this.baseurl + '?id=' + this.entityId + '&type=' + this.entityType,
timestamp: Date.now()
};
this.entityData[parser.timestampAttrName] = this.request.timestamp;
this.parseRequestFunction = parser.parseRequest;
this.getContextAttrsFunction = parser.getContextAttrs;
});

teardown(function() {
delete this.request;
delete this.entityData[parser.timestampAttrName];
parser.parseRequest = this.parseRequestFunction;
parser.getContextAttrs = this.getContextAttrsFunction;
});

test('get_update_request_fails_unimplemented_method_parse_request', function() {
var self = this;
assert.throws(
function() {
return parser.updateContextRequest(self.request);
},
/implement/
);
});

test('get_update_request_fails_unimplemented_method_get_context_attrs', function() {
var self = this;
parser.parseRequest = sinon.spy(function() { return {}; });
assert.throws(
function() {
return parser.updateContextRequest(self.request);
},
/implement/
);
});

test('get_update_request_fails_missing_entity_id', function() {
var self = this;
parser.parseRequest = sinon.spy(function() { return {}; });
parser.getContextAttrs = sinon.spy(function() { return {}; });
self.request.url = self.baseurl + '?type=type&another=another';
assert.throws(
function() {
return parser.updateContextRequest(self.request);
},
/entityId/
);
});

test('get_update_request_fails_missing_entity_type', function() {
var self = this;
parser.parseRequest = sinon.spy(function() { return {}; });
parser.getContextAttrs = sinon.spy(function() { return {}; });
self.request.url = self.baseurl + '?id=id&another=another';
assert.throws(
function() {
return parser.updateContextRequest(self.request);
},
/entityType/
);
});

test('get_update_request_fails_missing_entity_attributes', function() {
var self = this;
parser.parseRequest = sinon.spy(function() { return {}; });
parser.getContextAttrs = sinon.spy(function() { return {}; });
self.request.url = self.baseurl + '?id=id&type=type&another=another';
self.request.body = '';
assert.throws(
function() {
return parser.updateContextRequest(self.request);
}
);
});

test('get_update_request_ok_with_timestamp_added', function() {
var self = this;
parser.parseRequest = sinon.spy(function() { return {}; });
parser.getContextAttrs = sinon.spy(function() { return self.entityData; });
var update = parser.updateContextRequest(self.request);
common.assertValidUpdateXML(update, self);
});

});
Loading

0 comments on commit 360b180

Please sign in to comment.