root / ncclient / transport / hello.py @ a14c36f9
History | View | Annotate | Download (2.2 kB)
1 |
# Copyright 2009 Shikhar Bhushan
|
---|---|
2 |
#
|
3 |
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
# you may not use this file except in compliance with the License.
|
5 |
# You may obtain a copy of the License at
|
6 |
#
|
7 |
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
#
|
9 |
# Unless required by applicable law or agreed to in writing, software
|
10 |
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
# See the License for the specific language governing permissions and
|
13 |
# limitations under the License.
|
14 |
|
15 |
"All to do with NETCONF <hello> messages"
|
16 |
|
17 |
from ncclient import content |
18 |
|
19 |
class HelloHandler(Listener): |
20 |
|
21 |
def __init__(self, init_cb, error_cb): |
22 |
self._init_cb = init_cb
|
23 |
self._error_cb = error_cb
|
24 |
|
25 |
def __str__(self): |
26 |
return 'HelloListener' |
27 |
|
28 |
def callback(self, root, raw): |
29 |
if content.unqualify(root[0]) == 'hello': |
30 |
try:
|
31 |
id, capabilities = HelloHandler.parse(raw)
|
32 |
except Exception as e: |
33 |
self._error_cb(e)
|
34 |
else:
|
35 |
self._init_cb(id, capabilities) |
36 |
|
37 |
def errback(self, err): |
38 |
self._error_cb(err)
|
39 |
|
40 |
@staticmethod
|
41 |
def build(capabilities): |
42 |
"Given a list of capability URI's returns encoded <hello> message"
|
43 |
spec = { |
44 |
'tag': content.qualify('hello'), |
45 |
'children': [{
|
46 |
'tag': 'capabilities', |
47 |
'children': # this is fun :-) |
48 |
[{ 'tag': 'capability', 'text': uri} for uri in capabilities] |
49 |
}] |
50 |
} |
51 |
return content.to_xml(spec)
|
52 |
|
53 |
@staticmethod
|
54 |
def parse(raw): |
55 |
"Returns tuple of ('session-id', ['capability_uri', ...])"
|
56 |
sid, capabilities = 0, []
|
57 |
root = content.from_xml(raw) |
58 |
for child in root['children']: |
59 |
tag = content.unqualify(child['tag'])
|
60 |
if tag == 'session-id': |
61 |
sid = child['text']
|
62 |
elif tag == 'capabilities': |
63 |
for cap in child['children']: |
64 |
if content.unqualify(cap['text']) == 'capability': |
65 |
capabilities.append(cap['text'])
|
66 |
return sid, capabilities
|