Statistics
| Branch: | Tag: | Revision:

root / ncclient / transport / hello.py @ 4de03d63

History | View | Annotate | Download (2.1 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:
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
                'children': # 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