Statistics
| Branch: | Tag: | Revision:

root / ncclient / capabilities.py @ a7cb58ce

History | View | Annotate | Download (3.4 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:capability:'):
17
        return ':' + uri.split(':')[5]
18
    elif uri.startswith('urn:ietf:params:netconf:base:'):
19
        return ':base'
20

    
21
def version(uri):
22
    if uri.startswith('urn:ietf:params:netconf:capability:'):
23
        return uri.split(':')[6]
24
    elif uri.startswith('urn:ietf:params:netconf:base:'):
25
        return uri.split(':')[5]
26

    
27
class Capabilities:
28

    
29
    def __init__(self, capabilities=None):
30
        self._dict = {}
31
        if isinstance(capabilities, dict):
32
            self._dict = capabilities
33
        elif isinstance(capabilities, list):
34
            for uri in capabilities:
35
                self._dict[uri] = (abbreviate(uri), version(uri))
36

    
37
    def __contains__(self, key):
38
        if key in self._dict:
39
            return True
40
        for info in self._dict.values():
41
            if key == info[0]:
42
                return True
43
        return False
44

    
45
    def __iter__(self):
46
        return self._dict.keys().__iter__()
47

    
48
    def __repr__(self):
49
        return repr(self._dict.keys())
50

    
51
    def __list__(self):
52
        return self._dict.keys()
53

    
54
    def add(self, uri, info=None):
55
        if info is None:
56
            info = (abbreviate(uri), version(uri))
57
        self._dict[uri] = info
58

    
59
    set = add
60

    
61
    def remove(self, key):
62
        if key in self._dict:
63
            del self._dict[key]
64
        else:
65
            for uri in self._dict:
66
                if key in self._dict[uri]:
67
                    del self._dict[uri]
68
                    break
69

    
70
    def get_uri(self, shortname):
71
        for uri, info in self._dict.items():
72
            if info[0] == shortname:
73
                return uri
74

    
75
    def url_schemes(self):
76
        url_uri = get_uri(':url')
77
        if url_uri is None:
78
            return []
79
        else:
80
            return url_uri.partition("?scheme=")[2].split(',')
81

    
82
    def version(self, key):
83
        try:
84
            return self._dict[key][1]
85
        except KeyError:
86
            for uri, info in self._dict.items():
87
                if info[0] == key:
88
                    return info[1]
89

    
90

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