Statistics
| Branch: | Tag: | Revision:

root / ncclient / capabilities.py @ 0cdb8b3c

History | View | Annotate | Download (2.7 kB)

1 d095a59e Shikhar Bhushan
# Copyright 2009 Shikhar Bhushan
2 d095a59e Shikhar Bhushan
#
3 d095a59e Shikhar Bhushan
# Licensed under the Apache License, Version 2.0 (the "License");
4 d095a59e Shikhar Bhushan
# you may not use this file except in compliance with the License.
5 d095a59e Shikhar Bhushan
# You may obtain a copy of the License at
6 d095a59e Shikhar Bhushan
#
7 d095a59e Shikhar Bhushan
#    http://www.apache.org/licenses/LICENSE-2.0
8 d095a59e Shikhar Bhushan
#
9 d095a59e Shikhar Bhushan
# Unless required by applicable law or agreed to in writing, software
10 d095a59e Shikhar Bhushan
# distributed under the License is distributed on an "AS IS" BASIS,
11 d095a59e Shikhar Bhushan
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 d095a59e Shikhar Bhushan
# See the License for the specific language governing permissions and
13 d095a59e Shikhar Bhushan
# limitations under the License.
14 d095a59e Shikhar Bhushan
15 0cdb8b3c Shikhar Bhushan
def abbreviate(uri):
16 0cdb8b3c Shikhar Bhushan
    if uri.startswith('urn:ietf:params:netconf:capability:'):
17 0cdb8b3c Shikhar Bhushan
        return (':' + uri.split(':')[5])
18 0cdb8b3c Shikhar Bhushan
19 0cdb8b3c Shikhar Bhushan
def schemes(uri):
20 0cdb8b3c Shikhar Bhushan
    return uri.partition("?scheme=")[2].split(',')
21 78f6c132 Shikhar Bhushan
22 d095a59e Shikhar Bhushan
class Capabilities:
23 d095a59e Shikhar Bhushan
    
24 78f6c132 Shikhar Bhushan
    """Represent the capabilities of client or server. Also facilitates using
25 78f6c132 Shikhar Bhushan
    abbreviated capability names in addition to complete URI.
26 78f6c132 Shikhar Bhushan
    """
27 78f6c132 Shikhar Bhushan
    
28 d095a59e Shikhar Bhushan
    def __init__(self, capabilities=None):
29 d095a59e Shikhar Bhushan
        self._dict = {}
30 d095a59e Shikhar Bhushan
        if isinstance(capabilities, dict):
31 d095a59e Shikhar Bhushan
            self._dict = capabilities
32 d095a59e Shikhar Bhushan
        elif isinstance(capabilities, list):
33 d095a59e Shikhar Bhushan
            for uri in capabilities:
34 0cdb8b3c Shikhar Bhushan
                self._dict[uri] = abbreviate(uri)
35 d095a59e Shikhar Bhushan
    
36 d095a59e Shikhar Bhushan
    def __contains__(self, key):
37 d095a59e Shikhar Bhushan
        return ( key in self._dict ) or ( key in self._dict.values() )
38 d095a59e Shikhar Bhushan
    
39 d095a59e Shikhar Bhushan
    def __iter__(self):
40 d095a59e Shikhar Bhushan
        return self._dict.keys().__iter__()
41 d095a59e Shikhar Bhushan
    
42 d095a59e Shikhar Bhushan
    def __repr__(self):
43 d095a59e Shikhar Bhushan
        return repr(self._dict.keys())
44 d095a59e Shikhar Bhushan
    
45 94803aaf Shikhar Bhushan
    def __list__(self):
46 94803aaf Shikhar Bhushan
        return self._dict.keys()
47 94803aaf Shikhar Bhushan
    
48 d095a59e Shikhar Bhushan
    def add(self, uri, shorthand=None):
49 d095a59e Shikhar Bhushan
        if shorthand is None:
50 0cdb8b3c Shikhar Bhushan
            shorthand = abbreviate(uri)
51 d095a59e Shikhar Bhushan
        self._dict[uri] = shorthand
52 d095a59e Shikhar Bhushan
    
53 d095a59e Shikhar Bhushan
    set = add
54 d095a59e Shikhar Bhushan
    
55 d095a59e Shikhar Bhushan
    def remove(self, key):
56 d095a59e Shikhar Bhushan
        if key in self._dict:
57 d095a59e Shikhar Bhushan
            del self._dict[key]
58 d095a59e Shikhar Bhushan
        else:
59 d095a59e Shikhar Bhushan
            for uri in self._dict:
60 d095a59e Shikhar Bhushan
                if self._dict[uri] == key:
61 d095a59e Shikhar Bhushan
                    del self._dict[uri]
62 d095a59e Shikhar Bhushan
                    break
63 d095a59e Shikhar Bhushan
64 0cdb8b3c Shikhar Bhushan
# : the capabilities currently supported by ncclient
65 d095a59e Shikhar Bhushan
CAPABILITIES = Capabilities([
66 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:base:1.0',
67 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:writable-running:1.0',
68 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:candidate:1.0',
69 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:confirmed-commit:1.0',
70 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:rollback-on-error:1.0',
71 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:startup:1.0',
72 0cdb8b3c Shikhar Bhushan
    'urn:ietf:params:netconf:capability:url:1.0?scheme=http,ftp,file,https,sftp',
73 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:validate:1.0',
74 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:xpath:1.0',
75 0cdb8b3c Shikhar Bhushan
    #'urn:ietf:params:netconf:capability:notification:1.0', # TODO
76 0cdb8b3c Shikhar Bhushan
    #'urn:ietf:params:netconf:capability:interleave:1.0' # theoretically already supported
77 0cdb8b3c Shikhar Bhushan
])