root / ncclient / operations / edit.py @ dd8b8dd7
History | View | Annotate | Download (4.2 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 |
logger = logging.getLogger("ncclient.operations.edit")
|
23 |
|
24 |
"Operations related to changing device configuration"
|
25 |
|
26 |
class EditConfig(RPC): |
27 |
|
28 |
"*<edit-config>* RPC"
|
29 |
|
30 |
def request(self, target, config, default_operation=None, test_option=None, |
31 |
error_option=None):
|
32 |
#:arg default_operation: optional; one of {'merge', 'replace', 'none'}
|
33 |
#:type default_operation: `string`
|
34 |
#
|
35 |
#:arg error_option: optional; one of {'stop-on-error', 'continue-on-error', 'rollback-on-error'}. Last option depends on the *:rollback-on-error* capability
|
36 |
#:type error_option: string
|
37 |
#
|
38 |
#:arg test_option: optional; one of {'test-then-set', 'set'}. Depends on *:validate* capability.
|
39 |
#:type test_option: string
|
40 |
node = new_ele("edit-config")
|
41 |
node.append(util.datastore_or_url("target", target, self._assert)) |
42 |
if error_option is not None: |
43 |
if error_option == "rollback-on-error": |
44 |
self._assert(":rollback-on-error") |
45 |
sub_ele(node, "error-option").text = error_option
|
46 |
if test_option is not None: |
47 |
self._assert(':validate') |
48 |
sub_ele(node, "test-option").text = test_option
|
49 |
if default_operation is not None: |
50 |
# TODO: check if it is a valid default-operation
|
51 |
sub_ele(node, "default-operation").text = default_operation
|
52 |
node.append(validated_element(config, ("config", qualify("config")))) |
53 |
return self._request(spec) |
54 |
|
55 |
|
56 |
class DeleteConfig(RPC): |
57 |
|
58 |
"*<delete-config>* RPC"
|
59 |
|
60 |
def request(self, target): |
61 |
node = new_ele("delete-config")
|
62 |
node.append(util.datastore_or_url("target", target, self._assert)) |
63 |
return self._request(spec) |
64 |
|
65 |
|
66 |
class CopyConfig(RPC): |
67 |
|
68 |
"*<copy-config>* RPC"
|
69 |
|
70 |
def request(self, source, target): |
71 |
node = new_ele("copy-config")
|
72 |
node.append(util.datastore_or_url("target", target, self._assert)) |
73 |
node.append(util.datastore_or_url("source", source, self._assert)) |
74 |
return self._request(spec) |
75 |
|
76 |
|
77 |
class Validate(RPC): |
78 |
|
79 |
"*<validate>* RPC. Depends on the *:validate* capability."
|
80 |
|
81 |
DEPENDS = [':validate']
|
82 |
|
83 |
def request(self, source): |
84 |
node = new_ele("validate")
|
85 |
try:
|
86 |
src = validated_element(source, ("config", qualify("config"))) |
87 |
except Exception as e: |
88 |
logger.debug(e) |
89 |
src = util.datastore_or_url("source", source, self._assert) |
90 |
(node if src.tag == "source" else sub_ele(node, "source")).append(src) |
91 |
return self._request(spec) |
92 |
|
93 |
|
94 |
class Commit(RPC): |
95 |
|
96 |
"*<commit>* RPC. Depends on the *:candidate* capability."
|
97 |
|
98 |
DEPENDS = [':candidate']
|
99 |
|
100 |
def request(self, confirmed=False, timeout=None): |
101 |
"""
|
102 |
Requires *:confirmed-commit* capability if *confirmed* argument is
|
103 |
:const:`True`.
|
104 |
"""
|
105 |
#:arg confirmed: optional; request a confirmed commit
|
106 |
#:type confirmed: `bool`
|
107 |
#
|
108 |
#:arg timeout: specify timeout for confirmed commit
|
109 |
#:type timeout: `int`
|
110 |
node = new_ele("commit")
|
111 |
if confirmed:
|
112 |
self._assert(":confirmed-commit") |
113 |
sub_ele(node, "confirmed")
|
114 |
if timeout is not None: |
115 |
# TODO check if timeout is a valid integer?
|
116 |
sub_ele(node, "confirm-timeout").text = timeout
|
117 |
return self._request(Commit.SPEC) |
118 |
|
119 |
|
120 |
class DiscardChanges(RPC): |
121 |
|
122 |
"*<discard-changes>* RPC. Depends on the *:candidate* capability."
|
123 |
|
124 |
DEPENDS = [":candidate"]
|
125 |
|
126 |
def request(self): |
127 |
return self._request(new_ele("discard-changes")) |