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