..
[ncclient] / ncclient / operations / edit.py
1 # Copyright 2009 Shikhar Bhushan
2 #
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
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 from ncclient.xml_ import *
16
17 from rpc import RPC
18
19 import util
20
21 import logging
22
23 logger = logging.getLogger("ncclient.operations.edit")
24
25 "Operations related to changing device configuration"
26
27 class EditConfig(RPC):
28     "`edit-config` RPC"
29
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.
32
33         *target* is the name of the configuration datastore being edited
34
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`.
36
37         *default_operation* if specified must be one of { `"merge"`, `"replace"`, or `"none"` }
38
39         *test_option* if specified must be one of { `"test_then_set"`, `"set"` }
40
41         *error_option* if specified must be one of { `"stop-on-error"`, `"continue-on-error"`, `"rollback-on-error"` }
42
43         The `"rollback-on-error"` *error_option* depends on the `:rollback-on-error` capability.
44         """
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)
59
60
61 class DeleteConfig(RPC):
62     "`delete-config` RPC"
63
64     def request(self, target):
65         """Delete a configuration datastore.
66
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)
72
73
74 class CopyConfig(RPC):
75     "`copy-config` RPC"
76
77     def request(self, source, target):
78         """Create or replace an entire configuration datastore with the contents of another complete
79         configuration datastore.
80
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`
83
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)
90
91
92 class Validate(RPC):
93     "`validate` RPC. Depends on the `:validate` capability."
94
95     DEPENDS = [':validate']
96
97     def request(self, source):
98         """Validate the contents of the specified configuration.
99
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")
103         try:
104             src = validated_element(source, ("config", qualify("config")))
105         except Exception as e:
106             logger.debug(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)
110
111
112 class Commit(RPC):
113     "`commit` RPC. Depends on the `:candidate` capability, and the `:confirmed-commit`."
114
115     DEPENDS = [':candidate']
116
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.
119
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.
121
122         :param confirmed: whether this is a confirmed commit
123         :type confirmed: bool
124
125         :param timeout: confirm timeout in seconds
126         :type timeout: int"""
127         node = new_ele("commit")
128         if confirmed:
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)
134
135
136 class DiscardChanges(RPC):
137     "`discard-changes` RPC. Depends on the `:candidate` capability."
138
139     DEPENDS = [":candidate"]
140
141     def request(self):
142         """Revert the candidate configuration to the currently running configuration. Any uncommitted changes are discarded."""
143         return self._request(new_ele("discard-changes"))