UNPKG

fog-controller

Version:

Fog Controller project @ iotracks.com

558 lines (480 loc) 24 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _async = require('async'); var _async2 = _interopRequireDefault(_async); var _baseApiController = require('./baseApiController'); var _baseApiController2 = _interopRequireDefault(_baseApiController); var _changeTrackingService = require('../../services/changeTrackingService'); var _changeTrackingService2 = _interopRequireDefault(_changeTrackingService); var _comsatService = require('../../services/comsatService'); var _comsatService2 = _interopRequireDefault(_comsatService); var _dataTracksService = require('../../services/dataTracksService'); var _dataTracksService2 = _interopRequireDefault(_dataTracksService); var _elementService = require('../../services/elementService'); var _elementService2 = _interopRequireDefault(_elementService); var _elementInstanceService = require('../../services/elementInstanceService'); var _elementInstanceService2 = _interopRequireDefault(_elementInstanceService); var _fogService = require('../../services/fogService'); var _fogService2 = _interopRequireDefault(_fogService); var _fogTypeService = require('../../services/fogTypeService'); var _fogTypeService2 = _interopRequireDefault(_fogTypeService); var _networkPairingService = require('../../services/networkPairingService'); var _networkPairingService2 = _interopRequireDefault(_networkPairingService); var _routingService = require('../../services/routingService'); var _routingService2 = _interopRequireDefault(_routingService); var _satelliteService = require('../../services/satelliteService'); var _satelliteService2 = _interopRequireDefault(_satelliteService); var _satellitePortService = require('../../services/satellitePortService'); var _satellitePortService2 = _interopRequireDefault(_satellitePortService); var _consoleService = require('../../services/consoleService'); var _consoleService2 = _interopRequireDefault(_consoleService); var _streamViewerService = require('../../services/streamViewerService'); var _streamViewerService2 = _interopRequireDefault(_streamViewerService); var _userService = require('../../services/userService'); var _userService2 = _interopRequireDefault(_userService); var _appUtils = require('../../utils/appUtils'); var _appUtils2 = _interopRequireDefault(_appUtils); var _winstonLogs = require('../../utils/winstonLogs'); var _winstonLogs2 = _interopRequireDefault(_winstonLogs); var _constants = require('../../constants.js'); var _constants2 = _interopRequireDefault(_constants); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /********************************************* EndPoints ******************************************************/ /********** Instance Routing EndPoint (Get/Post: /api/v2/instance/routing/id/:ID/token/:Token) ***************/ var instanceRoutingEndPoint = function instanceRoutingEndPoint(req, res) { _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl); var params = {}, streamViewerProps = { instanceId: 'bodyParams.ID', setProperty: 'streamViewerData' }, consoleProps = { instanceId: 'bodyParams.ID', setProperty: 'consoleData' }, routingProps = { instanceId: 'bodyParams.ID', setProperty: 'routingData' }; params.bodyParams = req.params; _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams)); _async2.default.waterfall([_async2.default.apply(_baseApiController2.default.checkUserExistance, req, res), _async2.default.apply(_streamViewerService2.default.getStreamViewerByFogInstanceId, streamViewerProps, params), _async2.default.apply(_consoleService2.default.getConsoleByFogInstanceId, consoleProps), _async2.default.apply(_routingService2.default.findByInstanceId, routingProps), getRouting], function (err, result) { _appUtils2.default.sendResponse(res, err, 'routing', params.containerList, result); }); }; /********* Instance Route Create EndPoint (Post: /api/v2/authoring/element/instance/route/create) **********/ /** * @file instanceRoutingController.js * @author Zishan Iqbal * @description This file includes the implementation of the instance-routing end-point */ var instanceRouteCreateEndPoint = function instanceRouteCreateEndPoint(req, res) { _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl); var params = {}, currentTime = new Date().getTime(), watefallMethods = [], userProps = { userId: 'bodyParams.t', setProperty: 'user' }, pubFogProps = { fogId: 'bodyParams.publishingInstanceId', setProperty: 'publishingFogInstance' }, destFogProps = { fogId: 'bodyParams.destinationInstanceId', setProperty: 'destinationFogInstance' }, pubFogTypeProps = { fogTypeId: 'publishingFogInstance.typeKey', setProperty: 'pubFogType' }, destFogTypeProps = { fogTypeId: 'destinationFogInstance.typeKey', setProperty: 'destFogType' }, pubNetworkElementProps = { networkElementId: 'pubFogType.networkElementKey', setProperty: 'pubNetworkElement' }, destNetworkElementProps = { networkElementId: 'destFogType.networkElementKey', setProperty: 'destNetworkElement' }, networkPairingProps = { instanceId1: 'publishingFogInstance.uuid', instanceId2: 'destinationFogInstance.uuid', elementId1: 'bodyParams.publishingElementId', elementId2: 'bodyParams.destinationElementId', networkElementId1: 'pubNetworkElementInstance.uuid', networkElementId2: 'destNetworkElementInstance.uuid', isPublic: false, elementPortId: 'elementInstancePort.id', satellitePortId: 'satellitePort.id', setProperty: 'networkPairingObj' }, routingProps = { publishingInstanceId: 'publishingFogInstance.uuid', destinationInstanceId: 'publishingFogInstance.uuid', publishingElementId: 'bodyParams.publishingElementId', destinationElementId: 'bodyParams.destinationElementId', isNetworkConnection: false, setProperty: 'route' }, pubRoutingProps = { publishingInstanceId: 'publishingFogInstance.uuid', destinationInstanceId: 'publishingFogInstance.uuid', publishingElementId: 'bodyParams.publishingElementId', destinationElementId: 'pubNetworkElementInstance.uuid', isNetworkConnection: true, setProperty: 'publisingRoute' }, destRoutingProps = { publishingInstanceId: 'destinationFogInstance.uuid', destinationInstanceId: 'destinationFogInstance.uuid', publishingElementId: 'destNetworkElementInstance.uuid', destinationElementId: 'bodyParams.destinationElementId', isNetworkConnection: true, setProperty: 'destinationRoute' }, pubElementProps = { elementInstanceId: 'bodyParams.publishingElementId', setProperty: 'publishingElementInstance' }, destElementProps = { elementInstanceId: 'bodyParams.destinationElementId', setProperty: 'destinationElementInstance' }, pubChangeTrackingProps = { fogInstanceId: 'publishingFogInstance.uuid', changeObject: { 'containerList': currentTime, 'containerConfig': currentTime, 'routing': currentTime } }, destChangeTrackingProps = { fogInstanceId: 'destinationFogInstance.uuid', changeObject: { 'containerList': currentTime, 'containerConfig': currentTime, 'routing': currentTime } }, trackProps = { trackId: 'destinationElementInstance.trackId', setProperty: 'dataTrack' }, updateRebuildPubProps = { elementId: 'bodyParams.publishingElementId', updatedData: { rebuild: 1 } }, updateRebuildDestProps = { elementId: 'bodyParams.destinationElementId', updatedData: { rebuild: 1 } }, pubTrackProps = { trackId: 'bodyParams.publishingTrackId', setProperty: 'publishingTrackData', errorMsg: 'publishingTrackId is either missing or invalid.' }, destTrackProps = { trackId: 'bodyParams.destinationTrackId', setProperty: 'destinationTrackData', errorMsg: 'destinationTrackId is either missing or invalid.' }, pubElementInstanceTrackProps = { trackId: 'publishingElementInstance.trackId', setProperty: 'pubElementInstanceTrack', errorMsg: 'publishingElementId has invalid trackId.' }, destElementInstanceTrackProps = { trackId: 'destinationElementInstance.trackId', setProperty: 'destElementInstanceTrack', errorMsg: 'destinationElementId has invalid trackId.' }; params.bodyParams = req.body; _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams)); if (params.bodyParams.publishingInstanceId == params.bodyParams.destinationInstanceId) { watefallMethods = [_async2.default.apply(_userService2.default.getUser, userProps, params), _async2.default.apply(_fogService2.default.getFogInstance, pubFogProps), _async2.default.apply(_fogService2.default.getFogInstance, destFogProps), _async2.default.apply(_elementInstanceService2.default.getElementInstance, pubElementProps), _async2.default.apply(_elementInstanceService2.default.getElementInstance, destElementProps), _async2.default.apply(_dataTracksService2.default.getDataTrackById, pubElementInstanceTrackProps), _async2.default.apply(_dataTracksService2.default.getDataTrackById, destElementInstanceTrackProps), _async2.default.apply(_routingService2.default.createRoute, routingProps), _async2.default.apply(_elementInstanceService2.default.getElementInstanceRouteDetails, destElementProps), _async2.default.apply(_elementInstanceService2.default.updateElemInstance, updateRebuildPubProps), _async2.default.apply(_elementInstanceService2.default.updateElemInstance, updateRebuildDestProps), _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, pubChangeTrackingProps), getRouteDetails]; } else { watefallMethods = [_async2.default.apply(_userService2.default.getUser, userProps, params), _async2.default.apply(_fogService2.default.getFogInstance, pubFogProps), _async2.default.apply(_fogService2.default.getFogInstance, destFogProps), _async2.default.apply(_fogTypeService2.default.getFogTypeDetail, pubFogTypeProps), _async2.default.apply(_fogTypeService2.default.getFogTypeDetail, destFogTypeProps), _async2.default.apply(_elementInstanceService2.default.getElementInstance, pubElementProps), _async2.default.apply(_elementInstanceService2.default.getElementInstance, destElementProps), _async2.default.apply(_dataTracksService2.default.getDataTrackById, pubTrackProps), //async.apply(DataTracksService.getDataTrackById, destTrackProps), _async2.default.apply(_dataTracksService2.default.getDataTrackById, pubElementInstanceTrackProps), _async2.default.apply(_dataTracksService2.default.getDataTrackById, destElementInstanceTrackProps), _comsatService2.default.openPortOnRadomComsat, createSatellitePort, _async2.default.apply(_elementService2.default.getNetworkElement, pubNetworkElementProps), createPubNetworkElementInstance, _async2.default.apply(_elementService2.default.getNetworkElement, destNetworkElementProps), createDestNetworkElementInstance, _async2.default.apply(_networkPairingService2.default.createNetworkPairing, networkPairingProps), _async2.default.apply(_routingService2.default.createRoute, pubRoutingProps), _async2.default.apply(_routingService2.default.createRoute, destRoutingProps), _async2.default.apply(_elementInstanceService2.default.updateElemInstance, updateRebuildPubProps), _async2.default.apply(_elementInstanceService2.default.updateElemInstance, updateRebuildDestProps), _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, pubChangeTrackingProps), _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, destChangeTrackingProps), _async2.default.apply(_dataTracksService2.default.getDataTrackById, trackProps), getOutputDetails]; } _async2.default.waterfall(watefallMethods, function (err, result) { var errMsg = 'Internal error: There was a problem trying to create the ioElement Routing.' + result; _appUtils2.default.sendResponse(res, err, 'route', params.output, errMsg); }); }; var createPubNetworkElementInstance = function createPubNetworkElementInstance(params, callback) { var networkElementInstanceProps = { networkElement: 'pubNetworkElement', fogInstanceId: 'publishingFogInstance.uuid', satellitePort: 'satellitePort.port1', satelliteDomain: 'satellite.domain', satelliteCertificate: 'satellite.cert', passcode: 'comsatPort.passcode1', trackId: 'bodyParams.publishingTrackId', userId: 'user.id', networkName: 'Network for Element ' + params.bodyParams.publishingElementId, networkPort: 0, isPublic: false, setProperty: 'pubNetworkElementInstance' }; _elementInstanceService2.default.createNetworkElementInstance(networkElementInstanceProps, params, callback); }; var createDestNetworkElementInstance = function createDestNetworkElementInstance(params, callback) { var networkElementInstanceProps = { networkElement: 'destNetworkElement', fogInstanceId: 'destinationFogInstance.uuid', satellitePort: 'satellitePort.port2', satelliteDomain: 'satellite.domain', satelliteCertificate: 'satellite.cert', passcode: 'comsatPort.passcode2', trackId: 'bodyParams.publishingTrackId', userId: 'user.id', networkName: 'Network for Element ' + params.bodyParams.destinationElementId, networkPort: 0, isPublic: false, setProperty: 'destNetworkElementInstance' }; _elementInstanceService2.default.createNetworkElementInstance(networkElementInstanceProps, params, callback); }; /********* Instance Route Delete EndPoint (Post: /api/v2/authoring/element/instance/route/delete) **********/ var instanceRouteDeleteEndPoint = function instanceRouteDeleteEndPoint(req, res) { _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl); var params = {}, currentTime = new Date().getTime(), watefallMethods = [], userProps = { userId: 'bodyParams.t', setProperty: 'user' }, pubFogProps = { fogId: 'bodyParams.publishingInstanceId', setProperty: 'publishingFogInstance' }, destFogProps = { fogId: 'bodyParams.destinationInstanceId', setProperty: 'destinationFogInstance' }, deleteRouteProps = { instanceId1: 'bodyParams.publishingInstanceId', instanceId2: 'bodyParams.destinationInstanceId', elementId1: 'bodyParams.publishingElementId', elementId2: 'bodyParams.destinationElementId', isNetwork: false }, networkPairingProps = { instanceId1: 'bodyParams.publishingInstanceId', instanceId2: 'bodyParams.destinationInstanceId', elementId1: 'bodyParams.publishingElementId', elementId2: 'bodyParams.destinationElementId', setProperty: 'networkPairing' }, satellitePortProps = { satellitePortId: 'networkPairing.satellitePortId', setProperty: 'satellitePort' }, satelliteProps = { satelliteId: 'satellitePort.satellite_id', setProperty: 'satellite' }, deleteSatelliteProps = { satellitePortId: 'satellitePort.id' }, deletePubRouteProps = { instanceId1: 'bodyParams.publishingInstanceId', instanceId2: 'bodyParams.publishingInstanceId', elementId1: 'bodyParams.publishingElementId', elementId2: 'networkPairing.networkElementId1', isNetwork: true }, deleteDestRouteProps = { instanceId1: 'bodyParams.destinationInstanceId', instanceId2: 'bodyParams.destinationInstanceId', elementId1: 'networkPairing.networkElementId2', elementId2: 'bodyParams.destinationElementId', isNetwork: true }, deleteNWElement1Props = { elementId: 'networkPairing.networkElementId1' }, deleteNWElement2Props = { elementId: 'networkPairing.networkElementId2' }, delNetworkPairingProps = { networkPairingId: 'networkPairing.id' }, pubChangeTrackingProps = { fogInstanceId: 'bodyParams.publishingInstanceId', changeObject: { 'containerList': new Date().getTime(), 'containerConfig': new Date().getTime(), 'routing': new Date().getTime() } }, destChangeTrackingProps = { fogInstanceId: 'bodyParams.destinationInstanceId', changeObject: { 'containerList': new Date().getTime(), 'containerConfig': new Date().getTime(), 'routing': new Date().getTime() } }, changeTrackingProps = { fogInstanceId: 'bodyParams.destinationInstanceId', changeObject: { 'routing': new Date().getTime() } }; params.bodyParams = req.body; _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams)); if (params.bodyParams.isNetworkConnection == 0) { watefallMethods = [_async2.default.apply(_userService2.default.getUser, userProps, params), _async2.default.apply(_routingService2.default.deleteByFogAndElement, deleteRouteProps), _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, changeTrackingProps), getDeleteOutput]; } else { watefallMethods = [_async2.default.apply(_userService2.default.getUser, userProps, params), _async2.default.apply(_fogService2.default.getFogInstance, pubFogProps), _async2.default.apply(_fogService2.default.getFogInstance, destFogProps), _async2.default.apply(_networkPairingService2.default.getNetworkPairingByFogAndElement, networkPairingProps), _async2.default.apply(_satellitePortService2.default.getSatellitePort, satellitePortProps), _async2.default.apply(_satelliteService2.default.getSatelliteById, satelliteProps), _comsatService2.default.closePortOnComsat, _async2.default.apply(_satellitePortService2.default.deleteSatellitePort, deleteSatelliteProps), _async2.default.apply(_routingService2.default.deleteByFogAndElement, deletePubRouteProps), _async2.default.apply(_routingService2.default.deleteByFogAndElement, deleteDestRouteProps), _async2.default.apply(_elementInstanceService2.default.deleteElementInstance, deleteNWElement1Props), _async2.default.apply(_elementInstanceService2.default.deleteElementInstance, deleteNWElement2Props), _async2.default.apply(_networkPairingService2.default.deleteNetworkPairingById, delNetworkPairingProps), _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, pubChangeTrackingProps), _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, destChangeTrackingProps), getDeleteOutput]; } _async2.default.waterfall(watefallMethods, function (err, result) { var errMsg = 'Internal error: There was a problem trying to delete the ioElement Routing.' + result; _appUtils2.default.sendResponse(res, err, 'route', params.output, errMsg); }); }; /************************************* Extra Functions **************************************************/ var getRouting = function getRouting(params, callback) { try { var containerList = []; for (var i = 0; i < params.routingData.length; i++) { var container = params.routingData[i], containerID = container.publishing_element_id, destinationElementID = container.destination_element_id, foundIt = false; params.container = container; for (var j = 0; j < containerList.length; j++) { var curItem = containerList[j], curID = curItem.container; if (curID == containerID) { foundIt = true; var outElementLabel = destinationElementID; if (params.streamViewerData) { if (destinationElementID == params.streamViewerData.element_id) { outElementLabel = "viewer"; } } if (params.consoleData) { if (destinationElementID == params.consoleData.elementId) { outElementLabel = "debug"; } } containerList[j]["receivers"].push(outElementLabel); } } if (foundIt == false) { var tmpNewContainerItem = {}, receiverList = new Array(), _outElementLabel = destinationElementID; tmpNewContainerItem.container = containerID; if (params.streamViewerData) { if (destinationElementID == params.streamViewerData.element_id) { _outElementLabel = "viewer"; } } if (params.consoleData) { if (destinationElementID == params.consoleData.elementId) { _outElementLabel = "debug"; } } receiverList.push(_outElementLabel); tmpNewContainerItem.receivers = receiverList; containerList.push(tmpNewContainerItem); } } params.containerList = containerList; callback(null, params); } catch (e) { _winstonLogs2.default.error(e); } }; var createSatellitePort = function createSatellitePort(params, callback) { var satellitePortProps = { satellitePortObj: { port1: params.comsatPort.port1, port2: params.comsatPort.port2, maxConnectionsPort1: 60, maxConnectionsPort2: 0, passcodePort1: params.comsatPort.passcode1, passcodePort2: params.comsatPort.passcode2, heartBeatAbsenceThresholdPort1: 60000, heartBeatAbsenceThresholdPort2: 0, satellite_id: params.satellite.id, mappingId: params.comsatPort.id }, setProperty: 'satellitePort' }; _satellitePortService2.default.createSatellitePort(satellitePortProps, params, callback); }; var getRouteDetails = function getRouteDetails(params, callback) { params.output = { elementId: params.bodyParams.destinationElementId, elementName: params.destinationElementInstance[0].elementInstanceName, elementTypeName: params.destinationElementInstance[0].elementName, trackId: params.destinationElementInstance[0].trackId, trackName: params.destinationElementInstance[0].trackName, instanceId: params.destinationFogInstance.uuid, instanceName: params.destinationFogInstance.name }; if (params.bodyParams.publishingInstanceId != params.bodyParams.destinationInstanceId) {} else if (params.bodyParams.publishingTrackId != params.bodyParams.destinationTrackId) { params.output.instanceId = ''; params.output.instanceName = ''; } else { params.output.instanceId = ''; params.output.instanceName = ''; params.output.trackId = ''; params.output.trackName = ''; } callback(null, params); }; var getOutputDetails = function getOutputDetails(params, callback) { params.output = { elementId: params.bodyParams.destinationElementId, elementName: params.destinationElementInstance.name, elementTypeName: params.destinationElementInstance.typeKey, trackId: params.destinationElementInstance.trackId, trackName: params.dataTrack.name, instanceId: params.destinationFogInstance.uuid, instanceName: params.destinationFogInstance.name }; if (params.bodyParams.publishingInstanceId != params.bodyParams.destinationInstanceId) {} else if (params.bodyParams.publishingTrackId != params.bodyParams.destinationTrackId) { params.output.instanceId = ''; params.output.instanceName = ''; } else { params.output.instanceId = ''; params.output.instanceName = ''; params.output.trackId = ''; params.output.trackName = ''; } callback(null, params); }; var getDeleteOutput = function getDeleteOutput(params, callback) { params.output = { publishinginstanceid: params.bodyParams.publishingInstanceId, publishingtrackid: params.bodyParams.publishingTrackId, publishingelementid: params.bodyParams.publishingElementId, destinationinstanceid: params.bodyParams.destinationInstanceId, destinationtrackid: params.bodyParams.destinationTrackId, destinationelementid: params.bodyParams.destinationElementId }; callback(null, params); }; exports.default = { instanceRoutingEndPoint: instanceRoutingEndPoint, instanceRouteCreateEndPoint: instanceRouteCreateEndPoint, instanceRouteDeleteEndPoint: instanceRouteDeleteEndPoint };