root / ncclient / operations / edit.py @ 6e571704
History | View | Annotate | Download (5.6 kB)
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 |
*target* specifies the name or URL of configuration datastore to delete
|
68 |
|
69 |
:seealso: :ref:`srctarget_params`"""
|
70 |
node = new_ele("delete-config")
|
71 |
node.append(util.datastore_or_url("target", target, self._assert)) |
72 |
return self._request(node) |
73 |
|
74 |
|
75 |
class CopyConfig(RPC): |
76 |
"`copy-config` RPC"
|
77 |
|
78 |
def request(self, source, target): |
79 |
"""Create or replace an entire configuration datastore with the contents of another complete
|
80 |
configuration datastore.
|
81 |
|
82 |
*source* is the name of the configuration datastore to use as the source of the copy operation or `config` element containing the configuration subtree to copy
|
83 |
|
84 |
*target* is the name of the configuration datastore to use as the destination of the copy operation
|
85 |
|
86 |
:seealso: :ref:`srctarget_params`"""
|
87 |
node = new_ele("copy-config")
|
88 |
node.append(util.datastore_or_url("target", target, self._assert)) |
89 |
node.append(util.datastore_or_url("source", source, self._assert)) |
90 |
return self._request(node) |
91 |
|
92 |
|
93 |
class Validate(RPC): |
94 |
"`validate` RPC. Depends on the `:validate` capability."
|
95 |
|
96 |
DEPENDS = [':validate']
|
97 |
|
98 |
def request(self, source): |
99 |
"""Validate the contents of the specified configuration.
|
100 |
|
101 |
*source* is the name of the configuration datastore being validated or `config` element containing the configuration subtree to be validated
|
102 |
|
103 |
:seealso: :ref:`srctarget_params`"""
|
104 |
node = new_ele("validate")
|
105 |
try:
|
106 |
src = validated_element(source, ("config", qualify("config"))) |
107 |
except Exception as e: |
108 |
logger.debug(e) |
109 |
src = util.datastore_or_url("source", source, self._assert) |
110 |
(node if src.tag == "source" else sub_ele(node, "source")).append(src) |
111 |
return self._request(node) |
112 |
|
113 |
|
114 |
class Commit(RPC): |
115 |
"`commit` RPC. Depends on the `:candidate` capability, and the `:confirmed-commit`."
|
116 |
|
117 |
DEPENDS = [':candidate']
|
118 |
|
119 |
def request(self, confirmed=False, timeout=None): |
120 |
"""Commit the candidate configuration as the device's new current configuration. Depends on the `:candidate` capability.
|
121 |
|
122 |
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.
|
123 |
|
124 |
*confirmed* whether this is a confirmed commit
|
125 |
|
126 |
*timeout* specifies the confirm timeout in seconds"""
|
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")) |