1 # Copyright 2009 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 from ncclient.xml_ import *
22 logger = logging.getLogger("ncclient.operations.edit")
24 "Operations related to changing device configuration"
26 class EditConfig(RPC):
30 def request(self, target, config, default_operation=None, test_option=None, error_option=None):
31 node = new_ele("edit-config")
32 node.append(util.datastore_or_url("target", target, self._assert))
33 if error_option is not None:
34 if error_option == "rollback-on-error":
35 self._assert(":rollback-on-error")
36 sub_ele(node, "error-option").text = error_option
37 if test_option is not None:
38 self._assert(':validate')
39 sub_ele(node, "test-option").text = test_option
40 if default_operation is not None:
41 # TODO: check if it is a valid default-operation
42 sub_ele(node, "default-operation").text = default_operation
43 node.append(validated_element(config, ("config", qualify("config"))))
44 return self._request(node)
47 class DeleteConfig(RPC):
49 "*<delete-config>* RPC"
51 def request(self, target):
52 node = new_ele("delete-config")
53 node.append(util.datastore_or_url("target", target, self._assert))
54 return self._request(node)
57 class CopyConfig(RPC):
61 def request(self, source, target):
62 node = new_ele("copy-config")
63 node.append(util.datastore_or_url("target", target, self._assert))
64 node.append(util.datastore_or_url("source", source, self._assert))
65 return self._request(node)
70 "*<validate>* RPC. Depends on the *:validate* capability."
72 DEPENDS = [':validate']
74 def request(self, source):
75 node = new_ele("validate")
77 src = validated_element(source, ("config", qualify("config")))
78 except Exception as e:
80 src = util.datastore_or_url("source", source, self._assert)
81 (node if src.tag == "source" else sub_ele(node, "source")).append(src)
82 return self._request(node)
87 "*<commit>* RPC. Depends on the *:candidate* capability, and the *:confirmed-commit* "
89 DEPENDS = [':candidate']
91 def request(self, confirmed=False, timeout=None):
92 node = new_ele("commit")
94 self._assert(":confirmed-commit")
95 sub_ele(node, "confirmed")
96 if timeout is not None:
97 sub_ele(node, "confirm-timeout").text = timeout
98 return self._request(node)
101 class DiscardChanges(RPC):
103 "*<discard-changes>* RPC. Depends on the *:candidate* capability."
105 DEPENDS = [":candidate"]
108 return self._request(new_ele("discard-changes"))