Statistics
| Branch: | Tag: | Revision:

root / ncclient / content.py @ a4850088

History | View | Annotate | Download (1.9 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
NAMESPACE = 'urn:ietf:params:xml:ns:netconf:base:1.0'
16

    
17
def qualify(tag, ns=NAMESPACE):
18
    return '{%s}%s' % (ns, tag)
19

    
20
_ = qualify
21

    
22
def make_hello(capabilities):
23
    return '<hello xmlns="%s">%s</hello>' % (NAMESPACE, capabilities)
24

    
25
def make_rpc(id, op):
26
    return '<rpc message-id="%s" xmlns="%s">%s</rpc>' % (id, NAMESPACE, op)
27

    
28
def parse_hello(raw):
29
    from capability import Capabilities
30
    id, capabilities = 0, Capabilities()
31
    root = ElementTree.fromstring(raw)
32
    if root.tag == _('hello'):
33
        for child in hello.getchildren():
34
            if child.tag == _('session-id'):
35
                id = int(child.text)
36
            elif child.tag == _('capabilities'):
37
                for cap in child.getiterator(_('capability')):
38
                    capabilities.add(cap.text)
39
    return id, capabilities
40

    
41
def parse_message_type(raw):
42
    'returns 0 if notification, message-id if rpc-reply'
43
    
44
    class RootElementParser:
45
        
46
        def __init__(self):
47
            self.id = None
48
            
49
        def start(self, tag, attrib):
50
            if tag == _('rpc'):
51
                self.id = int(attrib['message-id'])
52
            elif tag == _('notification'):
53
                self.id = 0
54
    
55
    target = RootElementParser()
56
    parser = ElementTree.XMLTreeBuilder(target=target)
57
    parser.feed(raw)
58
    return target.id