Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / edit.py @ 216bb34c

History | View | Annotate | Download (5.4 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 import content
16

    
17
from rpc import RPC
18

    
19
import util
20

    
21
"Operations related to changing device configuration"
22

    
23
class EditConfig(RPC):
24

    
25
    "*<edit-config>* RPC"
26

    
27
    SPEC = {'tag': 'edit-config', 'subtree': []}
28

    
29
    def request(self, target, config, default_operation=None, test_option=None,
30
                error_option=None):
31
        """
32
        :arg target: see :ref:`source_target`
33
        :type target: string
34

35
        :arg config: a config element in :ref:`dtree`
36
        :type config: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
37

38
        :arg default_operation: optional; one of {'merge', 'replace', 'none'}
39
        :type default_operation: `string`
40

41
        :arg test_option: optional; one of {'stop-on-error', 'continue-on-error', 'rollback-on-error'}. Last option depends on the *:rollback-on-error* capability
42
        :type test_option: string
43

44
        :seealso: :ref:`return`
45
        """
46
        spec = EditConfig.SPEC.copy()
47
        subtree = spec['subtree']
48
        subtree.append(util.store_or_url('target', target, self._assert))
49
        subtree.append(content.validated_element(config, ('config', content.qualify('config'))))
50
        if default_operation is not None:
51
            subtree.append({
52
                'tag': 'default-operation',
53
                'text': default_operation
54
                })
55
        if test_option is not None:
56
            self._assert(':validate')
57
            subtree.append({
58
                'tag': 'test-option',
59
                'text': test_option
60
                })
61
        if error_option is not None:
62
            if error_option == 'rollback-on-error':
63
                self._assert(':rollback-on-error')
64
            subtree.append({
65
                'tag': 'error-option',
66
                'text': error_option
67
                })
68
        return self._request(spec)
69

    
70
class DeleteConfig(RPC):
71

    
72
    "*<delete-config>* RPC"
73

    
74
    SPEC = {'tag': 'delete-config', 'subtree': []}
75

    
76
    def request(self, target):
77
        """
78
        :arg target: See :ref:`source_target`
79
        :type target: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
80

81
        :seealso: :ref:`return`
82
        """
83
        spec = DeleteConfig.SPEC.copy()
84
        spec['subtree'].append(util.store_or_url('target', target, self._assert))
85
        return self._request(spec)
86

    
87

    
88
class CopyConfig(RPC):
89

    
90
    "*<copy-config>* RPC"
91

    
92
    SPEC = {'tag': 'copy-config', 'subtree': []}
93

    
94
    def request(self, source, target):
95
        """
96
        :arg source: See :ref:`source_target`
97
        :type source: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
98

99
        :arg target: See :ref:`source_target`
100
        :type target: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
101

102
        :seealso: :ref:`return`
103
        """
104
        spec = CopyConfig.SPEC.copy()
105
        spec['subtree'].append(util.store_or_url('source', source, self._assert))
106
        spec['subtree'].append(util.store_or_url('target', target, self._assert))
107
        return self._request(spec)
108

    
109

    
110
class Validate(RPC):
111

    
112
    "*<validate>* RPC. Depends on the *:validate* capability."
113

    
114
    DEPENDS = [':validate']
115

    
116
    SPEC = {'tag': 'validate', 'subtree': []}
117

    
118
    def request(self, source):
119
        """
120
        :arg source: See :ref:`source_target`
121
        :type source: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
122

123
        :seealso: :ref:`return`
124
        """
125
        spec = Validate.SPEC.copy()
126
        try:
127
            spec['subtree'].append({
128
                'tag': 'source',
129
                'subtree':
130
                    content.validated_element(
131
                        config, ('config', content.qualify('config')))
132
                })
133
        except:
134
            spec['subtree'].append(util.store_or_url('source', source, self._assert))
135
        return self._request(spec)
136

    
137

    
138
class Commit(RPC):
139

    
140
    "*<commit>* RPC. Depends on the *:candidate* capability."
141

    
142
    DEPENDS = [':candidate']
143

    
144
    SPEC = {'tag': 'commit', 'subtree': []}
145

    
146
    def _parse_hook(self):
147
        pass
148

    
149
    def request(self, confirmed=False, timeout=None):
150
        """
151
        Requires *:confirmed-commit* capability if *confirmed* argument is
152
        :const:`True`.
153

154
        :arg confirmed: optional; request a confirmed commit
155
        :type confirmed: `bool`
156

157
        :arg timeout: specify timeout for confirmed commit
158
        :type timeout: `int`
159

160
        :seealso: :ref:`return`
161
        """
162
        spec = SPEC.copy()
163
        if confirmed:
164
            self._assert(':confirmed-commit')
165
            spec['subtree'].append({'tag': 'confirmed'})
166
            if timeout is not None:
167
                spec['subtree'].append({
168
                    'tag': 'confirm-timeout',
169
                    'text': timeout
170
                })
171
        return self._request(Commit.SPEC)
172

    
173

    
174
class DiscardChanges(RPC):
175

    
176
    "*<discard-changes>* RPC. Depends on the *:candidate* capability."
177

    
178
    DEPENDS = [':candidate']
179

    
180
    SPEC = {'tag': 'discard-changes'}