may be broken...
[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.rpc import RPC
16 from ncclient.content import iselement
17
18 import util
19
20 class EditConfig(RPC):
21     
22     # tested: no
23     # combed: no
24     
25     SPEC = {
26         'tag': 'edit-config',
27         'subtree': []
28     }
29     
30     def request(self, target=None, target_url=None, config=None,
31                 default_operation=None, test_option=None, error_option=None):
32         util.one_of(target, target_url)
33         spec = EditConfig.SPEC.copy()
34         params = spec['subtree']
35         params.append({
36             'tag': 'target',
37             'subtree': util.store_or_url(target, target_url)
38             })
39         params.append({
40             'tag': 'config',
41             'subtree': config
42             })
43         if default_operation is not None:
44             params.append({
45                 'tag': 'default-operation',
46                 'text': default_operation
47                 })
48         if test_option is not None:
49             if test_option == '':
50                 self._assert(':validate')
51             params.append({
52                 'tag': 'test-option',
53                 'text': test_option
54                 })
55         if error_option is not None:
56             if error_option == 'rollback-on-error':
57                 self._assert(':rollback-on-error')
58             params.append({
59                 'tag': 'error-option',
60                 'text': error_option
61                 })
62
63
64 class DeleteConfig(RPC):
65     
66     # tested: no
67     
68     SPEC = {
69         'tag': 'delete-config',
70         'subtree': [ { 'tag': 'target', 'subtree': None } ]
71     }
72     
73     def request(self, target=None, target_url=None):
74         spec = DeleteConfig.SPEC.copy()
75         spec['subtree'][0]['subtree'] = util.store_or_url(target, target_url)
76         return self._request(spec)
77
78
79 class CopyConfig(RPC):
80     
81     # tested: no
82     
83     SPEC = {
84         'tag': 'copy-config',
85         'subtree': []
86     }
87     
88     def request(self, source=None, source_url=None, target=None, target_url=None):
89         spec = CopyConfig.SPEC.copy()
90         spec['subtree'].append({
91             'tag': 'target',
92             'subtree': util.store_or_url(source, source_url)
93             })
94         spec['subtree'].append({
95             'tag': 'target',
96             'subtree': util.store_or_url(target, target_url)
97             })
98         return self._request(spec)
99
100
101 class Validate(RPC):
102     
103     # tested: no
104     # combed: yes
105     
106     'config attr shd not include <config> root'
107     
108     DEPENDS = [':validate']
109     
110     SPEC = {
111         'tag': 'validate',
112         'subtree': []
113     }
114     
115     def request(self, source=None, config=None):
116         util.one_of(source, capability)
117         spec = SPEC.copy()
118         if source is not None:
119             spec['subtree'].append({
120                 'tag': 'source',
121                 'subtree': {'tag': source}
122             })
123         else:
124             spec['subtree'].append({
125                 'tag': 'config',
126                 'subtree': config
127             })
128         return self._request(spec)
129
130
131 class Commit(RPC):
132     
133     # tested: no
134     # combed: yes
135     
136     DEPENDS = [':candidate']
137     
138     SPEC = { 'tag': 'commit', 'subtree': [] }
139     
140     def _parse_hook(self):
141         pass
142     
143     def request(self, confirmed=False, timeout=None):
144         spec = SPEC.copy()
145         if confirmed:
146             spec['subtree'].append({'tag': 'confirmed'})
147             if timeout is not None:
148                 spec['subtree'].append({
149                     'tag': 'confirm-timeout',
150                     'text': timeout
151                 })
152         return self._request(Commit.SPEC)
153
154
155 class ConfirmedCommit(Commit):
156     "psuedo-op"
157     
158     # tested: no
159     # combed: yes
160     
161     DEPENDS = [':candidate', ':confirmed-commit']
162     
163     def request(self, timeout=None):
164         "Commit changes; requireing that a confirming commit follow"
165         return Commit.request(self, confirmed=True, timeout=timeout)
166     
167     def confirm(self):
168         "Make the confirming commit"
169         return Commit.request(self, confirmed=True)
170
171
172 class DiscardChanges(RPC):
173     
174     # tested: no
175     # combed: yes
176     
177     DEPENDS = [':candidate']
178     
179     SPEC = {'tag': 'discard-changes'}