def execute(self, run_cmd_func, args=[], kwargs={}): """Safely execute the supplied function with args and kwargs. Args: run_cmd_func(executable): Function to be run. Returns: The return value of the supplied function. Raises: NCError: if there is an error in the NETCONF protocol. NCTimeoutError: if a client-side timeout has occured. ConnectionClosedError: if the NETCONF session is closed. """ if self.connected is not True: raise ConnectionClosedError(self) try: self.lock() rsp = run_cmd_func(*args, **kwargs) except RPCError as e: raise NCError(e) except NcOpErrors.TimeoutExpiredError: raise NCTimeoutError except NcTransErrors.TransportError: raise ConnectionClosedError(self) finally: self.unlock() return rsp
def lock(self, target='running'): """Attempt to lock the NETCONF connection. Raises: NCError: if there is an error in the NETCONF protocol. LockConflictError: if another process hold the NETCONF lock. """ try: self.connection.lock(target) self._locked = True except RPCError as e: if e.tag == 'lock-denied': raise LockConflictError else: raise NCError(e)
def unlock(self, target='running'): """Attempt to unlock the NETCONF connection. Raises: NCError: if there is an error in the NETCONF protocol. LockConflictError: if another process hold the NETCONF lock. """ try: if self._locked: self.connection.unlock(target) self._locked = False except RPCError as e: if e.tag == 'operation-failed'\ and 'Unlock Failed' in e.message: raise UnlockConflictError else: raise NCError(e) except NcTransErrors.TransportError: pass