1 # Copyright 2h009 Shikhar Bhushan
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 "Locking-related NETCONF operations"
17 from ncclient.xml_ import *
19 from rpc import RaiseMode, RPC
21 # TODO: parse session-id from a lock-denied error, and raise a tailored exception?
27 def request(self, target):
28 """Allows the client to lock the configuration system of a device.
30 *target* is the name of the configuration datastore to lock
32 node = new_ele("lock")
33 sub_ele(sub_ele(node, "target"), target)
34 return self._request(node)
41 def request(self, target):
42 """Release a configuration lock, previously obtained with the lock operation.
44 *target* is the name of the configuration datastore to unlock
46 node = new_ele("unlock")
47 sub_ele(sub_ele(node, "target"), target)
48 return self._request(node)
53 """A context manager for the :class:`Lock` / :class:`Unlock` pair of RPC's.
55 Any `rpc-error` will be raised as an exception.
57 Initialise with (:class:`Session <ncclient.transport.Session>`) instance and lock target.
60 def __init__(self, session, target):
61 self.session = session
65 Lock(self.session, raise_mode=RaiseMode.ERRORS).request(self.target)
68 def __exit__(self, *args):
69 Unlock(self.session, raise_mode=RaiseMode.ERRORS).request(self.target)