issue 7 - async. op with manager now returns RPC object
[ncclient] / ncclient / capabilities.py
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 _capability_map = {
16     'urn:liberouter:params:netconf:capability:power-control:1.0':
17         [':power-control', ':power-control:1.0']
18 }
19
20 def _abbreviate(uri):
21     if uri.startswith('urn:ietf:params:netconf:'):
22         splitted = uri.split(':')
23         if ':capability:' in uri:
24             return [ ':' + splitted[5], ':' + splitted[5] + ':' + splitted[6] ]
25         elif ':base:' in uri:
26             return [ ':base', ':base' + ':'+ splitted[5] ]
27     elif uri in _capability_map:
28         return _capability_map[uri]
29     return []
30
31 def schemes(url_uri):
32     """Given a URI that has a *scheme* query string (i.e. *:url* capability
33     URI), will return a list of supported schemes.
34     """
35     return url_uri.partition("?scheme=")[2].split(',')
36
37 class Capabilities:
38
39     """Represents the set of capabilities for a NETCONF client or server.
40     Initialised with a list of capability URI's.
41
42     Presence of a capability can be checked with the *in* operations. In addition
43     to the URI, for capabilities of the form
44     *urn:ietf:params:netconf:capability:$name:$version* their shorthand can be
45     used as a key. For example, for
46     *urn:ietf:params:netconf:capability:candidate:1.0* the shorthand would be
47     *:candidate*. If version is significant, use *:candidate:1.0* as key.
48     """
49
50     def __init__(self, capabilities):
51         self._dict = {}
52         for uri in capabilities:
53             self._dict[uri] = _abbreviate(uri)
54
55     def __contains__(self, key):
56         if key in self._dict:
57             return True
58         for abbrs in self._dict.values():
59             if key in abbrs:
60                 return True
61         return False
62
63     def __len__(self):
64         return len(self._dict)
65
66     def __iter__(self):
67         return self._dict.keys().__iter__()
68
69     def __repr__(self):
70         return repr(self._dict.keys())
71
72     def __list__(self):
73         return self._dict.keys()
74
75     def add(self, uri):
76         "Add a capability"
77         self._dict[uri] = _abbreviate(uri)
78
79     def remove(self, uri):
80         "Remove a capability"
81         if key in self._dict:
82             del self._dict[key]
83
84     def check(self, key):
85         """Whether specified capability is present.
86
87         :arg key: URI or shorthand
88
89         .. note:
90             The *in* operation is the preferred form.
91         """
92         return key in self
93
94     def get_uri(self, shorthand):
95         for uri, abbrs in self._dict.items():
96             if shorthand in abbrs:
97                 return uri
98
99 #: :class:`Capabilities` object representing the capabilities currently supported by NCClient
100 CAPABILITIES = Capabilities([
101     'urn:ietf:params:netconf:base:1.0',
102     'urn:ietf:params:netconf:capability:writable-running:1.0',
103     'urn:ietf:params:netconf:capability:candidate:1.0',
104     'urn:ietf:params:netconf:capability:confirmed-commit:1.0',
105     'urn:ietf:params:netconf:capability:rollback-on-error:1.0',
106     'urn:ietf:params:netconf:capability:startup:1.0',
107     'urn:ietf:params:netconf:capability:url:1.0?scheme=http,ftp,file,https,sftp',
108     'urn:ietf:params:netconf:capability:validate:1.0',
109     'urn:ietf:params:netconf:capability:xpath:1.0',
110     'urn:liberouter:params:netconf:capability:power-control:1.0'
111     #'urn:ietf:params:netconf:capability:notification:1.0', # TODO
112     #'urn:ietf:params:netconf:capability:interleave:1.0' # theoretically already supported
113 ])