Statistics
| Branch: | Tag: | Revision:

root / ncclient / capabilities.py @ 4bc8021f

History | View | Annotate | Download (2.9 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
#_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 URI), will return a list
33
    of supported schemes.
34
    """
35
    return url_uri.partition("?scheme=")[2].split(",")
36

    
37
class Capabilities:
38

    
39
    """Represents the set of capabilities available to a NETCONF client or server. It is initialized
40
    with a list of capability URI's. These can be iterated over.
41
    
42
    Presence of a capability can be checked with the *in* operation. In addition to the URI, for
43
    capabilities of the form *urn:ietf:params:netconf:capability:$name:$version* their shorthand can
44
    be used as a key. For example, for *urn:ietf:params:netconf:capability:candidate:1.0* the
45
    shorthand would be *:candidate*. If version is significant, use *:candidate:1.0* as key.
46
    """
47
    
48
    def __init__(self, capabilities):
49
        self._dict = {}
50
        for uri in capabilities:
51
            self._dict[uri] = _abbreviate(uri)
52

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

    
61
    def __len__(self):
62
        return len(self._dict)
63

    
64
    def __iter__(self):
65
        return self._dict.keys().__iter__()
66

    
67
    def __repr__(self):
68
        return repr(self._dict.keys())
69

    
70
    def __list__(self):
71
        return self._dict.keys()
72

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

    
77
    def remove(self, uri):
78
        "Remove a capability."
79
        if key in self._dict:
80
            del self._dict[key]
81
    
82
    #def get_uri(self, shorthand):
83
    #    "Returns the URI that is inferred for a given shorthand."
84
    #    for uri, abbrs in self._dict.items():
85
    #        if shorthand in abbrs:
86
    #            return uri