-
Notifications
You must be signed in to change notification settings - Fork 0
/
calix.js
365 lines (335 loc) · 14.8 KB
/
calix.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
const Q = require('q')
const unirest = require('unirest');
const xml2js = require('xml2js');
const parser = xml2js.Parser();
var ourSession = 0
const url = 'http://172.16.3.254:18080/cmsexc/ex/netconf'
const authreply = "auth-reply"
const args = process.argv.splice(process.execArgv.length + 2);
const xmlLogin = function () {
return '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' + '\n' +
'<soapenv:Body><auth message-id="1"><login>' + '\n' +
'<UserName>rootgod</UserName><Password>root</Password>' + '\n' +
'</login></auth></soapenv:Body></soapenv:Envelope>'
}
const xmlLogout = function (session) {
return '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' + '\n' +
'<soapenv:Body><auth message-id="5"><logout>' + '\n' +
'<UserName>rootgod</UserName><SessionId>' + session + '</SessionId>' + '\n' +
'</logout></auth></soapenv:Body></soapenv:Envelope>'
}
const xmlCREATEONT = function (node, session, serial) {
return '<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body>' + '\n' +
'<rpc message-id="2" nodename="' + node + '" timeout="35000" username="rootgod" sessionid="' + session + '">' + '\n' +
'<edit-config><target><running/></target><config><top><object operation="create" get-config="true">' + '\n' +
'<type>Ont</type><id><ont>0</ont></id><admin>enabled</admin>' + '\n' +
'<serno>' + serial + '</serno>' + '\n' +
'<reg-id></reg-id><subscr-id></subscr-id><descr></descr><ontprof><type>OntProf</type><id>' + '\n' +
'<ontprof>157</ontprof>' + '\n' +
'</id></ontprof></object></top></config></edit-config></rpc></soapenv:Body></soapenv:Envelope>'
}
const xmlGETONT = function (node, session, serial) {
return '<soapenv:Envelope xmlns:soapenv="www.w3.org/2003/05/soap-envelope"><soapenv:Body>' + '\n' +
'<rpc message-id="2" nodename="' + node + '" username="rootgod" sessionid="' + session + '">' + '\n' +
'<action><action-type>show-ont</action-type><action-args><serno>' + serial + '</serno></action-args></action></rpc></soapenv:Body></soapenv:Envelope>'
}
const xmlGETPROF = function (node, session, ont) {
return `<soapenv:Envelope xmlns:soapenv="www.w3.org/2003/05/soap-envelope"><soapenv:Body>
<rpc message-id="3" nodename="${node}" username="rootgod" sessionid="${session}">
<get-config>
<source>
<running/>
</source>
<filter type="subtree">
<top>
<object>
<type>Ont</type>
<id><ont>${ont}</ont></id>
<children>
<type>OntRg</type>
<attr-list>pppoe-user pppoe-password</attr-list>
</children>
</object>
</top>
</filter>
</get-config>
</rpc>
</soapenv:Body>
</soapenv:Envelope>`
}
const xmlPROVDS = function (node, session, ont) {
return '<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">' + '\n' +
'<soapenv:Body><rpc message-id="3" nodename="' + node + '" timeout="35000" username="rootgod" sessionid="' + session + '">' + '\n' +
'<edit-config><target><running/></target><config><top><object operation="create" get-config="true"><type>EthSvc</type><id>' + '\n' +
'<ont>' + ont + '</ont>' + '\n' +
'<ontslot>8</ontslot><ontethany>1</ontethany><ethsvc>1</ethsvc></id><admin>enabled</admin>' + '\n' +
'<tag-action><type>SvcTagAction</type><id><svctagaction>2</svctagaction></id></tag-action>' + '\n' +
'<bw-prof><type>BwProf</type><id><bwprof>1</bwprof></id></bw-prof>' + '\n' +
'</object></top></config></edit-config></rpc></soapenv:Body></soapenv:Envelope>'
}
const xmlPPPOE = function (node, session, ont, user, pass) {
return '<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">' + '\n' +
'<soapenv:Body><rpc message-id="4" nodename="' + node + '" timeout="35000" username="rootgod" sessionid="' + session + '">' + '\n' +
'<edit-config><target><running/></target><config><top><object operation="merge"><type>OntRg</type><id>' + '\n' +
'<ont>' + ont + '</ont>' + '\n' +
'<ontslot>8</ontslot><ontrg>1</ontrg></id><admin>enabled</admin><mgmt-mode>native</mgmt-mode><wan-protocol>pppoe</wan-protocol>' + '\n' +
'<pppoe-user>' + user + '</pppoe-user><pppoe-password>' + pass + '</pppoe-password>' + '\n' +
'</object></top></config></edit-config></rpc></soapenv:Body></soapenv:Envelope>'
}
function restLOGIN(XML) {
var deferred = Q.defer()
unirest.post(url).headers({
'Content-Type': 'application/json;charset=UTF-8'
}).send(XML).end(function (response) {
let str = response.body.replace(/:/g, '').replace(/-/g, '')
parser.parseString(str, function (err, result) {
if (result.Envelope.Body[0].authreply[0].ResultCode[0] == '0') {
deferred.resolve(result.Envelope.Body[0].authreply[0].SessionId[0])
} else {
deferred.reject(result.Envelope.Body[0].authreply[0].ResultCode[0])
}
})
})
return deferred.promise
}
function restLOGOUT(XML) {
var deferred = Q.defer()
unirest.post(url).headers({
'Content-Type': 'application/json;charset=UTF-8'
}).send(XML).end(function (response) {
let str = response.body.replace(/:/g, '').replace(/-/g, '')
parser.parseString(str, function (err, result) {
if (result.Envelope.Body[0].authreply[0].ResultCode[0] == '0') {
deferred.resolve(result.Envelope.Body[0].authreply[0].SessionId[0])
} else {
deferred.reject(result.Envelope.Body[0].authreply[0].ResultCode[0])
}
})
})
return deferred.promise
}
function restCREATE(XML) {
var deferred = Q.defer()
unirest.post(url).headers({
'Content-Type': 'application/json;charset=UTF-8'
}).send(XML).end(function (response) {
let str = response.body.replace(/soapenv:/gi, '').replace(/-/gi, '')
parser.parseString(str, function (err, result) {
// console.log(JSON.stringify(result, null, 2))
if (result.Envelope.Body[0].rpcreply[0].rpcerror !== undefined) {
deferred.reject(result.Envelope.Body[0].rpcreply[0].rpcerror[0].errormessage[0]._)
} else {
deferred.resolve(JSON.stringify(result.Envelope.Body[0].rpcreply[0].data[0].top[0].object[0].id[0].ont[0]))
}
})
})
return deferred.promise
}
function restGET(XML, consolelog) {
var deferred = Q.defer()
unirest.post(url).headers({
'Content-Type': 'application/json;charset=UTF-8'
}).send(XML).end(function (response) {
let str = response.body.replace(/soapenv:/gi, '').replace(/-/gi, '')
parser.parseString(str, function (err, result) {
//console.log(JSON.stringify(result, null, 2))
if (result.Envelope.Body[0].rpcreply[0].rpcerror !== undefined) {
deferred.reject(result.Envelope.Body[0].rpcreply[0].rpcerror[0].errormessage[0]._)
} else {
if (consolelog === true) {
console.log()
console.log(' ONT ID: ' + result.Envelope.Body[0].rpcreply[0].actionreply[0].match[0].get[0].object[0].id[0].ont[0])
console.log(' Unit Status: ' + result.Envelope.Body[0].rpcreply[0].actionreply[0].match[0].get[0].object[0].opstat[0])
console.log(' SW Version: ' + result.Envelope.Body[0].rpcreply[0].actionreply[0].match[0].get[0].object[0].currswvers[0])
console.log(' Uptime: ' + result.Envelope.Body[0].rpcreply[0].actionreply[0].match[0].get[0].object[0].uptime[0])
console.log(' ONU Signal: -' + result.Envelope.Body[0].rpcreply[0].actionreply[0].match[0].get[0].object[0].optsiglvl[0])
console.log(' OLT Signal: -' + result.Envelope.Body[0].rpcreply[0].actionreply[0].match[0].get[0].object[0].feoptlvl[0])
console.log(' Range: ' + result.Envelope.Body[0].rpcreply[0].actionreply[0].match[0].get[0].object[0].rangelength[0] + ' meters')
}
deferred.resolve(result.Envelope.Body[0].rpcreply[0].actionreply[0].match[0].get[0].object[0].id[0].ont[0])
}
})
})
return deferred.promise
}
function restGETPROF(node, session, ont, consolelog) {
var deferred = Q.defer()
unirest.post(url).headers({
'Content-Type': 'application/json;charset=UTF-8'
}).send(xmlGETPROF(node, session, ont)).end(function (response) {
let str = response.body.replace(/soapenv:/gi, '').replace(/-/gi, '')
//console.log(xmlGETPROF(node, session, ont))
parser.parseString(str, function (err, result) {
// console.log(JSON.stringify(result, null, 2))
if (result.Envelope.Body[0].rpcreply[0].rpcerror !== undefined) {
deferred.reject(result.Envelope.Body[0].rpcreply[0].rpcerror[0].errormessage[0]._)
} else {
flat = JSON.flatten(result)
if (consolelog === true) {
console.log(" PPPoE Username: " + flat["Envelope.Body.0.rpcreply.0.data.0.top.0.object.0.children.0.child.0.pppoeuser.0"])
} else {
console.log(flat["Envelope.Body.0.rpcreply.0.data.0.top.0.object.0.children.0.child.0.pppoeuser.0"])
}
deferred.resolve(flat["Envelope.Body.0.rpcreply.0.data.0.top.0.object.0.id.0.ont.0"])
}
})
})
return deferred.promise
}
JSON.flatten = function(data) {
var result = {};
function recurse (cur, prop) {
if (Object(cur) !== cur) {
result[prop] = cur;
} else if (Array.isArray(cur)) {
for(var i=0, l=cur.length; i<l; i++)
recurse(cur[i], prop ? prop+"."+i : ""+i);
if (l == 0)
result[prop] = [];
} else {
var isEmpty = true;
for (var p in cur) {
isEmpty = false;
recurse(cur[p], prop ? prop+"."+p : p);
}
if (isEmpty)
result[prop] = {};
}
}
recurse(data, "");
return result;
}
function restPROVDS(XML) {
var deferred = Q.defer()
unirest.post(url).headers({
'Content-Type': 'application/json;charset=UTF-8'
}).send(XML).end(function (response) {
let str = response.body.replace(/soapenv:/gi, '').replace(/-/gi, '')
parser.parseString(str, function (err, result) {
if (result.Envelope.Body[0].rpcreply[0].rpcerror !== undefined) {
deferred.reject(result.Envelope.Body[0].rpcreply[0].rpcerror[0].errormessage[0]._)
} else {
deferred.resolve(JSON.stringify(result.Envelope.Body[0].rpcreply[0].data[0].top[0].object[0].id[0].ont[0]))
}
})
})
return deferred.promise
}
function restPPPOE(XML) {
var deferred = Q.defer()
unirest.post(url).headers({
'Content-Type': 'application/json;charset=UTF-8'
}).send(XML).end(function (response) {
let str = response.body.replace(/soapenv:/gi, '').replace(/-/gi, '')
parser.parseString(str, function (err, result) {
if (result.Envelope.Body[0].rpcreply[0].rpcerror !== undefined) {
deferred.reject(result.Envelope.Body[0].rpcreply[0].rpcerror[0].errormessage[0]._)
} else {
deferred.resolve(true)
}
})
})
return deferred.promise
}
//examples
let confNode = 'NTWK-CUPECOY-GPON1'
let confSerial = '111112'
let confUsername = 'butthead'
let confPassword = '12345678'
if (args.length == 2) {
console.log('Getting ONT: ' + args[1])
confNode = args[0]
confSerial = args[1]
startGet()
}
if (args.length == 4) {
console.log('Creating ONT: ' + args[1])
confNode = args[0]
confSerial = args[1]
confUsername = args[2]
confPassword = args[3]
startCreate()
}
if (args.length == 3) { //Just Print
if (args[2] == "pppoe") {
confNode = args[0]
confSerial = args[1]
startPPPOE()
} else { "Unknown Extra Command" }
}
if (args.length != 4 && args.length != 2 && args.length != 3) {
console.log('Options missing, "CREATE: node calix NODENAME SERIAL USERNAME PASSWORD"')
console.log('Options missing, "SEARCH: node calix NODENAME SERIAL"')
}
function startGet() {
let xml = xmlLogin()
restLOGIN(xml).then(getONT).catch(function (e) {
console.log('GET Login Error:', e)
})
}
function startPPPOE() {
let xml = xmlLogin()
restLOGIN(xml).then(getPPPoE).catch(function (e) {
console.log('GET Login Error:', e)
})
}
function startCreate() {
let xml = xmlLogin()
restLOGIN(xml).then(createONT).catch(function (e) {
console.log('CREATE Login Error:', e)
})
}
function getONT(session) {
console.log('Login Session:', session)
oursession = session
let xml = xmlGETONT(confNode, oursession, confSerial)
restGET(xml, true).then((ont) => restGETPROF(confNode, oursession, ont, true)).then(logout).catch(function (e) {
console.log('Get Error:', e)
})
}
function getPPPoE(session) {
oursession = session
let xml = xmlGETONT(confNode, oursession, confSerial)
restGET(xml, false).then((ont) => restGETPROF(confNode, oursession, ont, false)).then(logout).catch(function (e) {
console.log('Get Error:', e)
})
}
function createONT(session) {
console.log('Login Session:', session)
oursession = session
let xml = xmlCREATEONT(confNode, oursession, confSerial)
//console.log(xml)
restCREATE(xml).then(processONT).catch(function (e) {
console.log('Create Error:', e)
})
}
function processONT(ontid) {
ontid = ontid.replace(/\"/gi, '')
console.log('ONT Created', ontid, oursession)
let xml = xmlPROVDS(confNode, oursession, ontid)
// console.log(xml)
restPROVDS(xml).then(setPPPOE).catch(function (e) {
console.log('Prov Error:', e)
})
}
function setPPPOE(ontid) {
ontid = ontid.replace(/\"/gi, '')
console.log('DATA Provisioned', ontid, oursession)
let xml = xmlPPPOE(confNode, oursession, ontid, confUsername, confPassword)
//console.log(xml)
restPPPOE(xml).then(function (res) {
console.log('Provisioned PPPoE')
logout()
}).catch(function (e) {
console.log('PPPoE Error:', e)
logout()
})
}
function logout() {
let xml = xmlLogout(oursession)
restLOGOUT(xml).then(function (d) {
// console.log('Session Logout:', d)
}).catch(function (e) {
console.log('Logout Error:', e)
})
}