Statistics
| Branch: | Tag: | Revision:

root / ncclient / capabilities.py @ e769089a

History | View | Annotate | Download (2.3 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 216bb34c Shikhar Bhushan
def _abbreviate(uri):
16 e769089a Leonidas Poulopoulos
    if uri.startswith("urn:ietf:params") and ":netconf:" in uri:
17 9667bcb2 Shikhar Bhushan
        splitted = uri.split(":")
18 9667bcb2 Shikhar Bhushan
        if ":capability:" in uri:
19 e769089a Leonidas Poulopoulos
            if uri.startswith("urn:ietf:params:xml:ns:netconf"):
20 e769089a Leonidas Poulopoulos
                name, version = splitted[7], splitted[8]
21 e769089a Leonidas Poulopoulos
            else:
22 e769089a Leonidas Poulopoulos
                name, version = splitted[5], splitted[6]
23 b2d60e49 Shikhar Bhushan
            return [ ":" + name, ":" + name + ":" + version ]
24 9667bcb2 Shikhar Bhushan
        elif ":base:" in uri:
25 e769089a Leonidas Poulopoulos
            if uri.startswith("urn:ietf:params:xml:ns:netconf"):
26 e769089a Leonidas Poulopoulos
                return [ ":base", ":base" + ":" + splitted[7] ]
27 e769089a Leonidas Poulopoulos
            else:
28 e769089a Leonidas Poulopoulos
                return [ ":base", ":base" + ":" + splitted[5] ]
29 dd225c7a Shikhar Bhushan
    return []
30 216bb34c Shikhar Bhushan
31 216bb34c Shikhar Bhushan
def schemes(url_uri):
32 19e7c7f6 Shikhar Bhushan
    "Given a URI that has a *scheme* query string (i.e. `:url` capability URI), will return a list of supported schemes."
33 9667bcb2 Shikhar Bhushan
    return url_uri.partition("?scheme=")[2].split(",")
34 78f6c132 Shikhar Bhushan
35 d095a59e Shikhar Bhushan
class Capabilities:
36 a7cb58ce Shikhar Bhushan
37 19e7c7f6 Shikhar Bhushan
    "Represents the set of capabilities available to a NETCONF client or server. It is initialized with a list of capability URI's."
38 68ac4439 Shikhar Bhushan
    
39 216bb34c Shikhar Bhushan
    def __init__(self, capabilities):
40 d095a59e Shikhar Bhushan
        self._dict = {}
41 216bb34c Shikhar Bhushan
        for uri in capabilities:
42 216bb34c Shikhar Bhushan
            self._dict[uri] = _abbreviate(uri)
43 a7cb58ce Shikhar Bhushan
44 d095a59e Shikhar Bhushan
    def __contains__(self, key):
45 a7cb58ce Shikhar Bhushan
        if key in self._dict:
46 a7cb58ce Shikhar Bhushan
            return True
47 216bb34c Shikhar Bhushan
        for abbrs in self._dict.values():
48 216bb34c Shikhar Bhushan
            if key in abbrs:
49 a7cb58ce Shikhar Bhushan
                return True
50 a7cb58ce Shikhar Bhushan
        return False
51 a7cb58ce Shikhar Bhushan
52 216bb34c Shikhar Bhushan
    def __len__(self):
53 216bb34c Shikhar Bhushan
        return len(self._dict)
54 216bb34c Shikhar Bhushan
55 d095a59e Shikhar Bhushan
    def __iter__(self):
56 b2d60e49 Shikhar Bhushan
        return self._dict.iterkeys()
57 a7cb58ce Shikhar Bhushan
58 d095a59e Shikhar Bhushan
    def __repr__(self):
59 d095a59e Shikhar Bhushan
        return repr(self._dict.keys())
60 a7cb58ce Shikhar Bhushan
61 216bb34c Shikhar Bhushan
    def add(self, uri):
62 4bc8021f Shikhar Bhushan
        "Add a capability."
63 216bb34c Shikhar Bhushan
        self._dict[uri] = _abbreviate(uri)
64 a7cb58ce Shikhar Bhushan
65 216bb34c Shikhar Bhushan
    def remove(self, uri):
66 4bc8021f Shikhar Bhushan
        "Remove a capability."
67 d095a59e Shikhar Bhushan
        if key in self._dict:
68 b2d60e49 Shikhar Bhushan
            del self._dict[key]