| 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 ncclient.operations.rpc import RPC |
| 18 |
|
| 19 |
from ncclient.operations import util |
| 20 |
|
| 21 |
from .errors import OperationError |
| 22 |
|
| 23 |
import logging |
| 24 |
|
| 25 |
logger = logging.getLogger("ncclient.operations.edit") |
| 26 |
|
| 27 |
"Operations related to changing device configuration" |
| 28 |
|
| 29 |
class EditConfig(RPC): |
| 30 |
"`edit-config` RPC" |
| 31 |
|
| 32 |
def request(self, config, format='xml', target='candidate', default_operation=None, |
| 33 |
test_option=None, error_option=None): |
| 34 |
"""Loads all or part of the specified *config* to the *target* configuration datastore. |
| 35 |
|
| 36 |
*target* is the name of the configuration datastore being edited |
| 37 |
|
| 38 |
*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`. |
| 39 |
|
| 40 |
*default_operation* if specified must be one of { `"merge"`, `"replace"`, or `"none"` } |
| 41 |
|
| 42 |
*test_option* if specified must be one of { `"test-then-set"`, `"set"`, `"test-only"` } |
| 43 |
|
| 44 |
*error_option* if specified must be one of { `"stop-on-error"`, `"continue-on-error"`, `"rollback-on-error"` } |
| 45 |
|
| 46 |
The `"rollback-on-error"` *error_option* depends on the `:rollback-on-error` capability. |
| 47 |
""" |
| 48 |
node = new_ele("edit-config") |
| 49 |
node.append(util.datastore_or_url("target", target, self._assert)) |
| 50 |
if (default_operation is not None |
| 51 |
and util.validate_args('default_operation', default_operation, ["merge", "replace", "none"]) is True): |
| 52 |
sub_ele(node, "default-operation").text = default_operation |
| 53 |
if (test_option is not None |
| 54 |
and util.validate_args('test_option', test_option, ["test-then-set", "set", "test-only"]) is True): |
| 55 |
self._assert(':validate') |
| 56 |
if test_option == 'test-only': |
| 57 |
self._assert(':validate:1.1') |
| 58 |
sub_ele(node, "test-option").text = test_option |
| 59 |
if (error_option is not None |
| 60 |
and util.validate_args('error_option', error_option, ["stop-on-error", "continue-on-error", "rollback-on-error"]) is True): |
| 61 |
if error_option == "rollback-on-error": |
| 62 |
self._assert(":rollback-on-error") |
| 63 |
sub_ele(node, "error-option").text = error_option |
| 64 |
if format == 'xml': |
| 65 |
node.append(validated_element(config, ("config", qualify("config")))) |
| 66 |
elif format == 'text': |
| 67 |
config_text = sub_ele(node, "config-text") |
| 68 |
sub_ele(config_text, "configuration-text").text = config |
| 69 |
elif format == 'url': |
| 70 |
if util.url_validator(config): |
| 71 |
self._assert(':url') |
| 72 |
sub_ele(node, "url").text = config |
| 73 |
else: |
| 74 |
raise OperationError("Invalid URL.") |
| 75 |
node = self._device_handler.transform_edit_config(node) |
| 76 |
return self._request(node) |
| 77 |
|
| 78 |
|
| 79 |
class DeleteConfig(RPC): |
| 80 |
"`delete-config` RPC" |
| 81 |
|
| 82 |
def request(self, target): |
| 83 |
"""Delete a configuration datastore. |
| 84 |
|
| 85 |
*target* specifies the name or URL of configuration datastore to delete |
| 86 |
|
| 87 |
:seealso: :ref:`srctarget_params`""" |
| 88 |
node = new_ele("delete-config") |
| 89 |
node.append(util.datastore_or_url("target", target, self._assert)) |
| 90 |
return self._request(node) |
| 91 |
|
| 92 |
|
| 93 |
class CopyConfig(RPC): |
| 94 |
"`copy-config` RPC" |
| 95 |
|
| 96 |
def request(self, source, target): |
| 97 |
"""Create or replace an entire configuration datastore with the contents of another complete |
| 98 |
configuration datastore. |
| 99 |
|
| 100 |
*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 |
| 101 |
|
| 102 |
*target* is the name of the configuration datastore to use as the destination of the copy operation |
| 103 |
|
| 104 |
:seealso: :ref:`srctarget_params`""" |
| 105 |
node = new_ele("copy-config") |
| 106 |
node.append(util.datastore_or_url("target", target, self._assert)) |
| 107 |
|
| 108 |
try: |
| 109 |
# datastore name or URL |
| 110 |
node.append(util.datastore_or_url("source", source, self._assert)) |
| 111 |
except Exception: |
| 112 |
# `source` with `config` element containing the configuration subtree to copy |
| 113 |
node.append(validated_element(source, ("source", qualify("source")))) |
| 114 |
|
| 115 |
return self._request(node) |
| 116 |
|
| 117 |
|
| 118 |
class Validate(RPC): |
| 119 |
"`validate` RPC. Depends on the `:validate` capability." |
| 120 |
|
| 121 |
DEPENDS = [':validate'] |
| 122 |
|
| 123 |
def request(self, source="candidate"): |
| 124 |
"""Validate the contents of the specified configuration. |
| 125 |
|
| 126 |
*source* is the name of the configuration datastore being validated or `config` element containing the configuration subtree to be validated |
| 127 |
|
| 128 |
:seealso: :ref:`srctarget_params`""" |
| 129 |
node = new_ele("validate") |
| 130 |
if type(source) is str: |
| 131 |
src = util.datastore_or_url("source", source, self._assert) |
| 132 |
else: |
| 133 |
validated_element(source, ("config", qualify("config"))) |
| 134 |
src = new_ele("source") |
| 135 |
src.append(source) |
| 136 |
node.append(src) |
| 137 |
return self._request(node) |
| 138 |
|
| 139 |
|
| 140 |
class Commit(RPC): |
| 141 |
"`commit` RPC. Depends on the `:candidate` capability, and the `:confirmed-commit`." |
| 142 |
|
| 143 |
DEPENDS = [':candidate'] |
| 144 |
|
| 145 |
def request(self, confirmed=False, timeout=None, persist=None, persist_id=None): |
| 146 |
"""Commit the candidate configuration as the device's new current configuration. Depends on the `:candidate` capability. |
| 147 |
|
| 148 |
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. |
| 149 |
|
| 150 |
*confirmed* whether this is a confirmed commit |
| 151 |
|
| 152 |
*timeout* specifies the confirm timeout in seconds |
| 153 |
|
| 154 |
*persist* make the confirmed commit survive a session termination, and set a token on the ongoing confirmed commit |
| 155 |
|
| 156 |
*persist_id* value must be equal to the value given in the <persist> parameter to the original <commit> operation. |
| 157 |
""" |
| 158 |
node = new_ele("commit") |
| 159 |
if persist and persist_id: |
| 160 |
raise OperationError("Invalid operation as persist cannot be present with persist-id") |
| 161 |
if confirmed: |
| 162 |
self._assert(":confirmed-commit") |
| 163 |
sub_ele(node, "confirmed") |
| 164 |
if timeout is not None: |
| 165 |
sub_ele(node, "confirm-timeout").text = timeout |
| 166 |
if persist is not None: |
| 167 |
sub_ele(node, "persist").text = persist |
| 168 |
if persist_id: |
| 169 |
sub_ele(node, "persist-id").text = persist_id |
| 170 |
|
| 171 |
return self._request(node) |
| 172 |
|
| 173 |
|
| 174 |
class CancelCommit(RPC): |
| 175 |
"`cancel-commit` RPC. Depends on the `:candidate` and `:confirmed-commit` capabilities." |
| 176 |
|
| 177 |
DEPENDS = [':candidate', ':confirmed-commit'] |
| 178 |
|
| 179 |
def request(self, persist_id=None): |
| 180 |
"""Cancel an ongoing confirmed commit. Depends on the `:candidate` and `:confirmed-commit` capabilities. |
| 181 |
|
| 182 |
*persist-id* value must be equal to the value given in the <persist> parameter to the previous <commit> operation. |
| 183 |
""" |
| 184 |
node = new_ele("cancel-commit") |
| 185 |
if persist_id is not None: |
| 186 |
sub_ele(node, "persist-id").text = persist_id |
| 187 |
|
| 188 |
return self._request(node) |
| 189 |
|
| 190 |
|
| 191 |
class DiscardChanges(RPC): |
| 192 |
"`discard-changes` RPC. Depends on the `:candidate` capability." |
| 193 |
|
| 194 |
DEPENDS = [":candidate"] |
| 195 |
|
| 196 |
def request(self): |
| 197 |
"""Revert the candidate configuration to the currently running configuration. Any uncommitted changes are discarded.""" |
| 198 |
|
| 199 |
return self._request(new_ele("discard-changes")) |