Statistics
| Branch: | Tag: | Revision:

root / ncclient / capability.py @ efc6b445

History | View | Annotate | Download (2.5 kB)

1 7101ce49 Shikhar Bhushan
# Copyright 2009 Shikhar Bhushan
2 7101ce49 Shikhar Bhushan
#
3 7101ce49 Shikhar Bhushan
# Licensed under the Apache License, Version 2.0 (the "License");
4 7101ce49 Shikhar Bhushan
# you may not use this file except in compliance with the License.
5 7101ce49 Shikhar Bhushan
# You may obtain a copy of the License at
6 7101ce49 Shikhar Bhushan
#
7 7101ce49 Shikhar Bhushan
#    http://www.apache.org/licenses/LICENSE-2.0
8 7101ce49 Shikhar Bhushan
#
9 7101ce49 Shikhar Bhushan
# Unless required by applicable law or agreed to in writing, software
10 7101ce49 Shikhar Bhushan
# distributed under the License is distributed on an "AS IS" BASIS,
11 7101ce49 Shikhar Bhushan
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 7101ce49 Shikhar Bhushan
# See the License for the specific language governing permissions and
13 7101ce49 Shikhar Bhushan
# limitations under the License.
14 7101ce49 Shikhar Bhushan
15 7101ce49 Shikhar Bhushan
class Capabilities:
16 7101ce49 Shikhar Bhushan
    
17 7101ce49 Shikhar Bhushan
    def __init__(self, capabilities=None):
18 7101ce49 Shikhar Bhushan
        self._dict = {}
19 7101ce49 Shikhar Bhushan
        if isinstance(capabilities, dict):
20 7101ce49 Shikhar Bhushan
            self._dict = capabilities
21 7101ce49 Shikhar Bhushan
        elif isinstance(capabilities, list):
22 7101ce49 Shikhar Bhushan
            for uri in capabilities:
23 7101ce49 Shikhar Bhushan
                self._dict[uri] = Capabilities.guess_shorthand(uri)
24 7101ce49 Shikhar Bhushan
    
25 7101ce49 Shikhar Bhushan
    def __contains__(self, key):
26 7101ce49 Shikhar Bhushan
        return ( key in self._dict ) or ( key in self._dict.values() )
27 7101ce49 Shikhar Bhushan
    
28 7101ce49 Shikhar Bhushan
    def __repr__(self):
29 7101ce49 Shikhar Bhushan
        elems = ['<capability>%s</capability>' % uri for uri in self._dict]
30 7101ce49 Shikhar Bhushan
        return ('<capabilities>%s</capabilities>' % ''.join(elems))
31 7101ce49 Shikhar Bhushan
    
32 7101ce49 Shikhar Bhushan
    def add(self, uri, shorthand=None):
33 7101ce49 Shikhar Bhushan
        if shorthand is None:
34 7101ce49 Shikhar Bhushan
            shorthand = Capabilities.guess_shorthand(uri)
35 7101ce49 Shikhar Bhushan
        self._dict[uri] = shorthand
36 7101ce49 Shikhar Bhushan
    
37 35ad9d81 Shikhar Bhushan
    set = add
38 35ad9d81 Shikhar Bhushan
    
39 7101ce49 Shikhar Bhushan
    def remove(self, key):
40 7101ce49 Shikhar Bhushan
        if key in self._dict:
41 7101ce49 Shikhar Bhushan
            del self._dict[key]
42 7101ce49 Shikhar Bhushan
        else:
43 7101ce49 Shikhar Bhushan
            for uri in self._dict:
44 7101ce49 Shikhar Bhushan
                if self._dict[uri] == key:
45 7101ce49 Shikhar Bhushan
                    del self._dict[uri]
46 7101ce49 Shikhar Bhushan
                    break
47 35ad9d81 Shikhar Bhushan
        
48 7101ce49 Shikhar Bhushan
    @staticmethod
49 7101ce49 Shikhar Bhushan
    def guess_shorthand(uri):
50 7101ce49 Shikhar Bhushan
        if uri.startswith('urn:ietf:params:netconf:capability:'):
51 7101ce49 Shikhar Bhushan
            return (':' + uri.split(':')[5])
52 35ad9d81 Shikhar Bhushan
53 7101ce49 Shikhar Bhushan
    
54 7101ce49 Shikhar Bhushan
CAPABILITIES = Capabilities([
55 7101ce49 Shikhar Bhushan
    'urn:ietf:params:netconf:base:1.0',
56 7101ce49 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:writable-running:1.0',
57 7101ce49 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:candidate:1.0',
58 7101ce49 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:confirmed-commit:1.0',
59 7101ce49 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:rollback-on-error:1.0',
60 7101ce49 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:startup:1.0',
61 7101ce49 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:url:1.0',
62 7101ce49 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:validate:1.0',
63 7101ce49 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:xpath:1.0',
64 7101ce49 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:notification:1.0',
65 7101ce49 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:interleave:1.0'
66 7101ce49 Shikhar Bhushan
    ])
67 7101ce49 Shikhar Bhushan
68 7101ce49 Shikhar Bhushan
if __name__ == "__main__":
69 7101ce49 Shikhar Bhushan
    assert(':validate' in CAPABILITIES) # test __contains__
70 7101ce49 Shikhar Bhushan
    print CAPABILITIES # test __repr__