-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsoap.py
128 lines (102 loc) · 4.2 KB
/
soap.py
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
#!/usr/bin/env python3
# Author: Alexander Couzens <[email protected]>
# (C) 2021 by sysmocom - s.f.m.c. GmbH <[email protected]>
import logging
class Soap:
m_namespace = {
'soap-env': 'http://schemas.xmlsoap.org/soap/envelope/',
'cwmp': 'urn:dslforum-org:cwmp-1-2'
}
def get_cwmp_method(self, root):
""" retrieve the cwmp method from the xml root Node """
body = root.find('soap-env:Body', Soap.m_namespace)
if body is None:
logging.error('find soap-env:Body failed')
return None
prefix = '{' + Soap.m_namespace['cwmp'] + '}'
for child in body:
if prefix in child.tag:
return (child.tag[len(prefix):], child)
return None
def get_cwmp_id(self, root):
""" retrieve the cwmp id """
header = root.find('soap-env:Header', Soap.m_namespace)
if header is None:
return None
prefix = '{' + Soap.m_namespace['cwmp'] + '}'
cwmpid = header.find(prefix+ 'ID', Soap.m_namespace)
if cwmpid is not None:
return cwmpid.text
return None
def get_cwmp_inform_events(self, inform):
""" return a list of Inform Events """
eventnode = inform.find('Event')
if eventnode is None:
return None
events = []
""" parse
<Event soap-enc:arrayType="cwmp:EventStruct[2]"
<EventStruct>
<EventCode>4 VALUE CHANGE</EventCode>
<CommandKey></CommandKey>
</EventStruct>
<EventStruct>
<EventCode>0 BOOTSTRAP</EventCode>
<CommandKey></CommandKey>
</EventStruct>
</Event>
"""
for ev in eventnode:
if ev.tag != "EventStruct":
continue
evcodenode = ev.find('EventCode')
events.append(evcodenode.text)
return events
def get_cwmp_inform_sn(self, inform):
""" retrieve the sn from an inform node """
device_id = inform.find('DeviceId')
if device_id is None:
return None
sn = device_id.find('SerialNumber')
if sn is None:
return None
return sn.text
#########################################################################################################
# description: get the value of a partial leaf path from soap message, such as ManagementServer.ConnectionRequestURL.
# We can't use the absolute path because we must support both TR098 and TR181.
#
# input: node - whole soap message
# partial_leaf_path - partial leaf path, such as ManagementServer.ConnectionRequestURL.
#
# output: none
#
# return: success - value of the partial leaf path
# fail - None
##########################################################################################################
def get_cwmp_value(self, node, partial_leaf_path):
""" retrieve the value of partial from an inform message """
parameter_list = node.find('ParameterList')
if parameter_list is None:
return None
for parameter in parameter_list.iter('ParameterValueStruct'):
name = parameter.find('Name')
if partial_leaf_path in name.text:
logging.info(parameter.find('Value').text)
return parameter.find('Value').text
return None
def get_cwmp_all_value(self, node):
parameterDict = {}
parameter_list = node.find('ParameterList')
if parameter_list is None:
return None
for parameter in parameter_list.iter('ParameterValueStruct'):
name = parameter.find('Name')
value = parameter.find('Value')
parameterDict[name.text] = value.text
return parameterDict
def get_cwmp_setresponse_status(self, setparametervaluesresponse):
""" retrieve the status from a setparametervaluesresponse node """
statusnode = setparametervaluesresponse.find('Status')
if statusnode is None:
return None
return statusnode.text