Debian Codegraph @

lib/jnpr/junos/exception.py

raw
1 import re
2 from jnpr.junos import jxml
3 from jnpr.junos import jxml as JXML
4 from lxml.etree import _Element
5 from ncclient.operations.rpc import RPCError
6
7
8 class FactLoopError(RuntimeError):
9 """
10 Generated when there is a loop in fact gathering.
11 """
12
13 pass
14
15
16 class RpcError(Exception):
17 """
18 Parent class for all junos-pyez RPC Exceptions
19 """
20
21 def __init__(self, cmd=None, rsp=None, errs=None, dev=None, timeout=None, re=None):
22 """
23 :cmd: is the rpc command
24 :rsp: is the rpc response (after <rpc-reply>)
25 :errs: is a list of dictionaries of extracted <rpc-error> elements.
26 :dev: is the device rpc was executed on
27 :timeout: is the timeout value of the device
28 :re: is the RE or member exception occured on
29 """
30 self.cmd = cmd
31 self.rsp = rsp
32 self.dev = dev
33 self.timeout = timeout
34 self.re = re
35 self.rpc_error = None
36 self.xml = rsp
37 # To handle errors coming from ncclient, Here errs is list of RPCError
38 if isinstance(errs, RPCError) and hasattr(errs, "errors"):
39 self.errs = [JXML.rpc_error(error.xml) for error in errs.errors]
40 for error in errs.errors:
41 if error.severity == "error":
42 self.rsp = JXML.remove_namespaces(error.xml)
43 break
44 else:
45 if errs.severity == "warning":
46 for error in errs.errors:
47 if error.severity == "warning":
48 self.rsp = JXML.remove_namespaces(error.xml)
49 break
50 self.message = errs.message
51 else:
52 self.errs = errs
53 self.message = (
54 "\n".join(
55 [
56 "%s: %s" % (err["severity"].strip(), err["message"].strip())
57 for err in errs
58 if err["message"] is not None and err["severity"] is not None
59 ]
60 )
61 if isinstance(errs, list)
62 else ""
63 )
64
65 if isinstance(self.rsp, _Element):
66 self.rpc_error = jxml.rpc_error(self.rsp)
67 self.message = self.message or self.rpc_error["message"]
68 if self.errs is None or not isinstance(self.errs, list):
69 self.errs = [self.rpc_error]
70
71 def __repr__(self):
72 """
73 pprints the response XML attribute
74 """
75 if self.rpc_error is not None:
76 return "{}(severity: {}, bad_element: {}, message: {})".format(
77 self.__class__.__name__,
78 self.rpc_error["severity"],
79 self.rpc_error["bad_element"],
80 self.message,
81 )
82 else:
83 return self.__class__.__name__
84
85 __str__ = __repr__
86
87
88 class CommitError(RpcError):
89 """
90 Generated in response to a commit-check or a commit action.
91 """
92
93 def __init__(self, rsp, cmd=None, errs=None):
94 RpcError.__init__(self, cmd, rsp, errs)
95
96 def __repr__(self):
97 return "{}(edit_path: {}, bad_element: {}, message: {})".format(
98 self.__class__.__name__,
99 self.rpc_error["edit_path"],
100 self.rpc_error["bad_element"],
101 self.message,
102 )
103
104 __str__ = __repr__
105
106
107 class ConfigLoadError(RpcError):
108 """
109 Generated in response to a failure when loading a configuration.
110 """
111
112 def __init__(self, rsp, cmd=None, errs=None):
113 RpcError.__init__(self, cmd, rsp, errs)
114
115 def __repr__(self):
116 return "{}(severity: {}, bad_element: {}, message: {})".format(
117 self.__class__.__name__,
118 self.rpc_error["severity"],
119 self.rpc_error["bad_element"],
120 self.message,
121 )
122
123 __str__ = __repr__
124
125
126 class LockError(RpcError):
127 """
128 Generated in response to attempting to take an exclusive
129 lock on the configuration database.
130 """
131
132 def __init__(self, rsp):
133 RpcError.__init__(self, rsp=rsp)
134
135
136 class UnlockError(RpcError):
137 """
138 Generated in response to attempting to unlock the
139 configuration database.
140 """
141
142 def __init__(self, rsp):
143 RpcError.__init__(self, rsp=rsp)
144
145
146 class PermissionError(RpcError):
147 """
148 Generated in response to invoking an RPC for which the
149 auth user does not have user-class permissions.
150
151 PermissionError.message gives you the specific RPC that cause
152 the exceptions
153 """
154
155 def __init__(self, rsp, cmd=None, errs=None):
156 RpcError.__init__(self, cmd=cmd, rsp=rsp, errs=errs)
157 self.message = rsp.findtext(".//bad-element")
158
159
160 class RpcTimeoutError(RpcError):
161 """
162 Generated in response to a RPC execution timeout.
163 """
164
165 def __init__(self, dev, cmd, timeout):
166 RpcError.__init__(self, dev=dev, cmd=cmd, timeout=timeout)
167
168 def __repr__(self):
169 return "{}(host: {}, cmd: {}, timeout: {})".format(
170 self.__class__.__name__, self.dev.hostname, self.cmd, self.timeout
171 )
172
173 __str__ = __repr__
174
175
176 class SwRollbackError(RpcError):
177 """
178 Generated in response to a SW rollback error.
179 """
180
181 def __init__(self, rsp, re=None):
182 RpcError.__init__(self, re=re, rsp=rsp)
183
184 def __repr__(self):
185 if self.re:
186 return "{}(re: {}, output: {})".format(
187 self.__class__.__name__, self.re, self.rsp
188 )
189 else:
190 return "{}(output: {})".format(self.__class__.__name__, self.rsp)
191
192 __str__ = __repr__
193
194
195 # ================================================================
196 # ================================================================
197 # Connection Exceptions
198 # ================================================================
199 # ================================================================
200
201
202 class ConnectError(Exception):
203 """
204 Parent class for all connection related exceptions
205 """
206
207 def __init__(self, dev, msg=None):
208 self.dev = dev
209 self._orig = msg
210
211 @property
212 def user(self):
213 """login user-name"""
214 return self.dev.user
215
216 @property
217 def host(self):
218 """login host name/ipaddr"""
219 return self.dev.hostname
220
221 @property
222 def port(self):
223 """login SSH port"""
224 return self.dev._port
225
226 @property
227 def msg(self):
228 """login SSH port"""
229 return self._orig
230
231 def __repr__(self):
232 if self._orig:
233 return "{}(host: {}, msg: {})".format(
234 self.__class__.__name__, self.dev.hostname, self._orig
235 )
236 else:
237 return "{}({})".format(self.__class__.__name__, self.dev.hostname)
238
239 __str__ = __repr__
240
241
242 class ProbeError(ConnectError):
243 """
244 Generated if auto_probe is enabled and the probe action fails
245 """
246
247 pass
248
249
250 class ConnectAuthError(ConnectError):
251 """
252 Generated if the user-name, password is invalid
253 """
254
255 pass
256
257
258 class ConnectTimeoutError(ConnectError):
259 """
260 Generated if the NETCONF session fails to connect, could
261 be due to the fact the device is not ip reachable; bad
262 ipaddr or just due to routing
263 """
264
265 pass
266
267
268 class ConnectUnknownHostError(ConnectError):
269 """
270 Generated if the specific hostname does not DNS resolve
271 """
272
273 pass
274
275
276 class ConnectRefusedError(ConnectError):
277 """
278 Generated if the specified host denies the NETCONF; could
279 be that the services is not enabled, or the host has
280 too many connections already.
281 """
282
283 pass
284
285
286 class ConnectNotMasterError(ConnectError):
287 """
288 Generated if the connection is made to a non-master
289 routing-engine. This could be a backup RE on an MX
290 device, or a virtual-chassis member (linecard), for example
291 """
292
293 pass
294
295
296 class ConnectClosedError(ConnectError):
297 """
298 Generated if connection unexpectedly closed
299 """
300
301 def __init__(self, dev):
302 ConnectError.__init__(self, dev=dev)
303 dev.connected = False
304
305
306 class JSONLoadError(Exception):
307 """
308 Generated if json content of rpc reply fails to load
309 """
310
311 def __init__(self, exception, rpc_content):
312 self.ex_msg = str(exception)
313 self.rpc_content = rpc_content
314 self.offending_line = ""
315 obj = re.search(r"line (\d+)", self.ex_msg)
316 if obj:
317 line_no = int(obj.group(1))
318 rpc_lines = rpc_content.splitlines()
319 for line in range(line_no - 3, line_no + 2):
320 self.offending_line += "%s: %s\n" % (line + 1, rpc_lines[line])
321
322 def __repr__(self):
323 if self.offending_line:
324 return (
325 "{}(reason: {}, \nThe offending config appears "
326 "to be: \n{})".format(
327 self.__class__.__name__, self.ex_msg, self.offending_line
328 )
329 )
330 else:
331 return "{}(reason: {})".format(self.__class__.__name__, self.ex_msg)
332
333 __str__ = __repr__