-
Notifications
You must be signed in to change notification settings - Fork 26
/
boda-cell-file.js
390 lines (324 loc) · 8.81 KB
/
boda-cell-file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
const { Client, Pool } = window.require('pg');
const queryHelper = window.require('./query-helpers');
const fs = require("fs");
const BCF_2G_PARAMS = {
technology: {required: true},
ci: {required: true},
cellname: {},
siteid: {},
carrier_layer: {},
azimuth: {required: true},
electrical_tilt: {},
mechanical_tilt: {},
lac: {},
node: {required: true},
bcch: {},
trx_frequencies: {},
antenna_beam: {},
latitude: {required: true},
longitude: {required: true},
height: {},
vendor: {},
cell_type: {},
bsic: {},
bcc: {},
ncc: {},
mnc: {},
mcc: {},
cgi: {}
};
const BCF_3G_PARAMS = {
technology: {required: true},
ci: {required: true},
cellname: {},
siteid: {},
carrier_layer: {},
azimuth: {required: true},
electrical_tilt: {},
mechanical_tilt: {},
lac: {},
rac: {},
sac: {},
node: {required: true},
psc: {},
uarfcn: {},
antenna_beam: {},
latitude: {required: true},
longitude: {required: true},
height: {},
vendor: {},
cell_type: {},
mnc: {},
mcc: {},
cgi: {},
rncid: {}
};
const BCF_4G_PARAMS = {
technology: {required: true},
ci: {required: true},
cellname: {},
siteid: {},
enodeb_id: {},
carrier_layer: {},
azimuth: {required: true},
electrical_tilt: {},
mechanical_tilt: {},
tac: {},
node: {required: true},
pci: {},
euarfcn: {},
bandwidth: {},
ecgi: {},
mnc: {},
mcc: {},
antenna_beam: {},
latitude: {required: true},
longitude: {required: true},
height: {},
vendor: {},
cell_type: {}
};
const BCF_5G_PARAMS = {
};
const TECHNOLOGIES = ["GSM", "UMTS", "WCDMA", "LTE", "CDMA2000"];
/*
* Return the database cell table given the technology name in lower case
*
* @param string technology
*
* @return string
*/
function getDataCellTable(technology){
switch(technology){
case '2g':
case 'gsm':
return "2g_cells";
case '3g':
case 'umts':
case 'wcdma':
case 'cdma2000':
return "3g_cell"
case '4g':
case 'lte':
return "4g_cell";
case '5g':
case 'nr':
return "5g_cell";
default:
throw Error("Un-recognised technology. Expected gsm, umts, wcdma, lte, nr, 2g, 3g, 4g, or 5g")
}
}
/**
* Get the expected parameter list for a vendor
*
* @param string technology
*
* @return array
*/
function getTechParameterList(technology){
switch(technology){
case '2g':
case 'gsm':
return BCF_2G_PARAMS;
case '3g':
case 'umts':
case 'wcdma':
return BCF_3G_PARAMS;
case '4g':
case 'lte':
return BCF_4G_PARAMS;
case '5g':
case 'nr':
return BCF_5G_PARAMS;
default:
throw Error("Un-recognised technology. Expected gsm, umts, wcdma, lte, nr, 2g, 3g, 4g, or 5g")
}
}
/**
* Generate INSERT queries for data
*
* @param array fields
* @param array values
*
* @return string
*/
function generateParameterInsertQuery(fields, values){
//make the fields lower case
fields = fields.map(v => v.toLowerCase());
//Validate tech
let tech = null;
if(fields.indexOf("technology") === -1) throw Error("Technology field is missing");
tech = values[fields.indexOf("technology")];
if(tech.length === 0) throw Error("Technology value cannot be empty");
//Validate ci
let ci = null;
if(fields.indexOf("ci") === -1) throw Error("ci field is missing");
ci = values[fields.indexOf("ci")];
if(ci.length === 0) throw Error("ci value cannot be empty");
//Get database table for insertion
let tableName = getDataCellTable(tech.toLowerCase());
//Get list of std parameters for technology
const paramNames = Object.keys(getTechParameterList(tech.toLowerCase()));
let paramValues = paramNames.map(v => '');
let insFields = [];
let insValues = [];
let updatePhrase = [];
paramNames.forEach((p, i) => {
//Skip parameter that are not there
if(fields.indexOf(p) === -1 ) return;
paramValues[i] = values[fields.indexOf(p)];
updatePhrase.push(`${p} = EXCLUDED.${p}`);
});
let sql = `INSERT INTO plan_network."${tableName}"
(${paramNames.join(",")})
VALUES
('${paramValues.join("','")}')
ON CONFLICT ON CONSTRAINT unq_ci_node_${tableName} DO UPDATE
SET
${updatePhrase.join(",")}
`;
return sql;
}
/**
* Returns a list of nbrs cells given the field list and value array
*
* @param array fields Array of fields
* @param array values Array of row values
*
* @returns array
*/
function getNbrsFromValues(fields, values){
//Get indices of nbr columns. These are field names that start with
//nbr_ or NBR_
const nbrIndices = fields
.map((v, i) => v.match(/^nbr_/i) ? i : -1 )
.filter((v, i) => v > -1);
let nbrList = [];
nbrIndices.forEach((idxValue) => {
if(values[idxValue].length > 0 ) nbrList.push(values[idxValue]);
});
return nbrList;
}
/*
* Generate nbr insert query
*
* @param array fields Array of fields
* @param array values Array of row values
*
* @returns string SQL query
*/
function generateNbrInsertQuery(fields, values){
//make the fields lower case
fields = fields.map(v => v.toLowerCase());
const nbrList = getNbrsFromValues(fields, values);
//Return null if there are no nbrs
if(nbrList === null) return null;
//Validate ci
let ci = null;
if(fields.indexOf("ci") === -1) throw Error("ci field is missing");
ci = values[fields.indexOf("ci")];
if(ci.length === 0) throw Error("ci value cannot be empty");
let sql = "";
nbrList.forEach(nbr_ci => {
sql += `
INSERT INTO plan_network."relations"
(svr_ci, nbr_ci) VALUES (${ci}, ${nbr_ci})
ON CONFLICT ON CONSTRAINT unq_relations DO NOTHING;`;
});
return sql;
}
async function loadBodaCellFile(inputFile, truncateTables, beforeFileLoad, afterFileLoad, beforeLoad, afterLoad){
//1.Load cells
// -get columns from first row
// -validate that the key parameter columns exist
// -
//2.add nbrs
const dbConDetails = await queryHelper.getSQLiteDBConnectionDetails('boda');
const hostname = dbConDetails.hostname;
const port = dbConDetails.port;
const username = dbConDetails.username;
const password = dbConDetails.password;
const connectionString = `postgresql://${username}:${password}@${hostname}:${port}/boda`;
const pool = new Pool({
connectionString: connectionString,
})
pool.on('error', (err, client) => {
log.error(err.toString());
client.release();
})
if(typeof beforeLoad === 'function'){
beforeLoad();
}
if(truncateTables) {
log.info("Truncate tables before loading is set to true.")
client = await pool.connect();
if(client.processID === null){
log.error('Failed to connect to database');
return {status: "error", message: 'Failed to connect to database during boda cell file loading'};
}
await client.query(`TRUNCATE plan_network."2g_cells" RESTART IDENTITY CASCADE`);
await client.query(`TRUNCATE plan_network."3g_cells" RESTART IDENTITY CASCADE`);
await client.query(`TRUNCATE plan_network."4g_cells" RESTART IDENTITY CASCADE`);
client.release();
}
var fileList = [];
var fileIsDir = false;
if(fs.lstatSync(inputFile).isDirectory()){
fileIsDir = true;
fileList = fs.readdirSync(inputFile, { withFileTypes: true }).filter(dirent => !dirent.isDirectory()).map(dirent => dirent.name);
}else{
fileList = [inputFile]
}
for (let i=0; i< fileList.length; i++) {
let fileName = fileList[i];
let filePath = fileIsDir ? path.join(inputFile, fileList[i]) : fileName;
client = await pool.connect();
if(client.processID === null){
log.error('Failed to connect to database');
return {status: "error", message: 'Failed to connect to database during boda cell file loading'};
}
let parameterList = [];
//This is used to capture load error in onError Event
let loadError = null;
console.log("filePath:", filePath);
await new Promise((resolve, reject) => {
csv({output: "csv", noheader:true, trim:true})
.fromFile(filePath)
.subscribe(async (csvRow, index)=>{
//Header column
if(index === 0){
parameterList = csvRow;
return;
}
//Insert cell parameters
const sql = generateParameterInsertQuery(parameterList, csvRow);
await client.query(sql);
//Insert relations
const nbrSQL = generateNbrInsertQuery(parameterList, csvRow);
if (nbrSQL !== null) await client.query(nbrSQL);
},(err) => {//onError
log.error(`csvJoJson.onError: ${err.toString()}`);
client.release();
loadError = `Error while loading ${fileName}`;
resolve();
},
()=>{//onComplete
log.info(`End of csvToJson for ${fileName}.`)
client.release();
resolve();
});
});//eof: promise
//Return error status if loadError is not null
if(loadError !== null) throw new Error(loadError, 'boda-cell-file.js');
}
if(typeof afterLoad === 'function'){
afterLoad();
}
}
exports.BCF_2G_PARAMS = BCF_2G_PARAMS;
exports.BCF_3G_PARAMS = BCF_3G_PARAMS;
exports.BCF_4G_PARAMS = BCF_4G_PARAMS;
exports.generateParameterInsertQuery = generateParameterInsertQuery;
exports.generateNbrInsertQuery = generateNbrInsertQuery;
exports.loadBodaCellFile = loadBodaCellFile;
exports.getTechParameterList = getTechParameterList ;