Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
oisp-mqtt-gw: removed postgres Database functions and running on node…
Browse files Browse the repository at this point in the history
…16 as oer DPT version
  • Loading branch information
yshashix committed Oct 13, 2023
1 parent 26baf38 commit db2a001
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 246 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:14-alpine
FROM node:16-alpine

ADD / /app
RUN apk update && apk upgrade && \
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ The Bridge also handles the auth, acl and load-balancing for MQTT and MQTT with

``` bash
"sparkplug": {
"spBKafkaProduce": true,
"spBKafkaProduce": false,
"spBkafkaTopic": "sparkplugB",
"ngsildKafkaProduce": true,
"ngsildKafkaTopic": "ngsildSpB"
Expand All @@ -86,7 +86,6 @@ The Bridge also handles the auth, acl and load-balancing for MQTT and MQTT with
| true | true | SpB NGSI-LD format metric on "ngsildSpB" topic; Rest on "sparkplugB" |
| true | false | All message on "sparkplugB" |
| false | true | SpB NGSI-LD format metric on "ngsildSpB" topic; Rest ignored |
| false | false | All message on "metric" |


2. NGSI-LD message format converted from received SpB message format
Expand Down
10 changes: 2 additions & 8 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ var config = {
"linger": parsedConfig.kafkaConfig.linger,
"partitioner": parsedConfig.kafkaConfig.partitioner
},
"postgres": {
"host": parsedConfig.postgresConfig.hostname,
"dbname": parsedConfig.postgresConfig.dbname,
"port": parsedConfig.postgresConfig.port,
"username": parsedConfig.postgresConfig.username,
"password": parsedConfig.postgresConfig.password
},
"topics": {
"prefix": parsedConfig.topicsPrefix || "server",
"publish": {
Expand All @@ -118,7 +111,8 @@ var config = {
"sparkplugb_data_ingestion": "spBv1.0/+/+/+/+"
},
"publish": {
"error": "server/error/{accountId}/{deviceId}"
"error": "server/error/{accountId}/{deviceId}",
"actuation": "/{realmId}/DCMD/{gatewayId}/{deviceId}"
}
}
},
Expand Down
73 changes: 2 additions & 71 deletions lib/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
"use strict";

const redis = require("redis");
const { Sequelize } = require('sequelize');
const { QueryTypes } = require('sequelize');
var uuidValidate = require('uuid-validate');

var me;

class CacheFactory {
Expand All @@ -33,52 +29,21 @@ class CacheFactory {
getInstance() {
return CacheFactory.instance;
}

}

class Cache {

constructor(conf, log)
{
this.redisClient = redis.createClient({port: conf.cache.port, host: conf.cache.host});
if (process.env.PGSSLMODE === "require") {
this.sequelize = new Sequelize(conf.postgres.dbname, conf.postgres.username, conf.postgres.password, {
host: conf.postgres.host,
port: conf.postgres.port,
dialect: 'postgres',
dialectOptions: {
ssl: {
rejectUnauthorized: false
}
},
});
} else {
this.sequelize = new Sequelize(conf.postgres.dbname, conf.postgres.username, conf.postgres.password, {
host: conf.postgres.host,
port: conf.postgres.port,
dialect: 'postgres'
});
}

this.sequelize.authenticate()
.then(() => {
console.log('DB connection has been established.');
})
.catch(error => {
console.error('Unable to connect to DB:', error);
});

this.config = conf;
this.config = conf;
this.logger = log;
me = this;
this.redisClient.on("error", function(err) {
me.logger.info("Error in Redis client: " + err);
});
}
async initialize() {


}

async setValue(key, valueType, value) {
return new Promise((resolve, reject) => {
this.redisClient.hmset(key, valueType, value, (err, result) => {
Expand Down Expand Up @@ -117,39 +82,5 @@ class Cache {
});
});
}
async getDidAndDataType(item) {
var cid = item.componentId;
//check whether cid = uuid
if (!uuidValidate(cid)) {
throw new Error("cid not UUID. Rejected!");
}

var value = await this.getValues(cid);
var didAndDataType;
if (value === null || (Array.isArray(value) && value.length === 1 && value[0] === null)) {
me.logger.debug("Could not find " + cid + "in cache. Now trying sql query.");
// no cached value found => make db lookup and store in cache
var sqlquery='SELECT devices.id,"dataType" FROM dashboard.device_components,dashboard.devices,dashboard.component_types WHERE "componentId"::text=\'' + cid + '\' and "deviceUID"::text=devices.uid::text and device_components."componentTypeId"::text=component_types.id::text';
me.logger.debug("Applying SQL query: " + sqlquery);
didAndDataType = await this.sequelize.query(sqlquery, { type: QueryTypes.SELECT });
me.logger.debug("Result of sql query: " + JSON.stringify(didAndDataType));
} else {
me.logger.debug("Found in cache: " + JSON.stringify(value));
didAndDataType = [value];
}

if (didAndDataType === undefined || didAndDataType === null) {
throw new Error("DB lookup failed!");
}
var redisResult = await this.setValue(cid, "id", didAndDataType[0].id) && this.setValue(cid, "dataType", didAndDataType[0].dataType);
didAndDataType[0].dataElement = item;
if (redisResult) {
return didAndDataType[0];
} else {
me.logger.warn("Could not store db value in redis. This will significantly reduce performance");
return didAndDataType[0];
}
}

}
module.exports = CacheFactory;
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "iotkit-gateway",
"version": "0.12.1",
"date": "2015-01-26T13:46:41.332Z",
"date": "2023-10-09T13:46:41.332Z",
"description": "Authotization gateway and kafka bridge for MQTTGW",
"main": "app.js",
"dependencies": {
Expand All @@ -24,10 +24,6 @@
"test": "node gateway-agent",
"start": "node gateway-agent"
},
"repository": {
"type": "git",
"url": "git://github.com/enableiot/iotkit-analytics.git"
},
"keywords": [
"iotkit",
"authorization"
Expand All @@ -40,7 +36,7 @@
"homepage": "https://github.com/enableiot/iotkit-analytics",
"devDependencies": {
"asserts": "^4.0.0",
"chai": "^4.2.0",
"chai": "^4.3.6",
"grunt": "^1.3.0",
"grunt-cli": "^1.3.2",
"grunt-contrib-jshint": "^3.0.0",
Expand Down
22 changes: 0 additions & 22 deletions test/unit/api_actuationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ describe(fileToTest, function() {
"authServicePort": "2345", \
"redisConf": "@@OISP_REDIS_CONFIG", \
"kafkaConfig": "@@OISP_KAFKA_CONFIG", \
"postgresConfig": "@@OISP_POSTGRES_CONFIG", \
"keycloakConfig": "@@OISP_KEYCLOAK_CONFIG", \
"aesKey": "/app/keys/mqtt/mqtt_gw_secret.key" \
}';
Expand All @@ -61,17 +60,6 @@ describe(fileToTest, function() {
"password": "password" \
}';

process.env.OISP_POSTGRES_CONFIG = '{\
"dbname": "oisp",\
"hostname": "postgres-ro",\
"writeHostname": "postgres",\
"port": "5432",\
"su_username": "su_username",\
"su_password": "su_password",\
"username": "username",\
"password": "password"\
}';

var config = {
"mqttBrokerUrl": "brokerUrl",
"mqttBrokerLocalPort": "1234",
Expand Down Expand Up @@ -105,16 +93,6 @@ describe(fileToTest, function() {
"requestTimeout": 4,
"maxRetryTime": 10
},
"postgres": {
"dbname": "oisp",
"hostname": "postgres-ro",
"writeHostname": "postgres",
"port": "5432",
"su_username": "su_username",
"su_password": "su_password",
"username": "username",
"password": "password"
},
"aesKey": "/app/keys/mqtt/mqtt_gw_secret.key"
};
var ToTest = rewire(fileToTest);
Expand Down
32 changes: 0 additions & 32 deletions test/unit/api_spb_data_ingestionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ describe(fileToTest, function() {
"authServicePort": "2345", \
"redisConf": "@@OISP_REDIS_CONFIG", \
"kafkaConfig": "@@OISP_KAFKA_CONFIG", \
"postgresConfig": "@@OISP_POSTGRES_CONFIG", \
"keycloakConfig": "@@OISP_KEYCLOAK_CONFIG", \
"aesKey": "/app/keys/mqtt/mqtt_gw_secret.key" \
}';
Expand All @@ -58,17 +57,6 @@ describe(fileToTest, function() {
"password": "password" \
}';

process.env.OISP_POSTGRES_CONFIG = '{\
"dbname": "oisp",\
"hostname": "postgres-ro",\
"writeHostname": "postgres",\
"port": "5432",\
"su_username": "su_username",\
"su_password": "su_password",\
"username": "username",\
"password": "password"\
}';

var config = {
"mqttBrokerUrl": "brokerUrl",
"mqttBrokerLocalPort": "1234",
Expand Down Expand Up @@ -109,16 +97,6 @@ describe(fileToTest, function() {
"requestTimeout": 4,
"maxRetryTime": 10
},
"postgres": {
"dbname": "oisp",
"hostname": "postgres-ro",
"writeHostname": "postgres",
"port": "5432",
"su_username": "su_username",
"su_password": "su_password",
"username": "username",
"password": "password"
},
"aesKey": "/app/keys/mqtt/mqtt_gw_secret.key"
};

Expand Down Expand Up @@ -162,16 +140,6 @@ describe(fileToTest, function() {
"requestTimeout": 4,
"maxRetryTime": 10
},
"postgres": {
"dbname": "oisp",
"hostname": "postgres-ro",
"writeHostname": "postgres",
"port": "5432",
"su_username": "su_username",
"su_password": "su_password",
"username": "username",
"password": "password"
},
"aesKey": "/app/keys/mqtt/mqtt_gw_secret.key"
};

Expand Down
22 changes: 0 additions & 22 deletions test/unit/config_Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ describe(fileToTest, function() {
"authServicePort": "2345", \
"redisConf": "@@OISP_REDIS_CONFIG", \
"kafkaConfig": "@@OISP_KAFKA_CONFIG", \
"postgresConfig": "@@OISP_POSTGRES_CONFIG", \
"keycloakConfig": "@@OISP_KEYCLOAK_CONFIG", \
"aesKey": "/app/keys/mqtt/mqtt_gw_secret.key" \
}';
Expand All @@ -58,17 +57,6 @@ describe(fileToTest, function() {
"password": "password" \
}';

process.env.OISP_POSTGRES_CONFIG = '{\
"dbname": "oisp",\
"hostname": "postgres-ro",\
"writeHostname": "postgres",\
"port": "5432",\
"su_username": "su_username",\
"su_password": "su_password",\
"username": "username",\
"password": "password"\
}';

var parseResult = {
"mqttBrokerUrl": "brokerUrl",
"mqttBrokerLocalPort": "1234",
Expand All @@ -95,16 +83,6 @@ describe(fileToTest, function() {
"requestTimeout": 4,
"maxRetryTime": 10
},
"postgresConfig": {
"dbname": "oisp",
"hostname": "postgres-ro",
"writeHostname": "postgres",
"port": "5432",
"su_username": "su_username",
"su_password": "su_password",
"username": "username",
"password": "password"
},
"keycloakConfig": {
"auth-server-url": "keycloak",
"listenerPort": 4080
Expand Down
Loading

0 comments on commit db2a001

Please sign in to comment.