root / ncclient / transport / hello.py @ 179b00d4
History | View | Annotate | Download (2.3 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.glue import Listener |
18 |
from ncclient.content import XMLConverter, BASE_NS |
19 |
from ncclient.content import qualify as _ |
20 |
from ncclient.content import unqualify as __ |
21 |
|
22 |
class HelloHandler(Listener): |
23 |
|
24 |
def __init__(self, init_cb, error_cb): |
25 |
self._init_cb = init_cb
|
26 |
self._error_cb = error_cb
|
27 |
|
28 |
def __str__(self): |
29 |
return 'HelloListener' |
30 |
|
31 |
def callback(self, root, raw): |
32 |
if __(root[0]) == 'hello': |
33 |
try:
|
34 |
id, capabilities = HelloHandler.parse(raw)
|
35 |
except Exception as e: |
36 |
self._error_cb(e)
|
37 |
else:
|
38 |
self._init_cb(id, capabilities) |
39 |
|
40 |
def errback(self, err): |
41 |
self._error_cb(err)
|
42 |
|
43 |
@staticmethod
|
44 |
def build(capabilities): |
45 |
"Given a list of capability URI's returns encoded <hello> message"
|
46 |
spec = { |
47 |
'tag': _('hello', BASE_NS), |
48 |
'children': [{
|
49 |
'tag': 'capabilities', |
50 |
'children': # this is fun :-) |
51 |
[{ 'tag': 'capability', 'text': uri} for uri in capabilities] |
52 |
}] |
53 |
} |
54 |
return XMLConverter(spec).to_string()
|
55 |
|
56 |
@staticmethod
|
57 |
def parse(raw): |
58 |
"Returns tuple of ('session-id', ['capability_uri', ...])"
|
59 |
sid, capabilities = 0, []
|
60 |
root = XMLConverter.from_string(raw) |
61 |
for child in root['children']: |
62 |
if __(child['tag']) == 'session-id': |
63 |
sid = child['text']
|
64 |
elif __(child['tag']) == 'capabilities': |
65 |
for cap in child['children']: |
66 |
if __(cap['text']) == 'capability': |
67 |
capabilities.append(cap['text'])
|
68 |
return sid, capabilities
|