Skip to content

Commit 3f892a7

Browse files
authored
Merge pull request #33 from mirceaulinic/master
Enhanced alive
2 parents 4b8eab5 + 9724958 commit 3f892a7

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

pyIOSXR/exceptions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,15 @@ class InvalidXMLResponse(IOSXRException):
8989

9090
pass
9191

92+
9293
class TimeoutError(IOSXRException):
9394
"""TimeoutError Exception."""
9495

95-
pass
96+
def __init__(self, msg=None, dev=None):
97+
super(TimeoutError, self).__init__(msg=msg, dev=dev)
98+
if dev:
99+
self._xr = dev
100+
self._xr._xml_agent_alive = False
96101

97102

98103
class EOFError(IOSXRException):

pyIOSXR/iosxr.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ def make_rpc_call(self, rpc_command):
140140
:param rpc_command: (str) rpc command such as:
141141
<Get><Operational><LLDP><NodeTable></NodeTable></LLDP></Operational></Get>
142142
"""
143+
# ~~~ hack: ~~~
144+
if not self.is_alive():
145+
self.close() # force close for safety
146+
self.open() # reopen
147+
# ~~~ end hack ~~~
143148
result = self._execute_rpc(rpc_command)
144149
return ET.tostring(result)
145150

@@ -163,14 +168,15 @@ def open(self):
163168
raise ConnectError(au_err.message)
164169

165170
self._cli_prompt = self.device.find_prompt() # get the prompt
166-
167171
self._enter_xml_mode()
168172

169173
def is_alive(self):
170174
"""
171-
Returns the XML agent connection state.
175+
Returns the XML agent connection state (and SSH connection state).
172176
"""
173-
return self._xml_agent_alive
177+
if hasattr(self.device, 'remote_conn'):
178+
return self.device.remote_conn.transport.is_active() and self._xml_agent_alive
179+
return False # remote_conn not there => connection not init => not alive
174180

175181
def _timeout_exceeded(self, start=None, msg='Timeout exceeded!'):
176182
if not start:
@@ -449,9 +455,10 @@ def close(self):
449455
Clean up after you are done and explicitly close the router connection.
450456
"""
451457
if self.lock_on_connect or self.locked:
452-
self.unlock()
453-
self._unlock_xml_agent()
454-
self.device.remote_conn.close()
458+
self.unlock() # this refers to the config DB
459+
self._unlock_xml_agent() # this refers to the XML agent
460+
if hasattr(self.device, 'remote_conn'):
461+
self.device.remote_conn.close() # close the underlying SSH session
455462

456463
def lock(self):
457464
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# e.g. ['django==1.5.1', 'mezzanine==1.4.6']
2929
reqs = [str(ir.req) for ir in install_reqs]
3030

31-
version = '0.30'
31+
version = '0.31'
3232

3333
setup(
3434
name='pyIOSXR',

0 commit comments

Comments
 (0)