Statistics
| Branch: | Tag: | Revision:

root / ncclient / capabilities.py @ 564bee4f

History | View | Annotate | Download (3.6 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
def _abbreviate(uri):
16
    if uri.startswith('urn:ietf:params:netconf:'):
17
        splitted = uri.split(':')
18
        if ':capability:' in uri:
19
            return [ ':' + splitted[5], ':' + splitted[5] + ':' + splitted[6] ]
20
        elif ':base:' in uri:
21
            return [ ':base', ':base' + ':'+ splitted[5] ]
22
        else:
23
            return []
24
    else:
25
        return []
26

    
27
def schemes(url_uri):
28
    """Given a URI that has a *scheme* query string (i.e. *:url* capability
29
    URI), will return a list of supported schemes.
30
    """
31
    return url_uri.partition("?scheme=")[2].split(',')
32

    
33
class Capabilities:
34

    
35
    """Represents the set of capabilities for a NETCONF client or server.
36
    Initialised with a list of capability URI's.
37

38
    Presence of a capability can be checked with the *in* operations. In addition
39
    to the URI, for capabilities of the form
40
    *urn:ietf:params:netconf:capability:$name:$version* their shorthand can be
41
    used as a key. For example, for
42
    *urn:ietf:params:netconf:capability:candidate:1.0* the shorthand would be
43
    *:candidate*. If version is significant, use *:candidate:1.0* as key.
44
    """
45

    
46
    def __init__(self, capabilities):
47
        self._dict = {}
48
        for uri in capabilities:
49
            self._dict[uri] = _abbreviate(uri)
50

    
51
    def __contains__(self, key):
52
        if key in self._dict:
53
            return True
54
        for abbrs in self._dict.values():
55
            if key in abbrs:
56
                return True
57
        return False
58

    
59
    def __len__(self):
60
        return len(self._dict)
61

    
62
    def __iter__(self):
63
        return self._dict.keys().__iter__()
64

    
65
    def __repr__(self):
66
        return repr(self._dict.keys())
67

    
68
    def __list__(self):
69
        return self._dict.keys()
70

    
71
    def add(self, uri):
72
        "Add a capability"
73
        self._dict[uri] = _abbreviate(uri)
74

    
75
    def remove(self, uri):
76
        "Remove a capability"
77
        if key in self._dict:
78
            del self._dict[key]
79

    
80
    def check(self, key):
81
        """Whether specified capability is present.
82

83
        :arg key: URI or shorthand
84

85
        .. note:
86
            The *in* operation is the preferred form.
87
        """
88
        return key in self
89

    
90
    def get_uris(self, shorthand):
91
        return [uri for uri, abbrs in self._dict.items() if shorthand in abbrs]
92

    
93
#: :class:`Capabilities` object representing the capabilities currently supported by NCClient
94
CAPABILITIES = Capabilities([
95
    'urn:ietf:params:netconf:base:1.0',
96
    'urn:ietf:params:netconf:capability:writable-running:1.0',
97
    'urn:ietf:params:netconf:capability:candidate:1.0',
98
    'urn:ietf:params:netconf:capability:confirmed-commit:1.0',
99
    'urn:ietf:params:netconf:capability:rollback-on-error:1.0',
100
    'urn:ietf:params:netconf:capability:startup:1.0',
101
    'urn:ietf:params:netconf:capability:url:1.0?scheme=http,ftp,file,https,sftp',
102
    'urn:ietf:params:netconf:capability:validate:1.0',
103
    'urn:ietf:params:netconf:capability:xpath:1.0',
104
    #'urn:ietf:params:netconf:capability:notification:1.0', # TODO
105
    #'urn:ietf:params:netconf:capability:interleave:1.0' # theoretically already supported
106
])