| 1 |
""" |
| 2 |
Handler for default device information. |
| 3 |
|
| 4 |
Some devices require very specific information and action during client interaction. |
| 5 |
|
| 6 |
The "device handlers" provide a number of callbacks that return the necessary |
| 7 |
information. This allows the ncclient code to merely call upon this device handler - |
| 8 |
once configured - instead of cluttering its code with if-statements. |
| 9 |
|
| 10 |
Initially, not much is dealt with by the handler. However, in the future, as more |
| 11 |
devices with specific handling are added, more handlers and more functions should be |
| 12 |
implememted here, so that the ncclient code can use these callbacks to fill in the |
| 13 |
device specific information. |
| 14 |
|
| 15 |
Note that for proper import, the classname has to be: |
| 16 |
|
| 17 |
"<Devicename>DeviceHandler" |
| 18 |
|
| 19 |
...where <Devicename> is something like "Default", "Nexus", etc. |
| 20 |
|
| 21 |
All device-specific handlers derive from the DefaultDeviceHandler, which implements the |
| 22 |
generic information needed for interaction with a Netconf server. |
| 23 |
|
| 24 |
""" |
| 25 |
|
| 26 |
from ncclient.transport.parser import DefaultXMLParser |
| 27 |
|
| 28 |
|
| 29 |
class DefaultDeviceHandler: |
| 30 |
""" |
| 31 |
Default handler for device specific information. |
| 32 |
|
| 33 |
""" |
| 34 |
# Define the exempt error messages (those that shouldn't cause an exception). |
| 35 |
# Wild cards are possible: Start and/or end with a '*' to indicate that the text |
| 36 |
# can appear at the start, the end or the middle of the error message to still |
| 37 |
# match. All comparisons are case insensitive. |
| 38 |
_EXEMPT_ERRORS = [] |
| 39 |
|
| 40 |
_BASE_CAPABILITIES = [ |
| 41 |
"urn:ietf:params:netconf:base:1.0", |
| 42 |
"urn:ietf:params:netconf:base:1.1", |
| 43 |
"urn:ietf:params:netconf:capability:writable-running:1.0", |
| 44 |
"urn:ietf:params:netconf:capability:candidate:1.0", |
| 45 |
"urn:ietf:params:netconf:capability:confirmed-commit:1.0", |
| 46 |
"urn:ietf:params:netconf:capability:rollback-on-error:1.0", |
| 47 |
"urn:ietf:params:netconf:capability:startup:1.0", |
| 48 |
"urn:ietf:params:netconf:capability:url:1.0?scheme=http,ftp,file,https,sftp", |
| 49 |
"urn:ietf:params:netconf:capability:validate:1.0", |
| 50 |
"urn:ietf:params:netconf:capability:xpath:1.0", |
| 51 |
"urn:ietf:params:netconf:capability:notification:1.0", |
| 52 |
"urn:ietf:params:netconf:capability:interleave:1.0", |
| 53 |
"urn:ietf:params:netconf:capability:with-defaults:1.0" |
| 54 |
] |
| 55 |
|
| 56 |
def __init__(self, device_params=None, ignore_errors=None): |
| 57 |
self.device_params = device_params |
| 58 |
self.capabilities = [] |
| 59 |
self._EXEMPT_ERRORS = ignore_errors or self._EXEMPT_ERRORS |
| 60 |
# Turn all exempt errors into lower case, since we don't want those comparisons |
| 61 |
# to be case sensitive later on. Sort them into exact match, wildcard start, |
| 62 |
# wildcard end, and full wildcard categories, depending on whether they start |
| 63 |
# and/or end with a '*'. |
| 64 |
self._exempt_errors_exact_match = [] |
| 65 |
self._exempt_errors_startwith_wildcard_match = [] |
| 66 |
self._exempt_errors_endwith_wildcard_match = [] |
| 67 |
self._exempt_errors_full_wildcard_match = [] |
| 68 |
for i in range(len(self._EXEMPT_ERRORS)): |
| 69 |
e = self._EXEMPT_ERRORS[i].lower() |
| 70 |
if e.startswith("*"): |
| 71 |
if e.endswith("*"): |
| 72 |
self._exempt_errors_full_wildcard_match.append(e[1:-1]) |
| 73 |
else: |
| 74 |
self._exempt_errors_startwith_wildcard_match.append(e[1:]) |
| 75 |
elif e.endswith("*"): |
| 76 |
self._exempt_errors_endwith_wildcard_match.append(e[:-1]) |
| 77 |
else: |
| 78 |
self._exempt_errors_exact_match.append(e) |
| 79 |
|
| 80 |
|
| 81 |
def add_additional_ssh_connect_params(self, kwargs): |
| 82 |
""" |
| 83 |
Add device specific parameters for the SSH connect. |
| 84 |
|
| 85 |
Pass in the keyword-argument dictionary for the SSH connect call. The |
| 86 |
dictionary will be modified (!) with the additional device-specific parameters. |
| 87 |
|
| 88 |
""" |
| 89 |
pass |
| 90 |
|
| 91 |
def add_additional_netconf_params(self, kwargs): |
| 92 |
"""Add additional NETCONF parameters |
| 93 |
|
| 94 |
Accept a keyword-argument dictionary to add additional NETCONF |
| 95 |
parameters that may be in addition to those specified by the |
| 96 |
default and device specific handlers. |
| 97 |
|
| 98 |
Currently, only additional client specified capabilities are |
| 99 |
supported and will be appended to default and device specific |
| 100 |
capabilities. |
| 101 |
|
| 102 |
Args: |
| 103 |
kwargs: A dictionary of specific NETCONF parameters to |
| 104 |
apply in addition to those derived by default and |
| 105 |
device specific handlers. |
| 106 |
""" |
| 107 |
self.capabilities = kwargs.pop("capabilities", []) |
| 108 |
|
| 109 |
def get_capabilities(self): |
| 110 |
""" |
| 111 |
Return the capability list. |
| 112 |
|
| 113 |
A list of URI's representing the client's capabilities. This is used during |
| 114 |
the initial capability exchange. Modify (in a new device-handler subclass) |
| 115 |
as needed. |
| 116 |
|
| 117 |
""" |
| 118 |
return self._BASE_CAPABILITIES + self.capabilities |
| 119 |
|
| 120 |
def get_xml_base_namespace_dict(self): |
| 121 |
""" |
| 122 |
A dictionary containing the base namespace. |
| 123 |
|
| 124 |
For lxml's nsmap, the base namespace should have a 'None' key. |
| 125 |
|
| 126 |
{ |
| 127 |
None: "... base namespace... " |
| 128 |
} |
| 129 |
|
| 130 |
If no base namespace is needed, an empty dictionary should be |
| 131 |
returned. |
| 132 |
|
| 133 |
""" |
| 134 |
return {} |
| 135 |
|
| 136 |
def get_xml_extra_prefix_kwargs(self): |
| 137 |
""" |
| 138 |
Return any extra prefix that should be sent with each RPC request. |
| 139 |
|
| 140 |
Since these are used as kwargs, the function should return |
| 141 |
either an empty dictionary if there are no additional arguments, |
| 142 |
or a dictionary with keyword parameters suitable fo the Element() |
| 143 |
function. Mostly, this is the "nsmap" argument. |
| 144 |
|
| 145 |
{ |
| 146 |
"nsmap" : { |
| 147 |
... namespace definitions ... |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
""" |
| 152 |
return {} |
| 153 |
|
| 154 |
def get_ssh_subsystem_names(self): |
| 155 |
""" |
| 156 |
Return a list of names to try for the SSH subsystems. |
| 157 |
|
| 158 |
This always returns a list, even if only a single subsystem name is used. |
| 159 |
|
| 160 |
If the returned list contains multiple names then the various subsystems are |
| 161 |
tried in order, until one of them can successfully connect. |
| 162 |
|
| 163 |
""" |
| 164 |
return [ "netconf" ] |
| 165 |
|
| 166 |
def is_rpc_error_exempt(self, error_text): |
| 167 |
""" |
| 168 |
Check whether an RPC error message is excempt, thus NOT causing an exception. |
| 169 |
|
| 170 |
On some devices the RPC operations may indicate an error response, even though |
| 171 |
the operation actually succeeded. This may be in cases where a warning would be |
| 172 |
more appropriate. In that case, the client may be better advised to simply |
| 173 |
ignore that error and not raise an exception. |
| 174 |
|
| 175 |
Note that there is also the "raise_mode", set on session and manager, which |
| 176 |
controls the exception-raising behaviour in case of returned errors. This error |
| 177 |
filter here is independent of that: No matter what the raise_mode says, if the |
| 178 |
error message matches one of the exempt errors returned here, an exception |
| 179 |
will not be raised. |
| 180 |
|
| 181 |
The exempt error messages are defined in the _EXEMPT_ERRORS field of the device |
| 182 |
handler object and can be overwritten by child classes. Wild cards are |
| 183 |
possible: Start and/or end with a '*' to indicate that the text can appear at |
| 184 |
the start, the end or the middle of the error message to still match. All |
| 185 |
comparisons are case insensitive. |
| 186 |
|
| 187 |
Return True/False depending on found match. |
| 188 |
|
| 189 |
""" |
| 190 |
if error_text is not None: |
| 191 |
error_text = error_text.lower().strip() |
| 192 |
else: |
| 193 |
error_text = 'no error given' |
| 194 |
|
| 195 |
# Compare the error text against all the exempt errors. |
| 196 |
for ex in self._exempt_errors_exact_match: |
| 197 |
if error_text == ex: |
| 198 |
return True |
| 199 |
|
| 200 |
for ex in self._exempt_errors_startwith_wildcard_match: |
| 201 |
if error_text.endswith(ex): |
| 202 |
return True |
| 203 |
|
| 204 |
for ex in self._exempt_errors_endwith_wildcard_match: |
| 205 |
if error_text.startswith(ex): |
| 206 |
return True |
| 207 |
|
| 208 |
for ex in self._exempt_errors_full_wildcard_match: |
| 209 |
if ex in error_text: |
| 210 |
return True |
| 211 |
|
| 212 |
return False |
| 213 |
|
| 214 |
|
| 215 |
def perform_qualify_check(self): |
| 216 |
""" |
| 217 |
During RPC operations, we perform some initial sanity checks on the responses. |
| 218 |
|
| 219 |
This check will fail for some devices, in which case this function here should |
| 220 |
return False in order to skip the test. |
| 221 |
|
| 222 |
""" |
| 223 |
return True |
| 224 |
|
| 225 |
|
| 226 |
def add_additional_operations(self): |
| 227 |
""" |
| 228 |
Add device/vendor specific operations. |
| 229 |
|
| 230 |
""" |
| 231 |
return {} |
| 232 |
|
| 233 |
|
| 234 |
def handle_raw_dispatch(self, raw): |
| 235 |
return False |
| 236 |
|
| 237 |
|
| 238 |
def handle_connection_exceptions(self, sshsession): |
| 239 |
return False |
| 240 |
|
| 241 |
def reply_parsing_error_transform(self, reply_cls): |
| 242 |
""" |
| 243 |
Hook for working around bugs in replies from devices (the root element can be "fixed") |
| 244 |
|
| 245 |
:param reply_cls: the RPCReply class that is parsing the reply 'root' xml element |
| 246 |
|
| 247 |
:return: transform function for the 'root' xml element of the RPC reply in case the normal parsing fails |
| 248 |
""" |
| 249 |
# No transformation by default |
| 250 |
return None |
| 251 |
|
| 252 |
def transform_reply(self): |
| 253 |
return False |
| 254 |
|
| 255 |
def transform_edit_config(self, node): |
| 256 |
""" |
| 257 |
Hook for working around bugs in devices that cannot deal with |
| 258 |
standard config payloads for edits. This will be called |
| 259 |
in EditConfig.request just before the request is submitted, |
| 260 |
meaning it will get an XML tree rooted at edit-config. |
| 261 |
|
| 262 |
:param node: the XML tree for edit-config |
| 263 |
|
| 264 |
:return: either the original XML tree if no changes made or a modified XML tree |
| 265 |
""" |
| 266 |
return node |
| 267 |
|
| 268 |
def get_xml_parser(self, session): |
| 269 |
""" |
| 270 |
vendor can chose which parser to use for RPC reply response. |
| 271 |
Default being DOM |
| 272 |
|
| 273 |
:param session: ssh session object |
| 274 |
:return: default DOM parser |
| 275 |
""" |
| 276 |
return DefaultXMLParser(session) |