UNPKG

fog-controller

Version:

Fog Controller project @ iotracks.com

1,194 lines (1,068 loc) 45.3 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _async = require('async'); var _async2 = _interopRequireDefault(_async); var _express = require('express'); var _express2 = _interopRequireDefault(_express); var _changeTrackingService = require('../../services/changeTrackingService'); var _changeTrackingService2 = _interopRequireDefault(_changeTrackingService); var _comsatService = require('../../services/comsatService'); var _comsatService2 = _interopRequireDefault(_comsatService); var _consoleService = require('../../services/consoleService'); var _consoleService2 = _interopRequireDefault(_consoleService); var _elementService = require('../../services/elementService'); var _elementService2 = _interopRequireDefault(_elementService); var _elementInstanceService = require('../../services/elementInstanceService'); var _elementInstanceService2 = _interopRequireDefault(_elementInstanceService); var _elementInstancePortService = require('../../services/elementInstancePortService'); var _elementInstancePortService2 = _interopRequireDefault(_elementInstancePortService); var _fogAccessTokenService = require('../../services/fogAccessTokenService'); var _fogAccessTokenService2 = _interopRequireDefault(_fogAccessTokenService); var _fogProvisionKeyService = require('../../services/fogProvisionKeyService'); var _fogProvisionKeyService2 = _interopRequireDefault(_fogProvisionKeyService); var _fogService = require('../../services/fogService'); var _fogService2 = _interopRequireDefault(_fogService); var _fogTypeService = require('../../services/fogTypeService'); var _fogTypeService2 = _interopRequireDefault(_fogTypeService); var _fogUserService = require('../../services/fogUserService'); var _fogUserService2 = _interopRequireDefault(_fogUserService); var _networkPairingService = require('../../services/networkPairingService'); var _networkPairingService2 = _interopRequireDefault(_networkPairingService); var _satelliteService = require('../../services/satelliteService'); var _satelliteService2 = _interopRequireDefault(_satelliteService); var _satellitePortService = require('../../services/satellitePortService'); var _satellitePortService2 = _interopRequireDefault(_satellitePortService); 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); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /********************************************* EndPoints ******************************************************/ /**************************** Fog-Controller Status EndPoint (Get: /api/v2/status) ***************************/ /** * @file fogController.js * @author Zishan Iqbal * @description This file includes the implementation of the status end-point */ var getFogControllerStatusEndPoint = function getFogControllerStatusEndPoint(req, res) { _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl); var milliseconds = new Date().getTime(); _winstonLogs2.default.info("Endpoint served successfully"); res.status(200); res.send({ "status": "ok", "timestamp": milliseconds }); }; /******** Fog Instances List By UserID EndPoint (Get: /api/v2/authoring/integrator/instances/list/:t Post: /api/v2/authoring/fabric/instances/list ) *******/ var fogInstancesListEndPoint = function fogInstancesListEndPoint(req, res) { _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl); var params = {}, userProps = { userId: 'bodyParams.t', setProperty: 'user' }, fogInstanceForUserProps = { userId: 'user.id', setProperty: 'fogInstance' }; params.bodyParams = req.params; if (req.body.t) { params.bodyParams = req.body; } _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams)); _async2.default.waterfall([_async2.default.apply(_userService2.default.getUser, userProps, params), _async2.default.apply(_fogService2.default.getFogInstanceForUser, fogInstanceForUserProps)], function (err, result) { _appUtils2.default.sendResponse(res, err, 'instances', params.fogInstance, result); }); }; /** ******************* Fog Instance Create EndPoint (Get: /api/v2/instance/create/type/:type) **************** * @deprecated **/ var fogInstanceCreateEndPoint = function fogInstanceCreateEndPoint(req, res) { _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl); var params = {}, userProps = { userId: 'bodyParams.t', setProperty: 'user' }, fogTypeProps = { fogTypeId: 'bodyParams.type', setProperty: 'fogType' }, createFogProps = { name: 'bodyParams.name', location: 'bodyParams.location', latitude: 'bodyParams.latitude', longitude: 'bodyParams.longitude', description: 'bodyParams.description', fogType: 'bodyParams.type', setProperty: 'fogInstance' }, createChangeTrackingProps = { fogInstanceId: 'fogInstance.uuid', setProperty: null }, createFogUserProps = { userId: 'user.id', instanceId: 'fogInstance.uuid', setProperty: 'fogUser' }; params.bodyParams = req.params; params.bodyParams.t = req.query.t; _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams)); _async2.default.waterfall([_async2.default.apply(_userService2.default.getUser, userProps, params), _async2.default.apply(_fogTypeService2.default.getFogTypeDetail, fogTypeProps), _async2.default.apply(_fogService2.default.createFogInstance, createFogProps), _async2.default.apply(_changeTrackingService2.default.createFogChangeTracking, createChangeTrackingProps), _async2.default.apply(_fogUserService2.default.createFogUser, createFogUserProps)], function (err, result) { var output = void 0; if (!err) { output = params.fogInstance.uuid; } _appUtils2.default.sendResponse(res, err, 'instanceId', output, result); }); }; /******************** Get Fog List EndPoint (Get: /api/v2/instance/getfabriclist) ******************/ // const getFogListEndPoint = function(req, res){ // logger.info("Endpoint hit: "+ req.originalUrl); // let params = {}, // userProps = { // userId: 'bodyParams.t', // setProperty: 'user' // }, // fogListProps = { // setProperty: 'fogList' // }; // params.bodyParams = req.params; // params.bodyParams.t = req.query.t; // logger.info("Parameters:" + JSON.stringify(params.bodyParams)); // async.waterfall([ // async.apply(UserService.getUser, userProps, params), // async.apply(FogService.getFogList, fogListProps) // ], // function(err, result) { // let errMsg = 'Internal error: ' + result; // AppUtils.sendResponse(res, err, 'fogList', params.fogList, errMsg); // }); // }; /******************** Get Fog Types EndPoint (Get: /api/v2/authoring/fabric/types/list) ******************/ var getFogTypesEndPoint = function getFogTypesEndPoint(req, res) { _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl); var params = {}, userProps = { userId: 'bodyParams.t', setProperty: 'user' }, fogTypesListProps = { setProperty: 'fogTypesList' }; params.bodyParams = req.params; params.bodyParams.t = req.query.t; _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams)); _async2.default.waterfall([_async2.default.apply(_userService2.default.getUser, userProps, params), _async2.default.apply(_fogTypeService2.default.getFogTypesList, fogTypesListProps)], function (err, result) { var errMsg = 'Internal error: ' + result; _appUtils2.default.sendResponse(res, err, 'fogTypes', params.fogTypesList, errMsg); }); }; /***************** Fog Instance Delete EndPoint (Post: /api/v2/authoring/fabric/instance/delete) *************/ var fogInstanceDeleteEndPoint = function fogInstanceDeleteEndPoint(req, res) { _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl); var params = {}, userProps = { userId: 'bodyParams.t', setProperty: 'user' }, instanceProps = { instanceId: 'bodyParams.instanceId' }; params.bodyParams = req.body; _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams)); _async2.default.waterfall([_async2.default.apply(_userService2.default.getUser, userProps, params), _async2.default.apply(_changeTrackingService2.default.deleteChangeTracking, instanceProps), _async2.default.apply(_fogUserService2.default.deleteFogUserByInstanceId, instanceProps), _async2.default.apply(_streamViewerService2.default.deleteStreamViewerByFogInstanceId, instanceProps), _async2.default.apply(_consoleService2.default.deleteConsoleByFogInstanceId, instanceProps), _async2.default.apply(_fogProvisionKeyService2.default.deleteProvisonKeyByInstanceId, instanceProps), _async2.default.apply(_fogService2.default.deleteFogInstance, instanceProps)], function (err, result) { var errMsg = 'Internal error: ' + result; _appUtils2.default.sendResponse(res, err, 'instanceId', params.bodyParams.instanceId, errMsg); }); }; /*********** Integrator Instance Delete EndPoint (Post: /api/v2/authoring/integrator/instance/delete) **********/ var integratorInstanceDeleteEndPoint = function integratorInstanceDeleteEndPoint(req, res) { _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl); var params = {}, userProps = { userId: 'bodyParams.t', setProperty: 'user' }, instanceProps = { instanceId: 'bodyParams.instanceId' }, updateByFogUuIdProps = { fogInstanceId: 'bodyParams.instanceId', updatedFogId: null }, fogProps = { fogId: 'bodyParams.instanceId' }, fogUserProps = { instanceId: 'bodyParams.instanceId', setProperty: 'fogUser' }, deleteFogUserProps = { userId: 'fogUser.user_id', instanceId: 'bodyParams.instanceId' }; params.bodyParams = req.body; _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams)); _async2.default.waterfall([_async2.default.apply(_userService2.default.getUser, userProps, params), _async2.default.apply(_fogService2.default.getFogInstance, fogProps), _async2.default.apply(_elementInstanceService2.default.updateElemInstanceByFogUuId, updateByFogUuIdProps), _async2.default.apply(_changeTrackingService2.default.deleteChangeTracking, instanceProps), _async2.default.apply(_fogUserService2.default.getFogUserByInstanceId, fogUserProps), _async2.default.apply(_fogAccessTokenService2.default.deleteFogAccessTokenByFogId, fogProps), _async2.default.apply(_fogUserService2.default.deleteFogUserByInstanceIdAndUserId, deleteFogUserProps), //async.apply(UserService.deleteByUserId, instanceProps), _async2.default.apply(_streamViewerService2.default.deleteStreamViewerByFogInstanceId, instanceProps), _async2.default.apply(_consoleService2.default.deleteConsoleByFogInstanceId, instanceProps), _async2.default.apply(_fogProvisionKeyService2.default.deleteProvisonKeyByInstanceId, instanceProps), _async2.default.apply(_fogService2.default.deleteFogInstance, instanceProps)], function (err, result) { var errMsg = 'Internal error: ' + result; _appUtils2.default.sendResponse(res, err, 'instanceid', params.bodyParams.instanceId, errMsg); }); }; /*********** Get Fog Details EndPoint (Post: /api/v2/authoring/fabric/details) **********/ var getFogDetailsEndpoint = function getFogDetailsEndpoint(req, res) { _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl); var params = {}, userProps = { userId: 'bodyParams.t', setProperty: 'user' }, instanceProps = { instanceId: 'bodyParams.instanceId', setProperty: 'fogInstances' }; params.bodyParams = req.body; _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams)); _async2.default.waterfall([_async2.default.apply(_userService2.default.getUser, userProps, params), _async2.default.apply(_fogService2.default.getFogInstanceDetails, instanceProps)], function (err, result) { _appUtils2.default.sendResponse(res, err, 'fog', params.fogInstances, result); }); }; /*********** Update Fog settings EndPoint (Post: /api/v2/authoring/fabric/instances/settings/update) **********/ var updateFogSettingsEndpoint = function updateFogSettingsEndpoint(req, res) { _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl); var params = {}, userProps = { userId: 'bodyParams.t', setProperty: 'user' }, instanceProps = { fogId: 'bodyParams.instanceId', setProperty: 'fogInstance' }, fogTypeProps = { fogTypeId: 'fogInstance.typeKey', setProperty: 'fogTypeData' }; params.bodyParams = req.body; _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams)); _async2.default.waterfall([_async2.default.apply(_userService2.default.getUser, userProps, params), _async2.default.apply(_fogService2.default.getFogInstance, instanceProps), _async2.default.apply(_fogTypeService2.default.getFogTypeDetail, fogTypeProps), bluetoothElementForFog, debugConsoleForFog, streamViewerForFog, halForFog, mongoForFog, influxForFog, grafanaForFog, updateFog, updateChangeTracking], function (err, result) { if (!err) { var successLabelArr = void 0, successValueArr = void 0; successLabelArr = ['ppp', 'ttt'], successValueArr = [params.bodyParams.bluetooth, '1']; _appUtils2.default.sendMultipleResponse(res, err, successLabelArr, successValueArr, result); } else { _appUtils2.default.sendResponse(res, err, '', '', result); } }); }; var updateFog = function updateFog(params, callback) { var fogProps = { instanceId: 'bodyParams.instanceId', updatedFog: { name: params.bodyParams.name, description: params.bodyParams.description, location: params.bodyParams.location, networkinterface: params.bodyParams.networkInterface, dockerurl: params.bodyParams.dockerUrl, disklimit: params.bodyParams.diskLimit, diskdirectory: params.bodyParams.diskDirectory, memorylimit: params.bodyParams.memoryLimit, cpulimit: params.bodyParams.cpuLimit, loglimit: params.bodyParams.logLimit, logdirectory: params.bodyParams.logDirectory, logfilecount: params.bodyParams.logFileCount, debug: params.bodyParams.debug, viewer: params.bodyParams.viewer, bluetooth: params.bodyParams.bluetooth, hal: params.bodyParams.hal, mongo: params.bodyParams.mongo, influx: params.bodyParams.influx, grafana: params.bodyParams.grafana, statusfrequency: params.bodyParams.statusFrequency, changefrequency: params.bodyParams.changeFrequency, scanfrequency: params.bodyParams.scanFrequency, proxy: params.bodyParams.proxy, isolateddockercontainer: params.bodyParams.docker } }; populateLatAndLonIfValid(fogProps.updatedFog, params.bodyParams.lat, params.bodyParams.lon); _fogService2.default.updateFogInstance(fogProps, params, callback); }; var populateLatAndLonIfValid = function populateLatAndLonIfValid(to, lat, lon) { if (lat < 90 && lat > -90 && lon < 180 && lon > -180) { to['latitude'] = lat; to['longitude'] = lon; } }; var streamViewerForFog = function streamViewerForFog(params, callback) { if (params.bodyParams.viewer && params.bodyParams.viewer != params.fogInstance.viewer) { params.isViewer = 1; if (params.bodyParams.viewer > 0) { var elementProps = { networkElementId: 'fogTypeData.streamViewerElementKey', setProperty: 'streamViewerElement' }, streamViewerProps = { elementKey: 'fogTypeData.streamViewerElementKey', userId: 'user.id', fogInstanceId: 'fogInstance.uuid', registryId: 'streamViewerElement.registry_id', setProperty: 'streamViewerElementInstnace' }, streamViewerPortProps = { userId: 'user.id', internalPort: 80, externalPort: 60400, elementId: 'streamViewerElementInstnace.uuid', setProperty: 'streamViewerPort' }, elementInstanceProps = { updatedData: { last_updated: new Date().getTime(), updatedBy: params.user.id }, elementId: 'streamViewerElementInstnace.uuid' }, satelliteProps = { satelliteId: 'streamViewerSatellitePort.satellite_id', setProperty: 'streamViewerSatellite' }, networkElementProps = { networkElementId: 'fogTypeData.networkElementKey', setProperty: 'streamViewerNetworkElement' }, networkPairingProps = { instanceId1: 'fogInstance.uuid', instanceId2: null, elementId1: 'streamViewerElementInstnace.uuid', elementId2: null, networkElementId1: 'newStreamViewerNetworkElementInstance.uuid', networkElementId2: null, isPublic: true, elementPortId: 'streamViewerPort.id', satellitePortId: 'streamViewerSatellitePort.id', setProperty: 'streamViewerNetworkPairingObj' }, changeTrackingProps = { fogInstanceId: 'bodyParams.instanceId', changeObject: { containerConfig: new Date().getTime(), containerList: new Date().getTime() } }; _async2.default.waterfall([_async2.default.apply(_elementService2.default.getNetworkElement, elementProps, params), _async2.default.apply(_elementInstanceService2.default.createStreamViewerElement, streamViewerProps), _async2.default.apply(_elementInstancePortService2.default.createElementInstancePortByPortValue, streamViewerPortProps), _async2.default.apply(_elementInstanceService2.default.updateElemInstance, elementInstanceProps), _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, changeTrackingProps), _comsatService2.default.openPortOnRadomComsat, createStreamViewerSatellitePort, _async2.default.apply(_satelliteService2.default.getSatelliteById, satelliteProps), _async2.default.apply(_elementService2.default.getNetworkElement, networkElementProps), createStreamViewerNetworkElementInstance, _async2.default.apply(_networkPairingService2.default.createNetworkPairing, networkPairingProps), createStreamViewerConsole, _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, changeTrackingProps)], function (err, result) { if (!err) { callback(null, params); } else { callback('Error', result); } }); } else { var _elementInstanceProps = { instanceId: 'bodyParams.instanceId' }; _async2.default.waterfall([_async2.default.apply(_elementInstanceService2.default.deleteStreamViewerInstances, _elementInstanceProps, params), _async2.default.apply(_streamViewerService2.default.deleteStreamViewerByFogInstanceId, _elementInstanceProps)], function (err, result) { if (!err) { callback(null, params); } else { callback('Error', result); } }); } } else { callback(null, params); } }; var createStreamViewerConsole = function createStreamViewerConsole(params, callback) { var createStreamProps = { streamViewerObj: { version: 1, apiBaseUrl: 'https://' + params.streamViewerSatellite.domain + ':' + params.streamViewerSatellitePort.port2, accessToken: JSON.parse(params.streamViewerElementInstnace.config).accesstoken, elementId: params.streamViewerElementInstnace.uuid, iofog_uuid: params.fogInstance.uuid } }; _streamViewerService2.default.createStreamViewer(createStreamProps, params, callback); }; var createStreamViewerNetworkElementInstance = function createStreamViewerNetworkElementInstance(params, callback) { var config = { 'mode': 'public', 'host': params.streamViewerSatellite.domain, 'port': params.streamViewerSatellitePort.port1, 'cert': params.streamViewerSatellite.cert, 'connectioncount': 60, 'passcode': params.comsatPort.passcode1, 'localhost': 'iofog', 'localport': 60400, 'heartbeatfrequency': 20000, 'heartbeatabsencethreshold': 60000 }, elementInstanceProps = { elementInstance: { uuid: _appUtils2.default.generateInstanceId(32), trackId: 0, element_key: params.fogTypeData.networkElementKey, config: JSON.stringify(config), name: 'Network for Stream Viewer', last_updated: new Date().getTime(), updatedBy: params.user.id, configLastUpdated: new Date().getTime(), isStreamViewer: false, isDebugConsole: false, isManager: false, isNetwork: true, registryId: params.streamViewerNetworkElement.registry_id, rebuild: false, rootHostAccess: false, logSize: 50, iofog_uuid: params.bodyParams.instanceId, volumeMappings: '{"volumemappings": []}' }, setProperty: 'newStreamViewerNetworkElementInstance' }; _elementInstanceService2.default.createElementInstanceObj(elementInstanceProps, params, callback); }; var createStreamViewerSatellitePort = function createStreamViewerSatellitePort(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: 'streamViewerSatellitePort' }; _satellitePortService2.default.createSatellitePort(satellitePortProps, params, callback); }; var debugConsoleForFog = function debugConsoleForFog(params, callback) { if (params.bodyParams.debug && params.bodyParams.debug != params.fogInstance.debug) { params.isDebug = 1; if (params.bodyParams.debug > 0) { var elementProps = { networkElementId: 'fogTypeData.consoleElementKey', setProperty: 'debugElement' }, debugConsoleProps = { elementKey: 'fogTypeData.consoleElementKey', userId: 'user.id', fogInstanceId: 'fogInstance.uuid', registryId: 'debugElement.registry_id', setProperty: 'debugConsole' }, debugConsolePortProps = { userId: 'user.id', internalPort: 80, externalPort: 60401, elementId: 'debugConsole.uuid', setProperty: 'debugConsolePort' }, elementInstanceProps = { updatedData: { last_updated: new Date().getTime(), updatedBy: params.user.id }, elementId: 'debugConsole.uuid' }, satelliteProps = { satelliteId: 'satellitePort.satellite_id', setProperty: 'satellite' }, networkElementProps = { networkElementId: 'fogTypeData.networkElementKey', setProperty: 'networkElement' }, networkPairingProps = { instanceId1: 'fogInstance.uuid', instanceId2: null, elementId1: 'debugConsole.uuid', elementId2: null, networkElementId1: 'newNetworkElementInstance.uuid', networkElementId2: null, isPublic: true, elementPortId: 'debugConsolePort.id', satellitePortId: 'satellitePort.id', setProperty: 'networkPairingObj' }, changeTrackingProps = { fogInstanceId: 'bodyParams.instanceId', changeObject: { containerConfig: new Date().getTime(), containerList: new Date().getTime() } }; _async2.default.waterfall([_async2.default.apply(_elementService2.default.getNetworkElement, elementProps, params), _async2.default.apply(_elementInstanceService2.default.createDebugConsole, debugConsoleProps), _async2.default.apply(_elementInstancePortService2.default.createElementInstancePortByPortValue, debugConsolePortProps), _async2.default.apply(_elementInstanceService2.default.updateElemInstance, elementInstanceProps), _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, changeTrackingProps), _comsatService2.default.openPortOnRadomComsat, createSatellitePort, _async2.default.apply(_satelliteService2.default.getSatelliteById, satelliteProps), _async2.default.apply(_elementService2.default.getNetworkElement, networkElementProps), createNetworkElementInstance, _async2.default.apply(_networkPairingService2.default.createNetworkPairing, networkPairingProps), createConsole, _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, changeTrackingProps)], function (err, result) { if (!err) { callback(null, params); } else { callback('Error', result); } }); } else { var _elementInstanceProps2 = { instanceId: 'bodyParams.instanceId' }; _async2.default.waterfall([_async2.default.apply(_elementInstanceService2.default.deleteDebugConsoleInstances, _elementInstanceProps2, params), _async2.default.apply(_consoleService2.default.deleteConsoleByFogInstanceId, _elementInstanceProps2)], function (err, result) { if (!err) { callback(null, params); } else { callback('Error', result); } }); } } else { callback(null, params); } }; var createConsole = function createConsole(params, callback) { var createConsoleProps = { consoleObj: { version: 1, apiBaseUrl: 'https://' + params.satellite.domain + ':' + params.satellitePort.port2, accessToken: JSON.parse(params.debugConsole.config).accesstoken, elementId: params.debugConsole.uuid, iofog_uuid: params.fogInstance.uuid } }; _consoleService2.default.createConsole(createConsoleProps, params, callback); }; var createNetworkElementInstance = function createNetworkElementInstance(params, callback) { var config = { 'mode': 'public', 'host': params.satellite.domain, 'port': params.satellitePort.port1, 'cert': params.satellite.cert, 'connectioncount': 60, 'passcode': params.comsatPort.passcode1, 'localhost': 'iofog', 'localport': 60401, 'heartbeatfrequency': 20000, 'heartbeatabsencethreshold': 60000 }, elementInstanceProps = { elementInstance: { uuid: _appUtils2.default.generateInstanceId(32), trackId: 0, element_key: params.fogTypeData.networkElementKey, config: JSON.stringify(config), name: 'Network for Debug Console', last_updated: new Date().getTime(), updatedBy: params.user.id, configLastUpdated: new Date().getTime(), isStreamViewer: false, isDebugConsole: false, isManager: false, isNetwork: true, registryId: params.networkElement.registry_id, rebuild: false, rootHostAccess: false, logSize: 50, iofog_uuid: params.bodyParams.instanceId, volumeMappings: '{"volumemappings": []}' }, setProperty: 'newNetworkElementInstance' }; _elementInstanceService2.default.createElementInstanceObj(elementInstanceProps, params, callback); }; 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 halForFog = function halForFog(params, callback) { if (params.bodyParams.hal != params.fogInstance.hal) { params.isHal = 1; if (params.bodyParams.hal > 0) { var elementProps = { networkElementId: 'fogTypeData.halElementKey', setProperty: 'halElement' }, changeTrackingProps = { fogInstanceId: 'bodyParams.instanceId', changeObject: { containerConfig: new Date().getTime(), containerList: new Date().getTime() } }; _async2.default.waterfall([_async2.default.apply(_elementService2.default.getNetworkElement, elementProps, params), createHalElementInstance, _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, changeTrackingProps)], function (err, result) { if (!err) { callback(null, params); } else { callback('Error', result); } }); } else { var elementInstanceProps = { instanceId: 'bodyParams.instanceId', elementKey: 'fogTypeData.halElementKey' }; _elementInstanceService2.default.deleteElementInstancesByInstanceIdAndElementKey(elementInstanceProps, params, callback); } } else { callback(null, params); } }; var grafanaForFog = function grafanaForFog(params, callback) { if (params.bodyParams.grafana != params.fogInstance.grafana) { params.isGrafana = 1; if (params.bodyParams.grafana > 0) { var elementProps = { networkElementId: 'fogTypeData.grafanaElementKey', setProperty: 'grafanaElement' }, changeTrackingProps = { fogInstanceId: 'bodyParams.instanceId', changeObject: { containerConfig: new Date().getTime(), containerList: new Date().getTime() } }; _async2.default.waterfall([_async2.default.apply(_elementService2.default.getNetworkElement, elementProps, params), createGrafanaElementInstance, _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, changeTrackingProps)], function (err, result) { if (!err) { callback(null, params); } else { callback('Error', result); } }); } else { var elementInstanceProps = { instanceId: 'bodyParams.instanceId', elementKey: 'fogTypeData.grafanaElementKey' }; _elementInstanceService2.default.deleteElementInstancesByInstanceIdAndElementKey(elementInstanceProps, params, callback); } } else { callback(null, params); } }; var mongoForFog = function mongoForFog(params, callback) { if (params.bodyParams.mongo != params.fogInstance.mongo) { params.isMongo = 1; if (params.bodyParams.mongo > 0) { var elementProps = { networkElementId: 'fogTypeData.mongoElementKey', setProperty: 'mongoElement' }, mongoPortProps = { userId: 'user.id', internalPort: 27017, externalPort: 27017, elementId: 'mongoElementInstance.uuid', setProperty: 'mongodbPort' }, elementInstanceProps = { updatedData: { last_updated: new Date().getTime(), updatedBy: params.user.id }, elementId: 'mongoElementInstance.uuid' }, changeTrackingProps = { fogInstanceId: 'bodyParams.instanceId', changeObject: { containerConfig: new Date().getTime(), containerList: new Date().getTime() } }; _async2.default.waterfall([_async2.default.apply(_elementService2.default.getNetworkElement, elementProps, params), createMongoElementInstance, _async2.default.apply(_elementInstancePortService2.default.createElementInstancePortByPortValue, mongoPortProps), _async2.default.apply(_elementInstanceService2.default.updateElemInstance, elementInstanceProps), _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, changeTrackingProps)], function (err, result) { if (!err) { callback(null, params); } else { callback('Error', result); } }); } else { var _elementInstanceProps3 = { instanceId: 'bodyParams.instanceId', elementKey: 'fogTypeData.mongoElementKey' }; _elementInstanceService2.default.deleteElementInstancesByInstanceIdAndElementKey(_elementInstanceProps3, params, callback); } } else { callback(null, params); } }; var influxForFog = function influxForFog(params, callback) { if (params.bodyParams.influx != params.fogInstance.influx) { params.isInflux = 1; if (params.bodyParams.influx > 0) { var elementProps = { networkElementId: 'fogTypeData.influxElementKey', setProperty: 'influxElement' }, influxPortProps = { userId: 'user.id', internalPort: 8086, externalPort: 8086, elementId: 'influxElementInstance.uuid', setProperty: 'influxdbPort' }, elementInstanceProps = { updatedData: { last_updated: new Date().getTime(), updatedBy: params.user.id }, elementId: 'influxElementInstance.uuid' }, changeTrackingProps = { fogInstanceId: 'bodyParams.instanceId', changeObject: { containerConfig: new Date().getTime(), containerList: new Date().getTime() } }; _async2.default.waterfall([_async2.default.apply(_elementService2.default.getNetworkElement, elementProps, params), createInfluxElementInstance, _async2.default.apply(_elementInstancePortService2.default.createElementInstancePortByPortValue, influxPortProps), _async2.default.apply(_elementInstanceService2.default.updateElemInstance, elementInstanceProps), _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, changeTrackingProps)], function (err, result) { if (!err) { callback(null, params); } else { callback('Error', result); } }); } else { var _elementInstanceProps4 = { instanceId: 'bodyParams.instanceId', elementKey: 'fogTypeData.influxElementKey' }; _elementInstanceService2.default.deleteElementInstancesByInstanceIdAndElementKey(_elementInstanceProps4, params, callback); } } else { callback(null, params); } }; var bluetoothElementForFog = function bluetoothElementForFog(params, callback) { if (params.bodyParams.bluetooth != params.fogInstance.bluetooth) { params.isBluetooth = 1; if (params.bodyParams.bluetooth > 0) { var elementProps = { networkElementId: 'fogTypeData.bluetoothElementKey', setProperty: 'bluetoothElement' }, changeTrackingProps = { fogInstanceId: 'bodyParams.instanceId', changeObject: { containerConfig: new Date().getTime(), containerList: new Date().getTime() } }; _async2.default.waterfall([_async2.default.apply(_elementService2.default.getNetworkElement, elementProps, params), createBluetoothElementInstance, _async2.default.apply(_changeTrackingService2.default.updateChangeTracking, changeTrackingProps)], function (err, result) { if (!err) { callback(null, params); } else { callback('Error', result); } }); } else { var elementInstanceProps = { instanceId: 'bodyParams.instanceId', elementKey: 'fogTypeData.bluetoothElementKey' }; _elementInstanceService2.default.deleteElementInstancesByInstanceIdAndElementKey(elementInstanceProps, params, callback); } } else { callback(null, params); } }; var updateChangeTracking = function updateChangeTracking(params, callback) { var changeTrackingProps = { fogInstanceId: 'bodyParams.instanceId', changeObject: { containerList: new Date().getTime(), config: new Date().getTime(), reboot: params.bodyParams.reboot } }; _changeTrackingService2.default.updateChangeTracking(changeTrackingProps, params, callback); }; var createBluetoothElementInstance = function createBluetoothElementInstance(params, callback) { var elementInstanceProps = { elementInstance: { uuid: _appUtils2.default.generateInstanceId(32), trackId: 0, element_key: params.fogTypeData.bluetoothElementKey, config: '{}', configLastUpdated: new Date().getTime(), name: params.bluetoothElement.name, updatedBy: params.user.id, iofog_uuid: params.bodyParams.instanceId, isStreamViewer: false, isDebugConsole: false, isManager: false, isNetwork: false, rootHostAccess: true, logSize: 50, volumeMappings: '{"volumemappings": []}', registryId: params.bluetoothElement.registry_id, rebuild: false }, setProperty: 'newBluetoothElementInstance' }; _elementInstanceService2.default.createElementInstanceObj(elementInstanceProps, params, callback); }; var createHalElementInstance = function createHalElementInstance(params, callback) { var elementInstanceProps = { elementInstance: { uuid: _appUtils2.default.generateInstanceId(32), trackId: 0, element_key: params.fogTypeData.halElementKey, config: '{}', configLastUpdated: new Date().getTime(), name: params.halElement.name, updatedBy: params.user.id, iofog_uuid: params.bodyParams.instanceId, isStreamViewer: false, isDebugConsole: false, isManager: false, isNetwork: false, rootHostAccess: true, logSize: 50, volumeMappings: '{"volumemappings": []}', registryId: params.halElement.registry_id, rebuild: false }, setProperty: 'newHalElementInstance' }; _elementInstanceService2.default.createElementInstanceObj(elementInstanceProps, params, callback); }; var createGrafanaElementInstance = function createGrafanaElementInstance(params, callback) { var elementInstanceProps = { elementInstance: { uuid: _appUtils2.default.generateInstanceId(32), trackId: 0, element_key: params.fogTypeData.grafanaElementKey, config: '{}', configLastUpdated: new Date().getTime(), name: params.grafanaElement.name, updatedBy: params.user.id, iofog_uuid: params.bodyParams.instanceId, isStreamViewer: false, isDebugConsole: false, isManager: false, isNetwork: false, rootHostAccess: true, logSize: 50, volumeMappings: '{"volumemappings": []}', registryId: params.grafanaElement.registry_id, rebuild: false }, setProperty: 'grafanaElementInstance' }; _elementInstanceService2.default.createElementInstanceObj(elementInstanceProps, params, callback); }; var createMongoElementInstance = function createMongoElementInstance(params, callback) { var volumeMappings = { volumemappings: [{ hostdestination: "/var/lib/mongodb", containerdestination: "/data/db", accessmode: "rw" }] }; var elementInstanceProps = { elementInstance: { uuid: _appUtils2.default.generateInstanceId(32), trackId: 0, element_key: params.fogTypeData.mongoElementKey, config: '{}', configLastUpdated: new Date().getTime(), name: params.mongoElement.name, updatedBy: params.user.id, iofog_uuid: params.bodyParams.instanceId, isStreamViewer: false, isDebugConsole: false, isManager: false, isNetwork: false, rootHostAccess: false, logSize: 50, volumeMappings: JSON.stringify(volumeMappings), registryId: params.mongoElement.registry_id, rebuild: false }, setProperty: 'mongoElementInstance' }; _elementInstanceService2.default.createElementInstanceObj(elementInstanceProps, params, callback); }; var createInfluxElementInstance = function createInfluxElementInstance(params, callback) { var volumeMappings = { volumemappings: [{ hostdestination: "/var/lib/influxdb", containerdestination: "/var/lib/influxdb", accessmode: "rw" }] }; var elementInstanceProps = { elementInstance: { uuid: _appUtils2.default.generateInstanceId(32), trackId: 0, element_key: params.fogTypeData.influxElementKey, config: '{}', configLastUpdated: new Date().getTime(), name: params.influxElement.name, updatedBy: params.user.id, iofog_uuid: params.bodyParams.instanceId, isStreamViewer: false, isDebugConsole: false, isManager: false, isNetwork: false, rootHostAccess: false, logSize: 50, volumeMappings: JSON.stringify(volumeMappings), registryId: params.influxElement.registry_id, rebuild: false }, setProperty: 'influxElementInstance' }; _elementInstanceService2.default.createElementInstanceObj(elementInstanceProps, params, callback); }; /****************** Add Bluebox EndPoint (Post: /api/v2/authoring/fabric/instance/bluebox/add) ***************/ // const addBlueboxEndpoint = function (req, res){ // logger.info("Endpoint hit: "+ req.originalUrl); // let params = {}, // userProps = { // userId : 'bodyParams.t', // setProperty: 'user' // }, // generatedIdsProps = { // bbid: 'bodyParams.token', // setProperty:'tokenData' // }, // fogControllerProps = { // uuid: 'tokenData.controllerId', // setProperty: 'fogControllerData' // }, // instanceProps = { // fogId: 'tokenData.iofog_uuid', // setProperty: 'fogData' // }, // getTrackingProps = { // instanceId: 'tokenData.iofog_uuid', // setProperty: 'changeTrackingData' // }; // params.bodyParams = req.body; // logger.info("Parameters:" + JSON.stringify(params.bodyParams)); // async.waterfall([ // async.apply(UserService.getUser, userProps, params), // async.apply(GeneratedIdsService.findGeneratedIdsByBBID, generatedIdsProps), // async.apply(FogControllerService.findFogControllersByUUID, fogControllerProps), // validateFogInstance, // async.apply(FogService.getFogInstanceOptional, instanceProps), // createFogInstanceWithUUID, // updateGeneratedIdsByBBID, // async.apply(ChangeTrackingService.getChangeTrackingByInstanceId, getTrackingProps), // createChangeTracking, // ], // function(err, result) { // AppUtils.sendResponse(res, err, 'test', params, result); // }); // } // const createChangeTracking = function(params, callback){ // if(!params.changeTrackingData){ // let changeTrackingProps = { // fogInstanceId: 'tokenData.iofog_uuid', // setProperty: 'newChangeTracking' // }; // ChangeTrackingService.createFogChangeTracking(changeTrackingProps, params, callback); // }else{ // callback(null, params); // } // } // const createFogInstanceWithUUID = function(params, callback){ // if (!params.fogData){ // let fogProps = { // fogObj :{ // uuid: params.tokenData.iofog_uuid, // name: 'My BlueBox', // location: '', // latitude: '', // longitude: '', // description: '', // token: '', // typeKey: '2', // daemonstatus: 'Not provisioned', // daemonlaststart: 0, // lastactive: 0, // elementstatus: 0, // memoryviolation: 'no', // diskviolation: 'no', // cpuviolation: 'no', // repositorycount: 0, // repositorystatus: '', // systemtime: 0, // laststatustime: 0, // elementmessagecounts: '', // messagespeed: 0, // lastcommandtime: 0, // version: '' // }, // setProperty: 'blueBoxFogIntance' // }; // FogService.createFogInstanceWithUUID(fogProps, params, callback); // }else{ // callback('Error', 'Registration failed: Unable to create fog instance.') // } // } // const validateFogInstance = function(params, callback){ // if(params.tokenData.iofog_uuid){ // if(params.tokenData.iofog_uuid.length == 32){ // callback(null, params); // }else{ // callback('Error', 'Registration failed: Unable to create fog instance.') // } // }else{ // callback('Error', 'Registration failed: Unable to create fog instance.') // } // } // const updateGeneratedIdsByBBID = function(params, callback){ // let updateProps = { // updatedObj: { // email: params.user.email, // firstName: params.user.firstName, // lastName: params.user.lastName, // activated: 1 // }, // bbid: 'bodyParams.token' // }; // GeneratedIdsService.updateGeneratedIdsByBBID(updateProps, params, callback); // } exports.default = { // addBlueboxEndpoint: addBlueboxEndpoint, getFogDetailsEndpoint: getFogDetailsEndpoint, getFogControllerStatusEndPoint: getFogControllerStatusEndPoint, fogInstancesListEndPoint: fogInstancesListEndPoint, fogInstanceCreateEndPoint: fogInstanceCreateEndPoint, // getFogListEndPoint: getFogListEndPoint, getFogTypesEndPoint: getFogTypesEndPoint, fogInstanceDeleteEndPoint: fogInstanceDeleteEndPoint, integratorInstanceDeleteEndPoint: integratorInstanceDeleteEndPoint, updateFogSettingsEndpoint: updateFogSettingsEndpoint };