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 *
23 logger = logging.getLogger("ncclient.operations.edit")
25 "Operations related to changing device configuration"
27 class EditConfig(RPC):
30 def request(self, target, config, default_operation=None, test_option=None, error_option=None):
31 """Loads all or part of the specified *config* to the *target* configuration datastore.
33 *target* is the name of the configuration datastore being edited
35 *config* is the configuration, which must be rooted in the `config` element. It can be specified either as a string or an :class:`~xml.etree.ElementTree.Element`.
37 *default_operation* if specified must be one of { `"merge"`, `"replace"`, or `"none"` }
39 *test_option* if specified must be one of { `"test_then_set"`, `"set"` }
41 *error_option* if specified must be one of { `"stop-on-error"`, `"continue-on-error"`, `"rollback-on-error"` }
43 The `"rollback-on-error"` *error_option* depends on the `:rollback-on-error` capability.
45 node = new_ele("edit-config")
46 node.append(util.datastore_or_url("target", target, self._assert))
47 if error_option is not None:
48 if error_option == "rollback-on-error":
49 self._assert(":rollback-on-error")
50 sub_ele(node, "error-option").text = error_option
51 if test_option is not None:
52 self._assert(':validate')
53 sub_ele(node, "test-option").text = test_option
54 if default_operation is not None:
55 # TODO: check if it is a valid default-operation
56 sub_ele(node, "default-operation").text = default_operation
57 node.append(validated_element(config, ("config", qualify("config"))))
58 return self._request(node)
61 class DeleteConfig(RPC):
64 def request(self, target):
65 """Delete a configuration datastore.
67 :param target: name or URL of configuration datastore to delete
68 :type: :ref:`srctarget_params`"""
69 node = new_ele("delete-config")
70 node.append(util.datastore_or_url("target", target, self._assert))
71 return self._request(node)
74 class CopyConfig(RPC):
77 def request(self, source, target):
78 """Create or replace an entire configuration datastore with the contents of another complete
79 configuration datastore.
81 :param source: configuration datastore to use as the source of the copy operation or `config` element containing the configuration subtree to copy
82 :type source: :ref:`srctarget_params`
84 :param target: configuration datastore to use as the destination of the copy operation
85 :type target: :ref:`srctarget_params`"""
86 node = new_ele("copy-config")
87 node.append(util.datastore_or_url("target", target, self._assert))
88 node.append(util.datastore_or_url("source", source, self._assert))
89 return self._request(node)
93 "`validate` RPC. Depends on the `:validate` capability."
95 DEPENDS = [':validate']
97 def request(self, source):
98 """Validate the contents of the specified configuration.
100 :param source: name of the configuration datastore being validated or `config` element containing the configuration subtree to be validated
101 :type source: :ref:`srctarget_params`"""
102 node = new_ele("validate")
104 src = validated_element(source, ("config", qualify("config")))
105 except Exception as e:
107 src = util.datastore_or_url("source", source, self._assert)
108 (node if src.tag == "source" else sub_ele(node, "source")).append(src)
109 return self._request(node)
113 "`commit` RPC. Depends on the `:candidate` capability, and the `:confirmed-commit`."
115 DEPENDS = [':candidate']
117 def request(self, confirmed=False, timeout=None):
118 """Commit the candidate configuration as the device's new current configuration. Depends on the `:candidate` capability.
120 A confirmed commit (i.e. if *confirmed* is `True`) is reverted if there is no followup commit within the *timeout* interval. If no timeout is specified the confirm timeout defaults to 600 seconds (10 minutes). A confirming commit may have the *confirmed* parameter but this is not required. Depends on the `:confirmed-commit` capability.
122 :param confirmed: whether this is a confirmed commit
123 :type confirmed: bool
125 :param timeout: confirm timeout in seconds
126 :type timeout: int"""
127 node = new_ele("commit")
129 self._assert(":confirmed-commit")
130 sub_ele(node, "confirmed")
131 if timeout is not None:
132 sub_ele(node, "confirm-timeout").text = timeout
133 return self._request(node)
136 class DiscardChanges(RPC):
137 "`discard-changes` RPC. Depends on the `:candidate` capability."
139 DEPENDS = [":candidate"]
142 """Revert the candidate configuration to the currently running configuration. Any uncommitted changes are discarded."""
143 return self._request(new_ele("discard-changes"))