| 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 |
import logging |
| 16 |
|
| 17 |
|
| 18 |
logger = logging.getLogger("ncclient.capabilities") |
| 19 |
|
| 20 |
|
| 21 |
def _abbreviate(uri): |
| 22 |
if uri.startswith("urn:ietf:params") and ":netconf:" in uri: |
| 23 |
splitted = uri.split(":") |
| 24 |
if ":capability:" in uri: |
| 25 |
if uri.startswith("urn:ietf:params:xml:ns:netconf"): |
| 26 |
name, version = splitted[7], splitted[8] |
| 27 |
else: |
| 28 |
name, version = splitted[5], splitted[6] |
| 29 |
return [ ":" + name, ":" + name + ":" + version ] |
| 30 |
elif ":base:" in uri: |
| 31 |
if uri.startswith("urn:ietf:params:xml:ns:netconf"): |
| 32 |
return [ ":base", ":base" + ":" + splitted[7] ] |
| 33 |
else: |
| 34 |
return [ ":base", ":base" + ":" + splitted[5] ] |
| 35 |
return [] |
| 36 |
|
| 37 |
def schemes(url_uri): |
| 38 |
"Given a URI that has a *scheme* query string (i.e. `:url` capability URI), will return a list of supported schemes." |
| 39 |
return url_uri.partition("?scheme=")[2].split(",") |
| 40 |
|
| 41 |
class Capabilities: |
| 42 |
|
| 43 |
"Represents the set of capabilities available to a NETCONF client or server. It is initialized with a list of capability URI's." |
| 44 |
|
| 45 |
def __init__(self, capabilities): |
| 46 |
self._dict = {} |
| 47 |
for uri in capabilities: |
| 48 |
self.add(uri) |
| 49 |
|
| 50 |
def __contains__(self, key): |
| 51 |
try: |
| 52 |
self.__getitem__(key) |
| 53 |
except KeyError: |
| 54 |
return False |
| 55 |
else: |
| 56 |
return True |
| 57 |
|
| 58 |
def __getitem__(self, key): |
| 59 |
try: |
| 60 |
return self._dict[key] |
| 61 |
except KeyError: |
| 62 |
for capability in self._dict.values(): |
| 63 |
if key in capability.get_abbreviations(): |
| 64 |
return capability |
| 65 |
|
| 66 |
raise KeyError(key) |
| 67 |
|
| 68 |
def __len__(self): |
| 69 |
return len(self._dict) |
| 70 |
|
| 71 |
def __iter__(self): |
| 72 |
return iter(self._dict.keys()) |
| 73 |
|
| 74 |
def __repr__(self): |
| 75 |
return repr(self._dict.keys()) |
| 76 |
|
| 77 |
def add(self, uri): |
| 78 |
"Add a capability." |
| 79 |
self._dict[uri] = Capability.from_uri(uri) |
| 80 |
|
| 81 |
def remove(self, uri): |
| 82 |
"Remove a capability." |
| 83 |
if uri in self._dict: |
| 84 |
del self._dict[uri] |
| 85 |
|
| 86 |
|
| 87 |
class Capability: |
| 88 |
|
| 89 |
"""Represents a single capability""" |
| 90 |
|
| 91 |
def __init__(self, namespace_uri, parameters=None): |
| 92 |
self.namespace_uri = namespace_uri |
| 93 |
self.parameters = parameters or {} |
| 94 |
|
| 95 |
@classmethod |
| 96 |
def from_uri(cls, uri): |
| 97 |
split_uri = uri.split("?") |
| 98 |
namespace_uri = split_uri[0] |
| 99 |
capability = cls(namespace_uri) |
| 100 |
|
| 101 |
try: |
| 102 |
param_string = split_uri[1] |
| 103 |
except IndexError: |
| 104 |
return capability |
| 105 |
|
| 106 |
capability.parameters = { |
| 107 |
param.key: param.value |
| 108 |
for param in _parse_parameter_string(param_string, uri) |
| 109 |
} |
| 110 |
|
| 111 |
return capability |
| 112 |
|
| 113 |
def __eq__(self, other): |
| 114 |
return ( |
| 115 |
self.namespace_uri == other.namespace_uri and |
| 116 |
self.parameters == other.parameters |
| 117 |
) |
| 118 |
|
| 119 |
def get_abbreviations(self): |
| 120 |
return _abbreviate(self.namespace_uri) |
| 121 |
|
| 122 |
|
| 123 |
def _parse_parameter_string(string, uri): |
| 124 |
for param_string in string.split("&"): |
| 125 |
try: |
| 126 |
yield _Parameter.from_string(param_string) |
| 127 |
except _InvalidParameter: |
| 128 |
logger.error( |
| 129 |
"Invalid parameter '{param}' in capability URI '{uri}'".format( |
| 130 |
param=param_string, |
| 131 |
uri=uri, |
| 132 |
) |
| 133 |
) |
| 134 |
|
| 135 |
|
| 136 |
class _Parameter: |
| 137 |
|
| 138 |
"""Represents a parameter to a capability""" |
| 139 |
|
| 140 |
def __init__(self, key, value): |
| 141 |
self.key = key |
| 142 |
self.value = value |
| 143 |
|
| 144 |
@classmethod |
| 145 |
def from_string(cls, string): |
| 146 |
try: |
| 147 |
key, value = string.split("=") |
| 148 |
except ValueError: |
| 149 |
raise _InvalidParameter |
| 150 |
|
| 151 |
return cls(key, value) |
| 152 |
|
| 153 |
|
| 154 |
class _InvalidParameter(Exception): |
| 155 |
|
| 156 |
pass |