-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
332 lines (302 loc) · 12.8 KB
/
main.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
const fetch = require("fetch-retry")(global.fetch);
const util = require("util");
const Models = require("./lib/Model");
const statusReasons = require("./lib/statusReasons");
const parseString = require("xml2js").parseString;
const stripPrefix = require("xml2js").processors.stripPrefix;
const http = require("http");
/**
* Cisco RisPort Service
* This is a service class that uses fetch and promises to pull RisPort data from Cisco CUCM
*
*
* @class risPortService
*/
var XML_ENVELOPE = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.cisco.com/ast/soap">
<soapenv:Header/>
<soapenv:Body>
<soap:selectCmDevice>
<soap:StateInfo></soap:StateInfo>
<soap:CmSelectionCriteria>
<soap:MaxReturnedDevices>%s</soap:MaxReturnedDevices>
<soap:DeviceClass>%s</soap:DeviceClass>
<soap:Model>%s</soap:Model>
<soap:Status>%s</soap:Status>
<soap:NodeName>%s</soap:NodeName>
<soap:SelectBy>%s</soap:SelectBy>
<soap:SelectItems>%s</soap:SelectItems>
<soap:Protocol>%s</soap:Protocol>
<soap:DownloadStatus>%s</soap:DownloadStatus>
</soap:CmSelectionCriteria>
</soap:selectCmDevice>
</soapenv:Body>
</soapenv:Envelope>`;
var XML_EXT_ENVELOPE = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.cisco.com/ast/soap">
<soapenv:Header/>
<soapenv:Body>
<soap:selectCmDeviceExt>
<soap:StateInfo></soap:StateInfo>
<soap:CmSelectionCriteria>
<soap:MaxReturnedDevices>%s</soap:MaxReturnedDevices>
<soap:DeviceClass>%s</soap:DeviceClass>
<soap:Model>%s</soap:Model>
<soap:Status>%s</soap:Status>
<soap:NodeName>%s</soap:NodeName>
<soap:SelectBy>%s</soap:SelectBy>
<soap:SelectItems>%s</soap:SelectItems>
<soap:Protocol>%s</soap:Protocol>
<soap:DownloadStatus>%s</soap:DownloadStatus>
</soap:CmSelectionCriteria>
</soap:selectCmDeviceExt>
</soapenv:Body>
</soapenv:Envelope>`;
var XML_CTI_ENVELOPE = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.cisco.com/ast/soap">
<soapenv:Header/>
<soapenv:Body>
<soap:selectCtiItem>
<soap:StateInfo></soap:StateInfo>
<soap:CtiSelectionCriteria>
<soap:MaxReturnedItems>%s</soap:MaxReturnedItems>
<soap:CtiMgrClass>%s</soap:CtiMgrClass>
<soap:Status>%s</soap:Status>
<soap:NodeName>%s</soap:NodeName>
<soap:SelectAppBy>%s</soap:SelectAppBy>
<soap:AppItems>%s</soap:AppItems>
<soap:DevNames>%s</soap:DevNames>
<soap:DirNumbers>%s</soap:DirNumbers>
</soap:CtiSelectionCriteria>
</soap:selectCtiItem>
</soapenv:Body>
</soapenv:Envelope>`;
class risPortService {
constructor(host, username, password, options = {}, retry = true) {
this._OPTIONS = {
retryOn: async function (attempt, error, response) {
if (!retry) {
return false;
}
if (attempt > (process.env.RP_RETRY ? parseInt(process.env.RP_RETRY) : 3)) {
return false;
}
// retry on any network error, or 4xx or 5xx status codes
if (error !== null || response.status >= 400) {
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
await delay(process.env.RP_RETRY_DELAY ? parseInt(process.env.RP_RETRY_DELAY) : 5000);
return true;
}
},
method: "POST",
headers: {
Authorization: "Basic " + Buffer.from(username + ":" + password).toString("base64"),
"Content-Type": "text/xml;charset=UTF-8",
Connection: "keep-alive",
},
};
if (options) {
this._OPTIONS.headers = Object.assign(this._OPTIONS.headers, options);
}
this._HOST = host;
}
/**
* Post Fetch using Cisco RisPort API
*
* @selectCmDevice
var service = new risPortService();
service.selectCmDevice().then((success => {
console.log(success);
}))
* @memberof risPortService
* @param {('SelectCmDevice'|'SelectCmDeviceExt')} soapAction - The soap action to use
* @param {number} maxReturnedDevices - The maximum number of devices to return. The maximum parameter value is 2000.
* @param {('Any'|'Phone'|'Gateway'|'H323'|'Cti'|'VoiceMail'|'MediaResources'|'HuntList'|'SIPTrunk'|'Unknown')} deviceclass - The device class to query for real-time status.
* @param {string|number} model - The model to search for or 255 for "any model". Alternatively, you can use the model name.
* @param {('Any'|'Registered'|'UnRegistered'|'Rejected'|'PartiallyRegistered'|'Unknown')} status - The status to search for. If you do not specify a status, the system returns all devices that match the other criteria.
* @param {string} node - The UC Manager node name to query. If no NodeName is given, all nodes in the cluster are queried.
* @param {('Name'|'IPV4Address'|'IPV6Address'|'DirNumber'|'Description'|'SIPStatus')} selectBy - The select by to search for. If you do not specify a select by, the system returns all devices that match the other criteria.
* @param {string|array} selectItem - An array of one or more item elements, which may include names, IP addresses, or directory numbers, depending on the SelectBy parameter. The item value can include a * to return wildcard matches. You can also pass a single item.
* @param {('Any'|'SCCP'|'SIP'|'Unknown')} protocol - The protocol to search for. If you do not specify a protocol, the system returns all devices that match the other criteria.
* @param {('Any'|'Upgrading'|'Successful'|'Failed'|'Unknown')} downloadStatus - The download status to search for. If you do not specify a download status, the system returns all devices that match the other criteria.
* @returns {object} returns a object with cookie and results
*/
async selectCmDevice(soapAction, maxReturnedDevices, deviceclass, model, status, node, selectBy, selectItem, protocol, downloadStatus) {
try {
let options = this._OPTIONS;
let host = this._HOST;
options.SOAPAction = `http://schemas.cisco.com/ast/soap/action/#RisPort#${soapAction}`;
let itemStr;
let XML;
if (Array.isArray(selectItem)) {
itemStr = selectItem.map((phoneName) => "<soap:item>" + "<soap:Item>" + phoneName + "</soap:Item>" + "</soap:item>");
} else {
itemStr = "<soap:item>" + "<soap:Item>" + selectItem + "</soap:Item>" + "</soap:item>";
}
// Let's check if the user gave us a numeric value. If not let's convert to model enum. If not found set to "Any".
if (!Number.isInteger(model)) {
model = Object.keys(Models).find((key) => Models[key] === model);
if (!model) {
model = 255;
}
}
if (soapAction === "SelectCmDeviceExt") {
XML = util.format(XML_EXT_ENVELOPE, maxReturnedDevices, deviceclass, model, status, node, selectBy, itemStr, protocol, downloadStatus);
} else {
XML = util.format(XML_ENVELOPE, maxReturnedDevices, deviceclass, model, status, node, selectBy, itemStr, protocol, downloadStatus);
}
let soapBody = Buffer.from(XML);
options.body = soapBody;
let response = await fetch(`https://${host}:8443/realtimeservice2/services/RISService70`, options);
let promiseResults = {
cookie: "",
results: "",
};
promiseResults.cookie = response.headers.get("set-cookie") ? response.headers.get("set-cookie") : "";
let output = await parseXml(await response.text());
// Remove unnecessary keys
removeKeys(output, "$");
if (!response.ok) {
// Local throw; if it weren't, I'd use Error or a subclass
throw { status: response.status, code: http.STATUS_CODES[response.status], message: output?.Body?.Fault?.faultstring ? output.Body.Fault.faultstring : "Unknown" };
}
if (output?.Body?.selectCmDeviceResponse?.selectCmDeviceReturn) {
let returnResults = output?.Body?.selectCmDeviceResponse?.selectCmDeviceReturn?.SelectCmDeviceResult?.CmNodes?.item;
promiseResults.results = (returnResults ? clean(returnResults) : "");
return promiseResults;
} else {
return promiseResults;
}
} catch (error) {
throw error;
}
}
/**
* Post Fetch using Cisco RisPort API
*
* @selectCtiDevice
var service = new risPortService();
service.selectCtiDevice().then((success => {
console.log(success);
}))
* @memberof risPortService
* @returns {object} returns a object with cookie and results
*/
async selectCtiDevice(maxReturnedDevices, ctiMgrClass, status, node, selectAppBy, appItem, devName, dirNumber) {
try {
let appItemsStr;
let devNamesStr;
let dirNumbersStr;
let options = this._OPTIONS;
let XML;
let host = this._HOST;
options.SOAPAction = `http://schemas.cisco.com/ast/soap/action/#RisPort#SelectCtiItem`;
if (Array.isArray(appItem)) {
appItemsStr = appItem.map((item) => "<soap:item>" + "<soap:AppItem>" + item + "</soap:AppItem>" + "</soap:item>");
} else {
appItemsStr = "<soap:item>" + "<soap:AppItem>" + appItem + "</soap:AppItem>" + "</soap:item>";
}
if (Array.isArray(devName)) {
devNamesStr = appItem.map((item) => "<soap:item>" + "<soap:DevName>" + item + "</soap:DevName>" + "</soap:item>");
} else {
devNamesStr = "<soap:item>" + "<soap:DevName>" + devName + "</soap:DevName>" + "</soap:item>";
}
if (Array.isArray(dirNumber)) {
dirNumbersStr = dirNumber.map((item) => "<soap:item>" + "<soap:DirNumber>" + item + "</soap:DirNumber>" + "</soap:item>");
} else {
dirNumbersStr = "<soap:item>" + "<soap:DirNumber>" + dirNumber + "</soap:DirNumber>" + "</soap:item>";
}
XML = util.format(XML_CTI_ENVELOPE, maxReturnedDevices, ctiMgrClass, status, node, selectAppBy, appItemsStr, devNamesStr, dirNumbersStr);
let soapBody = Buffer.from(XML);
options.body = soapBody;
let response = await fetch(`https://${host}:8443/realtimeservice2/services/RISService70`, options);
let promiseResults = {
cookie: "",
results: "",
};
promiseResults.cookie = response.headers.get("set-cookie") ? response.headers.get("set-cookie") : "";
let output = await parseXml(await response.text());
// Remove unnecessary keys
removeKeys(output, "$");
if (!response.ok) {
// Local throw; if it weren't, I'd use Error or a subclass
throw { status: response.status, code: http.STATUS_CODES[response.status], sessionId: SessionHandle, message: output?.Body?.Fault?.faultstring ? output.Body.Fault.faultstring : "Unknown" };
}
if (output?.Body?.selectCtiItemResponse?.selectCtiItemReturn) {
let returnResults = output?.Body?.selectCtiItemResponse?.selectCtiItemReturn?.SelectCtiItemResult?.CtiNodes?.item;
promiseResults.results = (returnResults ? clean(returnResults) : "");
return promiseResults;
} else {
return promiseResults;
}
} catch (error) {
throw error;
}
}
returnModels() {
return Models;
}
returnStatusReasons() {
return statusReasons;
}
}
/**
* Remove all specified keys from an object, no matter how deep they are.
* The removal is done in place, so run it on a copy if you don't want to modify the original object.
* This function has no limit so circular objects will probably crash the browser
*
* @param obj The object from where you want to remove the keys
* @param keys An array of property names (strings) to remove
*/
const removeKeys = (obj, keys) => {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
switch (typeof obj[prop]) {
case "object":
if (keys.indexOf(prop) > -1) {
delete obj[prop];
} else {
removeKeys(obj[prop], keys);
}
break;
default:
if (keys.indexOf(prop) > -1) {
delete obj[prop];
}
break;
}
}
}
};
const clean = (object) => {
Object.entries(object).forEach(([k, v]) => {
if (v && typeof v === "object") {
clean(v);
}
if ((v && typeof v === "object" && !Object.keys(v).length) || v === null || v === undefined) {
if (Array.isArray(object)) {
object.splice(k, 1);
} else {
delete object[k];
}
}
});
return object;
};
const parseXml = (xmlPart) => {
return new Promise((resolve, reject) => {
parseString(
xmlPart,
{
explicitArray: false,
explicitRoot: false,
tagNameProcessors: [stripPrefix],
},
(err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
}
);
});
};
module.exports = risPortService;