ale-ov2500-node
Version:
Node to consume REST API of the Alcatel-Lucent Enterprise OV2500 Network Manager and Cirrus Cloud Manager
643 lines (608 loc) • 21.5 kB
JavaScript
const request = require('request');
var diff = require('deep-diff').diff;
const moment = require('moment-timezone');
const Rx = require('rxjs');
class OV {
constructor(ip) {
this.token = '';
this.cookie = '';
this.ip = ip;
this.info = "";
this.mode = "";
this.url = 'https://' + this.ip;
}
get headers() {
return {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Ov-App-Version': '4.4R2',
'Cookie': this.cookie
}
}
login(data) {
return new Promise((resolve, reject) => {
request.post({
url: this.url + '/api/login',
headers: this.headers,
rejectUnauthorized: false,
body: JSON.stringify({
userName: data.login.username,
password: data.login.password,
}),
}, (err, response, body) => {
if (err) {
this.token = "";
return reject(err);
}
this.token = body.accessToken || "";
this.cookie = response.headers['set-cookie'];
this.about().then((info) => {
this.info = info;
//console.log(info);
let x = info.productName;
if (x.indexOf("Cirrus") !== -1) {
this.mode = "CIRRUS"
} else {
this.mode = "OV"
}
resolve(info);
})
});
});
}
about() {
return new Promise((resolve, reject) => {
request.get({
url: this.url + '/api/about',
headers: this.headers,
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = JSON.parse(body);
let aboutInfo = result;
resolve(aboutInfo);
} catch (err) {
reject("JSON Error")
}
});
})
}
getDevices() {
// devices?fieldSetName=discovery
return new Promise((resolve, reject) => {
request.get({
url: this.url + '/api/devices?fieldSetName=discovery',
headers: this.headers,
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let devices = result.response;
resolve(devices);
} catch (err) {
reject("JSON Error")
}
});
})
}
getPorts(instanceidArray) {
// devices?fieldSetName=discovery
return new Promise((resolve, reject) => {
request.post({
url: this.url + '/api/port/ports',
headers: this.headers,
body: instanceidArray,
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let devices = result.response;
resolve(devices);
} catch (err) {
reject("JSON Error")
}
});
})
}
getWlanClientList(mode) {
switch (mode) {
case "OV":
return new Promise((resolve, reject) => {
request.post({
url: this.url + '/api/wma/onlineClient/getOnlineClientList',
headers: this.headers,
body: JSON.stringify(new OV_WlanClientList()),
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let clients = result.data;
resolve(clients);
} catch (err) {
reject("JSON Error")
}
});
});
case "CIRRUS":
return new Promise((resolve, reject) => {
request.get({
url: this.url + '/api/wma/onlineClient/getOnlineClientList',
headers: this.headers,
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let clients = result.data;
resolve(clients);
} catch (err) {
reject("JSON Error")
}
});
});
}
}
//api/wma/wips/APlist/interfer
getWlanAPIntrusionList() {
return new Promise((resolve, reject) => {
request.get({
url: this.url + '/api/wma/wips/APlist/interfer',
headers: this.headers,
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let clients = result.data;
resolve(clients);
} catch (err) {
reject("JSON Error")
}
});
});
}
//api/ham/radius/authRecord/getAuthRecordList
getAuthRecordList() {
return new Promise((resolve, reject) => {
request.get({
url: this.url + '/api/ham/radius/authRecord/getAuthRecordList',
headers: this.headers,
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let clients = result.data;
resolve(clients);
} catch (err) {
reject("JSON Error")
}
});
});
}
//post api/ham/userAccount/addUser
addAccount(usr, pwd, profile, policy) {
return new Promise((resolve, reject) => {
request.post({
url: this.url + '/api/ham/userAccount/addUser',
headers: this.headers,
body: JSON.stringify(new OV_AddUser(usr, pwd, profile, policy)),
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let accts = result.response;
resolve(accts);
} catch (err) {
reject("JSON Error")
}
});
});
}
//get api/ham/userAccount/getAllAccountList
getAllAccountList() {
return new Promise((resolve, reject) => {
request.get({
url: this.url + '/api/ham/userAccount/getAllAccountList',
headers: this.headers,
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let clients = result.data;
resolve(clients);
} catch (err) {
reject("JSON Error")
}
});
});
}
//get api/ag/uatemplate/AccessRoleProfile
getAllAccessRoleProfileList() {
return new Promise((resolve, reject) => {
request.get({
url: this.url + '/api/ag/uatemplate/AccessRoleProfile',
headers: this.headers,
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let clients = result.response;
resolve(clients);
} catch (err) {
reject("JSON Error")
}
});
});
}
//post api/ag/uatemplate/assign/AccessRoleProfile
/*
UnifiedProfileAssignRequestObject {…}
deviceRequests {…}
0 {…}
configInfo {…}
locationPolicy <Inline Policy>
mappingType Vlan
periodPolicy <Inline Policy>
vlanNumber 24
deviceId 5ab501e2e4b02de9afc32211
deviceType AP_GROUP
documentIds {…}
0 5aba0c13e4b0f9907d47ea57
*/
// post api/ag/deviceconfig/vlan/devices
getDeviceConfig(vlan) {
return new Promise((resolve, reject) => {
request.post({
url: this.url + '/api/ag/deviceconfig/vlan/devices',
headers: this.headers,
body: JSON.stringify(new OV_DeviceConfig(vlan)),
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let devices = result.response;
resolve(devices);
} catch (err) {
reject("JSON Error")
}
});
});
}
sendScriptingfile(scriptName,ipAddress, instanceId){
return new Promise((resolve, reject) => {
console.log("sendScript",JSON.stringify(new OV_SendScript(scriptName,ipAddress, instanceId)));
request.post({
url: this.url + '/api/telnet/sendscriptingfile',
headers: this.headers,
body: JSON.stringify(new OV_SendScript(scriptName,ipAddress, instanceId)),
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let accts = result.response;
resolve(accts);
} catch (err) {
reject("JSON Error")
}
});
});
}
getNotifications(ip) {
return new Promise((resolve, reject) => {
request.post({
url: this.url + '/api/notifications/alarms',
headers: this.headers,
body: JSON.stringify(new OV_Notifications(ip, 100)),
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let traps = result.response.trapData;
resolve(traps);
} catch (err) {
reject("JSON Error")
}
});
});
}
getNotificationsWithRetry(ip) {
return Rx.Observable.fromPromise(new Promise((resolve, reject) => {
request.post({
url: this.url + '/api/notifications/alarms',
headers: this.headers,
body: JSON.stringify(new OV_Notifications(ip, 100)),
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let traps = result.response.trapData;
if (traps.length === 0) {
return reject("No notifications");
}
if (traps.length > 1) traps.sort((a, b) => {
return a.instanceId - b.instanceId
});
resolve(traps);
} catch (err) {
return reject("JSON Error")
}
});
})
).retry(3)
}
getNotificationsWithRetryByDevice(mac, instanceId) {
return Rx.Observable.fromPromise(new Promise((resolve, reject) => {
request.post({
url: this.url + '/api/notifications/alarms',
headers: this.headers,
body: JSON.stringify(new OV_Notifications2(mac, instanceId, 100)),
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
let result = this.result2JSON(body)[0];
let traps = result.response.trapData;
if (traps.length === 0) {
return reject("No notifications");
}
if (traps.length > 1) traps.sort((a, b) => {
return a.instanceId - b.instanceId
});
resolve(traps);
} catch (err) {
return reject("JSON Error")
}
});
})
).retry(3)
}
getAllNotificationsWithRetry() {
return Rx.Observable.fromPromise(new Promise((resolve, reject) => {
request.post({
url: this.url + '/api/notifications/alarms',
headers: this.headers,
body: JSON.stringify(new OV_Notifications3(100)),
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
try {
//console.log(body);
let result = this.result2JSON(body)[0];
let traps = result.response.trapData;
if (traps.length === 0) {
return reject("No notifications");
}
if (traps.length > 1) traps.sort((a, b) => {
return a.instanceId - b.instanceId
});
resolve(traps);
} catch (err) {
return reject("JSON Error")
}
});
})
).retry(3)
}
getIotInventoryWithRetry() {
return Rx.Observable.fromPromise(new Promise((resolve, reject) => {
request.post({
url: this.url + '/api/iot/inventory',
headers: this.headers,
body: JSON.stringify(new OV_getIotInventory()),
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
console.log((body));
try {
let result = this.result2JSON(body)[0];
let macs = result.response;
resolve(macs);
} catch (err) {
reject("getIotInventoryWithRetry JSON Error")
}
});
})
).retry(3)
}
getMacAddresses(ip) {
return new Promise((resolve, reject) => {
let ipList = "";
ip.forEach((addr, idx) => {
ipList = ipList + addr + (idx !== ip.length - 1 ? "," : "")
});
request.post({
url: this.url + '/api/locator/browse',
headers: this.headers,
body: [ipList],
rejectUnauthorized: false,
}, (err, response, body) => {
if (err) return reject(err);
let result = this.result2JSON(body);
let entries_f = result.filter((entry, idx) => {
return (entry.type === "LocatorFwdResponseObject");
});
let macs = [];
entries_f.forEach(entry => {
entry.response.ovResponseObject.forEach(port => {
macs.push(port);
});
});
resolve(macs);
});
});
}
result2JSON(body) {
let x = body.split("}{");
if (x.length < 2) return [JSON.parse(body)];
x.forEach((bdy, idx) => {
switch (idx) {
case 0:
x[idx] = JSON.parse(bdy + '}');
break;
case x.length - 1:
x[idx] = JSON.parse('{' + bdy);
break;
default:
x[idx] = JSON.parse('{' + bdy + '}');
}
});
return x;
}
}
module.exports = {
OV: OV
};
class OV_Notifications {
constructor(ip, num) {
this.ack = 'FALSE';
this.apGroups = null;
this.count = num;
this.filterBy = 'IP';
this.ips = ip;
this.mapIds = null;
this.name = null;
this.request = null;
this.sessionKey = 0;
this.severities = null;
this.timeRange = null;
this.type = 'GetTrapDataRequestObject';
this.uniqueRequestId = 0;
}
}
class OV_Notifications2 {
constructor(mac, id, num) {
this.ack = 'FALSE';
this.apGroups = null;
this.count = num;
this.filterBy = 'MAC';
this.deviceMap = {mac, id};
this.ips = null;
this.mapIds = null;
this.name = null;
this.request = null;
this.sessionKey = 0;
this.severities = null;
this.timeRange = null;
this.type = 'GetTrapDataRequestObject';
this.uniqueRequestId = 0;
}
}
class OV_Notifications3 {
constructor(num) {
/*this.ack = "FALSE";
this.apGroups = null;
this.count = num;
this.filterBy = "ALL";
this.deviceMap = null;
this.mapIds = null;
this.name = null;
this.request = null;
this.sessionKey = 0;
this.severities = null;
this.timeRange = null;
this.trapNames = [];
this.type = "GetTrapDataRequestObject";
this.uniqueRequestId = 0;*/
this.type = "GetTrapDataRequestObject";
this.uniqueRequestId = 0;
this.sessionKey = 0;
this.name = null;
this.request = null;
this.filterBy = "ALL";
this.mapIds = null;
this.deviceMap = null;
this.apGroups = null;
this.severities = null;
this.ack = "FALSE";
this.count = 100;
this.timeRange = null;
this.pageNumber = 0;
this.pageSize = 100;
this.trapNames = null;
}
}
class OV_WlanClientList {
constructor() {
this.filterBy = "all";
this.filterContent = "";
this.startMac = "";
}
}
class OV_DeviceConfig {
constructor(vlan) {
this.AGRequestObject = {
objectType: 'AccessRoleProfile',
others: {
deviceType: "AP_GROUP",
vlanNumber: vlan
}
}
}
}
class OV_AddUser {
constructor(usr, pwd, profile, policy) {
this.accessRoleProfile = profile;
this.password = pwd;
this.policyList = policy;
this.repeat = pwd;
this.username = usr;
}
}
class OV_SendScript {
constructor(scriptName, ipAddress, deviceId) {
this.TelnetDeviceRequestObject=
{
requestType:"sendscript",
objectType:"ScriptingFile",
valueObjects:[{
ScriptingFileVO: {
fileName: scriptName,
shared: false,
params: {}
}
}],
devices:[{
TelnetDeviceVO: {
//device: {ipAddress: ipAddress},
deviceId: deviceId,
hasAuthen: true,
username: "",
password: "",
hasSecondPassword: false,
secondPassword: null
}
}]
}
}
}
class OV_getIotInventory {
constructor() {
this.type = "IotInventoryRequest";
this.endpointStatus = "All";
this.filterBy = "ALL";
this.macAddresses = [];
this.categories = [];
this.unps = [];
this.ssids = [];
this.deviceMap = {};
this.apGroups = null;
this.mapIds = null;
this.timeRange = null;
this.pageSize = 1000;
this.pageNumber = 1;
this.debugMode = false;
this.latestRecords = true;
}
}