Revision 583c11f6 ncclient/transport/hello.py
b/ncclient/transport/hello.py | ||
---|---|---|
14 | 14 |
|
15 | 15 |
"All to do with NETCONF <hello> messages" |
16 | 16 |
|
17 |
from ncclient import content |
|
18 | 17 |
|
19 |
class HelloHandler: |
|
20 |
|
|
21 |
def __init__(self, init_cb, error_cb): |
|
22 |
self._init_cb = init_cb |
|
23 |
self._error_cb = error_cb |
|
24 |
|
|
25 |
def callback(self, root, raw): |
|
26 |
if content.unqualify(root[0]) == 'hello': |
|
27 |
try: |
|
28 |
id, capabilities = HelloHandler.parse(raw) |
|
29 |
except Exception as e: |
|
30 |
self._error_cb(e) |
|
31 |
else: |
|
32 |
self._init_cb(id, capabilities) |
|
33 |
|
|
34 |
def errback(self, err): |
|
35 |
self._error_cb(err) |
|
36 |
|
|
37 |
@staticmethod |
|
38 |
def build(capabilities): |
|
39 |
"Given a list of capability URI's returns encoded <hello> message" |
|
40 |
spec = { |
|
41 |
'tag': content.qualify('hello'), |
|
42 |
'subtree': [{ |
|
43 |
'tag': 'capabilities', |
|
44 |
'subtree': # this is fun :-) |
|
45 |
[{'tag': 'capability', 'text': uri} for uri in capabilities] |
|
46 |
}] |
|
47 |
} |
|
48 |
return content.dtree2xml(spec) |
|
49 |
|
|
50 |
@staticmethod |
|
51 |
def parse(raw): |
|
52 |
"Returns tuple of ('session-id', ['capability_uri', ...])" |
|
53 |
sid, capabilities = 0, [] |
|
54 |
root = content.xml2ele(raw) |
|
55 |
for child in root.getchildren(): |
|
56 |
tag = content.unqualify(child.tag) |
|
57 |
if tag == 'session-id': |
|
58 |
sid = child.text |
|
59 |
elif tag == 'capabilities': |
|
60 |
for cap in child.getchildren(): |
|
61 |
if content.unqualify(cap.tag) == 'capability': |
|
62 |
capabilities.append(cap.text) |
|
63 |
return sid, capabilities |
|
64 | 18 |
|
65 |
''' |
|
66 |
from ncclient.capabilities import CAPABILITIES |
|
67 |
from ncclient.transport.hello import HelloHandler |
|
19 |
from session import SessionListener |
|
68 | 20 |
|
69 |
print HelloHandler.build(CAPABILITIES) |
|
70 |
''' |
Also available in: Unified diff