Major updates paving the road support ACS methods

This commit is contained in:
Zaid Abdulla committed 2013-01-22 11:48:24 +03:00
1 parent 3ce097359c
commit 462e9e3afd
3 files changed
+207 -191

No files matched your search

+136 -133
View File
@@ -10,16 +10,13 @@ NAMESPACES = {
'cwmp' : 'urn:dslforum-org:cwmp-1-0',
}
trim = (str) ->
str.replace /^\s+|\s+$/g, ''
cookiesToObj = (cookieLine) ->
cookies = {}
for c in trim(cookieLine).split ','
for c in cookieLine.trim().split ','
nv = c.split '='
continue if nv.length < 2
cookies[trim(nv[0])] = trim(nv[1])
cookies[nv[0].trim()] = nv[1].trim()
return cookies
@@ -30,108 +27,105 @@ cookiesToStr = (obj) ->
return l# + 'path=/'
sessionId = (xml) ->
e = xml.get('//soap-env:Envelope/soap-env:Header/cwmp:ID', NAMESPACES)
if e?
return trim(e.text())
return undefined
event = (xml) ->
e.text().trim() for e in xml.find('EventStruct/EventCode', NAMESPACES)
deviceId = (xml) ->
e = xml.get('//soap-env:Envelope/soap-env:Body/cwmp:Inform/DeviceId', NAMESPACES)
if e?
id = {}
for n in e.find('//child::*')
id[n.name()] = trim(n.text())
return id
return undefined
eventCodes = (xml) ->
(trim(e.text()) for e in xml.find('//soap-env:Envelope/soap-env:Body/cwmp:Inform/Event/EventStruct/EventCode', NAMESPACES))
retryCount = (xml) ->
e = xml.get('//soap-env:Envelope/soap-env:Body/cwmp:Inform/RetryCount', NAMESPACES)
if e
return JSON.parse(e.text())
return undefined
informParameterValues = (xml) ->
if not xml.get( '//soap-env:Envelope/soap-env:Body/cwmp:Inform/ParameterList', NAMESPACES)
return undefined
values = []
for e in xml.find('//soap-env:Envelope/soap-env:Body/cwmp:Inform/ParameterList/ParameterValueStruct', NAMESPACES)
valueType = trim(e.get('Value').attr('type').value())
name = trim(e.get('Name').text())
switch valueType
when 'xsd:boolean'
value = Boolean(JSON.parse(e.get('Value').text()))
when 'xsd:unsignedInt'
value = JSON.parse(e.get('Value').text())
else
value = trim(e.get('Value').text())
values.push([name, value])
return values
getParameterNamesResponse = (xml) ->
if not xml.get('//soap-env:Envelope/soap-env:Body/cwmp:GetParameterNamesResponse', NAMESPACES)
return undefined
response = []
for e in xml.find('//soap-env:Envelope/soap-env:Body/cwmp:GetParameterNamesResponse/ParameterList/ParameterInfoStruct', NAMESPACES)
name = trim(e.get('Name').text())
parameterInfoList = (xml) ->
infoList = []
for e in xml.find('ParameterInfoStruct', NAMESPACES)
name = e.get('Name').text().trim()
writable = Boolean(JSON.parse(e.get('Writable').text()))
response.push([name, writable])
return response
infoList.push([name, writable])
return infoList
getParameterValuesResponse = (xml) ->
if not xml.get( '//soap-env:Envelope/soap-env:Body/cwmp:GetParameterValuesResponse', NAMESPACES)
return undefined
values = []
for e in xml.find('//soap-env:Envelope/soap-env:Body/cwmp:GetParameterValuesResponse/ParameterList/ParameterValueStruct', NAMESPACES)
valueType = trim(e.get('Value').attr('type').value())
name = trim(e.get('Name').text())
parameterValueList = (xml) ->
valueList = []
for e in xml.find('ParameterValueStruct', NAMESPACES)
valueType = e.get('Value').attr('type').value().trim()
name = e.get('Name').text().trim()
value = e.get('Value').text().trim()
switch valueType
when 'xsd:boolean'
value = Boolean(JSON.parse(e.get('Value').text()))
value = Boolean(JSON.parse(value))
when 'xsd:unsignedInt'
value = JSON.parse(e.get('Value').text())
else
value = trim(e.get('Value').text())
values.push([name, value])
return values
value = JSON.parse(value)
valueList.push([name, value])
return valueList
setParameterValuesResponse = (xml) ->
e = xml.get('//soap-env:Envelope/soap-env:Body/cwmp:SetParameterValuesResponse/Status', NAMESPACES)
if e
return JSON.parse(e.text())
return undefined
cpeGetParameterNames = (xml, methodRequest) ->
el = xml.node('cwmp:GetParameterNames')
el.node('cwmp:ParameterPath').text(methodRequest.parameterPath)
el.node('cwmp:NextLevel').text(+methodRequest.nextLevel)
cpeGetParameterNamesResponse = (xml) ->
{parameterList : parameterInfoList(xml.get('ParameterList'))}
cpeGetParameterValues = (xml, methodRequest) ->
el = xml.node('cwmp:GetParameterValues').node('cwmp:ParameterNames')
for p in methodRequest.parameterNames
el.node('xsd:string').text(p)
cpeGetParameterValuesResponse = (xml) ->
{parameterList : parameterValueList(xml.get('ParameterList'))}
cpeSetParameterValues = (xml, methodRequest) ->
el = xml.node('cwmp:SetParameterValues')
paramList = el.node('ParameterList')
for i in methodRequest.parameterList
pvs = paramList.node('ParameterValueStruct')
pvs.node('Name').text(i[0])
pvs.node('Value').text(i[1])
# Huawei CPEs need this element present otherwise won't respond
el.node('ParameterKey').text(if methodRequest.parameterKey? then methodRequest.parameterKey else '')
cpeSetParameterValuesResponse = (xml) ->
{status : JSON.parse(xml.get('Status').text())}
traverseXml = (xml) ->
obj = {}
for n in xml.childNodes()
if n.type() == 'element'
obj[n.name()] = traverseXml(n)
if Object.keys(obj).length == 0
xml.text()
else
obj
fault = (xml) ->
f = xml.get('//soap-env:Envelope/soap-env:Body/soap-env:Fault', NAMESPACES)
if f
traverse = (el) ->
children = el.childNodes()
obj = {}
for n in el.childNodes()
if n.type() == 'element'
obj[n.name()] = traverse(n)
if Object.keys(obj).length == 0
return el.text()
else
return obj
return traverseXml(xml)
return traverse(f)
return undefined
acsInform = (xml) ->
{
parameterList : parameterValueList(xml.get('ParameterList')),
deviceId : traverseXml(xml.get('DeviceId')),
event : event(xml.get('Event')),
retryCount : JSON.parse(xml.get('RetryCount').text())
}
acsInformResponse = (xml) ->
xml.node('cwmp:InformResponse').node('MaxEnvelopes').text(1)
cpeReboot = (xml, methodRequest) ->
el = xml.node('cwmp:Reboot')
# Huawei CPEs need this element present otherwise won't respond
el.node('CommandKey').text(if methodRequest.commandKey then methodRequest.commandKey else '')
cpeRebootResponse = (xml) ->
{}
exports.request = (httpRequest) ->
@@ -147,21 +141,36 @@ exports.request = (httpRequest) ->
# some devices send invalid utf8 characters
xml = libxmljs.parseXml httpRequest.getBody('binary')
cwmpRequest.sessionId = sessionId xml
cwmpRequest.inform = true if xml.get('//soap-env:Envelope/soap-env:Body/cwmp:Inform', NAMESPACES)
cwmpRequest.informParameterValues = informParameterValues xml
cwmpRequest.reboot = true if xml.get('//soap-env:Envelope/soap-env:Body/cwmp:RebootResponse', NAMESPACES)
cwmpRequest.deviceId = deviceId xml
cwmpRequest.eventCodes = eventCodes xml
cwmpRequest.retryCount = retryCount xml
cwmpRequest.getParameterNamesResponse = getParameterNamesResponse xml
cwmpRequest.getParameterValuesResponse = getParameterValuesResponse xml
cwmpRequest.setParameterValuesResponse = setParameterValuesResponse xml
cwmpRequest.fault = fault xml
cwmpRequest.id = xml.get('//soap-env:Envelope/soap-env:Header/cwmp:ID', NAMESPACES).text()
methodElement = xml.get('/soap-env:Envelope/soap-env:Body/cwmp:*', NAMESPACES)
if methodElement?
switch methodElement.name()
when 'Inform'
cwmpRequest.methodRequest = acsInform(methodElement)
cwmpRequest.methodRequest.type = 'Inform'
when 'GetParameterNamesResponse'
cwmpRequest.methodResponse = cpeGetParameterNamesResponse(methodElement)
cwmpRequest.methodResponse.type = 'GetParameterNamesResponse'
when 'GetParameterValuesResponse'
cwmpRequest.methodResponse = cpeGetParameterValuesResponse(methodElement)
cwmpRequest.methodResponse.type = 'GetParameterValuesResponse'
when 'SetParameterValuesResponse'
cwmpRequest.methodResponse = cpeSetParameterValuesResponse(methodElement)
cwmpRequest.methodResponse.type = 'SetParameterValuesResponse'
when 'RebootResponse'
cwmpRequest.methodResponse = cpeRebootResponse(methodElement)
cwmpRequest.methodResponse.type = 'RebootResponse'
else
throw Error('8000 Method not supported')
else
faultElement = xml.get('/soap-env:Envelope/soap-env:Body/soap-env:Fault', NAMESPACES)
cwmpRequest.fault = fault(faultElement)
return cwmpRequest
exports.response = (sessionId, cwmpResponse, cookies = null) ->
exports.response = (id, cwmpResponse, cookies = null) ->
headers = {
'Content-Type' : 'text/xml; charset="utf-8"',
'Server' : SERVER_NAME,
@@ -183,35 +192,29 @@ exports.response = (sessionId, cwmpResponse, cookies = null) ->
env.defineNamespace prefix, url
header = env.node('soap-env:Header')
header.node('cwmp:ID').attr({'soap-env:mustUnderstand' : 1}).text(sessionId)
#header.node('cwmp:NoMoreRequests').text(0) # depracated
header.node('cwmp:ID').attr({'soap-env:mustUnderstand' : 1}).text(id)
body = env.node('soap-env:Body')
if cwmpResponse.inform
body.node('cwmp:InformResponse').node('MaxEnvelopes').text(1)
if cwmpResponse.getParameterNames?
getParameterNames = body.node('cwmp:GetParameterNames')
getParameterNames.node('cwmp:ParameterPath').text(cwmpResponse.getParameterNames[0])
getParameterNames.node('cwmp:NextLevel').text(+cwmpResponse.getParameterNames[1])
if cwmpResponse.getParameterValues?
parameterNames = body.node('cwmp:GetParameterValues').node('cwmp:ParameterNames')
for p in cwmpResponse.getParameterValues
parameterNames.node('xsd:string').text(p)
if cwmpResponse.setParameterValues?
setParameterValuesNode = body.node('cwmp:SetParameterValues')
parameterList = setParameterValuesNode.node('ParameterList')
for i in cwmpResponse.setParameterValues
pvs = parameterList.node('ParameterValueStruct')
pvs.node('Name').text(i[0])
pvs.node('Value').text(i[1])
setParameterValuesNode.node('ParameterKey')
if cwmpResponse.reboot?
body.node('cwmp:Reboot').node('CommandKey').text(cwmpResponse.reboot)
if cwmpResponse.methodResponse?
switch cwmpResponse.methodResponse.type
when 'InformResponse'
acsInformResponse(body)
else
throw Error("Unknown method response type #{cwmpResponse.methodResponse.type}")
else if cwmpResponse.methodRequest?
switch cwmpResponse.methodRequest.type
when 'GetParameterNames'
cpeGetParameterNames(body, cwmpResponse.methodRequest)
when 'GetParameterValues'
cpeGetParameterValues(body, cwmpResponse.methodRequest)
when 'SetParameterValues'
cpeSetParameterValues(body, cwmpResponse.methodRequest)
when 'Reboot'
cpeReboot(body, cwmpResponse.methodRequest)
else
throw Error("Unknown method request #{cwmpResponse.methodRequest.type}")
else
throw Error('Invalid response arguments')
data = xml.toString()
headers['Content-Length'] = data.length