looking at this code and trying things out after a long time. lots of cleanups and...
[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 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, error_option=None):
31         node = new_ele("edit-config")
32         node.append(util.datastore_or_url("target", target, self._assert))
33         if error_option is not None:
34             if error_option == "rollback-on-error":
35                 self._assert(":rollback-on-error")
36             sub_ele(node, "error-option").text = error_option
37         if test_option is not None:
38             self._assert(':validate')
39             sub_ele(node, "test-option").text = test_option
40         if default_operation is not None:
41             # TODO: check if it is a valid default-operation
42             sub_ele(node, "default-operation").text = default_operation
43         node.append(validated_element(config, ("config", qualify("config"))))
44         return self._request(node)
45
46
47 class DeleteConfig(RPC):
48
49     "*<delete-config>* RPC"
50
51     def request(self, target):
52         node = new_ele("delete-config")
53         node.append(util.datastore_or_url("target", target, self._assert))
54         return self._request(node)
55
56
57 class CopyConfig(RPC):
58
59     "*<copy-config>* RPC"
60     
61     def request(self, source, target):
62         node = new_ele("copy-config")
63         node.append(util.datastore_or_url("target", target, self._assert))
64         node.append(util.datastore_or_url("source", source, self._assert))
65         return self._request(node)
66
67
68 class Validate(RPC):
69
70     "*<validate>* RPC. Depends on the *:validate* capability."
71
72     DEPENDS = [':validate']
73
74     def request(self, source):
75         node = new_ele("validate")
76         try:
77             src = validated_element(source, ("config", qualify("config")))
78         except Exception as e:
79             logger.debug(e)
80             src = util.datastore_or_url("source", source, self._assert)
81         (node if src.tag == "source" else sub_ele(node, "source")).append(src)
82         return self._request(node)
83
84
85 class Commit(RPC):
86
87     "*<commit>* RPC. Depends on the *:candidate* capability, and the *:confirmed-commit* "
88
89     DEPENDS = [':candidate']
90     
91     def request(self, confirmed=False, timeout=None):
92         node = new_ele("commit")
93         if confirmed:
94             self._assert(":confirmed-commit")
95             sub_ele(node, "confirmed")
96             if timeout is not None:
97                 sub_ele(node, "confirm-timeout").text = timeout
98         return self._request(node)
99
100
101 class DiscardChanges(RPC):
102
103     "*<discard-changes>* RPC. Depends on the *:candidate* capability."
104
105     DEPENDS = [":candidate"]
106
107     def request(self):
108         return self._request(new_ele("discard-changes"))