fixes
[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     SPEC = {
23         'tag': 'edit-config',
24         'children': [ ]
25     }
26     
27     def request(self, target=None, target_url=None, config=None,
28                 default_operation=None, test_option=None, error_option=None):
29         util.one_of(target, target_url)
30         spec = EditConfig.SPEC.copy()
31         params = spec['children']
32         params.append({'tag': 'target', 'children': util.store_or_url(target, target_url)})
33         params.append({'tag': 'config', 'children': config})
34         if default_operation is not None:
35             params.append({'tag': 'default-operation', 'text': default_operation})
36         if test_option is not None:
37             params.append({'tag': 'test-option', 'text': test_option})
38         if error_option is not None:
39             params.append({'tag': 'test-option', 'text': test_option})
40
41 class DeleteConfig(RPC):
42     
43     SPEC = {
44         'tag': 'delete-config',
45         'children': [ { 'tag': 'target', 'children': None } ]
46     }
47     
48     def request(self, target=None, target_url=None):
49         spec = DeleteConfig.SPEC.copy()
50         spec['children'][0]['children'] = util.store_or_url(target, target_url)
51         return self._request(spec)
52
53
54 class CopyConfig(RPC):
55     
56     SPEC = {
57         'tag': 'copy-config',
58         'children': [
59             { 'tag': 'source', 'children': {'tag': None } },
60             { 'tag': 'target', 'children': {'tag': None } }
61         ]
62     }
63     
64     def request(self, source=None, source_url=None, target=None, target_url=None):
65         spec = CopyConfig.SPEC.copy()
66         spec['children'][0]['children'] = util.store_or_url(source, source_url)
67         spec['children'][1]['children'] = util.store_or_url(target, target_url)
68         return self._request(spec)
69
70
71 class Validate(RPC):
72     
73     'config attr shd not include <config> root'
74     
75     DEPENDS = [':validate']
76     
77     SPEC = {
78         'tag': 'validate',
79         'children': []
80     }
81     
82     def request(self, source=None, config=None):
83         util.one_of(source, capability)
84         spec = SPEC.copy()
85         if source is not None:
86             spec['children'].append({
87                 'tag': 'source', 'children': {'tag': source}
88                 })
89         else:
90             spec['children'].append({'tag': 'config', 'children': config})
91         return self._request(spec)
92
93
94 class Commit(RPC):
95     
96     DEPENDS = [':candidate']
97     
98     SPEC = {'tag': 'commit', 'children': [] }
99     
100     def _parse_hook(self):
101         pass
102     
103     def request(self, confirmed=False, timeout=None):
104         spec = SPEC.copy()
105         if confirmed:
106             self._assert(':confirmed-commit')
107             children = spec['children']
108             children.append({'tag': 'confirmed'})
109             if timeout is not None:
110                 children.append({
111                     'tag': 'confirm-timeout',
112                     'text': timeout
113                 })
114         return self._request(Commit.SPEC)
115
116
117 class DiscardChanges(RPC):
118     
119     DEPENDS = [':candidate']
120     
121     SPEC = {'tag': 'discard-changes'}
122     
123     request = lambda self: self._request(DiscardChanges.SPEC)