Last active
October 3, 2017 14:59
-
-
Save dfranciosi/90e626f80191550a5b4b7dc3ec00c0cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pipelineState = require('state').pipeline; | |
const config = require('config'); | |
const utils = require('utils'); | |
const glob = require('glob'); | |
const csvParser = require('csv-parse/lib/sync'); | |
const fs = require('fs'); | |
const log = require('log'); | |
const moment = require('moment'); | |
const request = require('superagent'); | |
const _ = require('lodash'); | |
/** | |
* Nokia NMS - Parse Registry | |
* | |
* This step parses all the registries found given a pattern to search for. | |
* It stops when there is an error while reading or parsing a registry file. | |
* After parsing the following structure is set into the State: | |
* ```javascript | |
* [ | |
* { | |
* filename: "", | |
* content: [{ | |
* registryId: 0, | |
* networkElementName: '', | |
* networkElementTypeName: '', | |
* networkElementActivity: '', | |
* networkElementStatus: 0, | |
* lastBackupDate: '', | |
* lastErrorDate: '', | |
* }] | |
* } | |
* ] | |
* ``` | |
*/ | |
module.exports = (done) => { | |
const managementSystem = pipelineState.get('managementSystem'); | |
const scheduleCreateDate = pipelineState.get('scheduleCreateDate'); | |
const schedule = pipelineState.get('schedule'); | |
let registriesFilename = ''; | |
console.log('managementSystem data', managementSystem); | |
console.log('scheduleCreateDate data', moment(scheduleCreateDate).format('DD-MM-YYYY HH:mi:ss')); | |
if (!managementSystem) return done(Error('Management system not found')); | |
const cmd = utils.parseCommand(managementSystem.defaultFilename); | |
if (cmd !== null && cmd.command === 'custom_date') { | |
const extendedCommand = utils.evaluateCommand(managementSystem.defaultFilename, { returnParameters: true }); | |
const [date, format] = extendedCommand.parameters; | |
registriesFilename = extendedCommand.out; | |
pipelineState.set('scheduleCreateDate', moment(date, format).toISOString()); | |
} else { | |
registriesFilename = utils.evaluateCommand(managementSystem.defaultFilename, { refDate: scheduleCreateDate }); | |
} | |
const matchingRegistryFiles = glob.sync(registriesFilename, { cwd: managementSystem.defaultPath }); | |
if (matchingRegistryFiles.length === 0) return done(Error('No registry files found')); | |
log.info(`[STEPS][NMS] Found registries: ${matchingRegistryFiles.join(',')}`); | |
let parsedRegistries = []; | |
let saveRegistries = []; | |
try { | |
parsedRegistries = matchingRegistryFiles.map((file) => { | |
let parsedContentFile = parseRegistryFile(managementSystem, schedule.schedulingId, file, done); | |
saveRegistries = saveRegistries.concat(parsedContentFile); | |
return { | |
filename: file, | |
content: parsedContentFile, | |
} | |
}); | |
} catch (err) { | |
log.error(`[STEPS][NMS] Error parsing registry: ${err.message}`); | |
return done(err); | |
} | |
//get existing registry | |
getExistingRegistry(`${config.policyManagerBaseURL}/networkManagementSystems/${managementSystem.managementSystemId}/registries`, (err, res) => { | |
if (err) { | |
console.log('[STEP][NMS] Error to get existing registry', err); | |
return done(err); | |
} | |
const dataExistingRegistry = res.body.data && res.body.data.length !== 0 ? res.body.data[0] : []; | |
// merge existing and remote registry | |
const registryMerged = saveRegistries.map(item => Object.assign(item, { registryId: (_.isEmpty(dataExistingRegistry) ? 0 : dataExistingRegistry.registryId) })); | |
console.log('dataExistingRegistry', dataExistingRegistry); | |
pipelineState.set('parsedRegistries', parsedRegistries); | |
// used for save registry in db | |
pipelineState.set('registry.parsed', registryMerged); | |
done(); | |
}); | |
}; | |
function parseRegistryFile(managementSystem, schedulingId, filePath, done) { | |
let parsedCsv = []; | |
try { | |
const fileContent = fs.readFileSync(`${managementSystem.defaultPath}/${filePath}`, { encoding: 'utf8' }); | |
parsedCsv = csvParser(fileContent, { delimiter: ',' }); | |
} catch (err) { | |
log.error(`[STEPS][NMS] Error parsing registry: ${err.message}`); | |
return done(Error(`Error while parsing the registry.`)); | |
} | |
return parsedCsv.map((row) => { | |
return row[0] === '' ? null : { | |
registryId: 0, | |
networkElementName: row[0], | |
networkElementTypeName: row[3], | |
networkElementActivity: row[1], | |
networkElementStatus: parseInt(row[2]), | |
lastBackupDate: row[6], | |
lastErrorDate: row[7], | |
schedulingId: schedulingId, | |
} | |
}).filter(empty => empty !== null); | |
} | |
function getExistingRegistry(url, cb){ | |
if(!url) return []; | |
request | |
.get(url) | |
.end(cb); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment