Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / edit.py @ cb96607b

History | View | Annotate | Download (5.6 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
import logging
22
logger = logging.getLogger('ncclient.operations.edit')
23

    
24

    
25

    
26
"Operations related to changing device configuration"
27

    
28
class EditConfig(RPC):
29

    
30
    "*<edit-config>* RPC"
31

    
32
    SPEC = {'tag': 'edit-config', 'subtree': []}
33

    
34
    def request(self, target, config, default_operation=None, test_option=None,
35
                error_option=None):
36
        """
37
        :arg target: see :ref:`source_target`
38
        :type target: string
39

40
        :arg config: a config element in :ref:`dtree`
41
        :type config: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
42

43
        :arg default_operation: optional; one of {'merge', 'replace', 'none'}
44
        :type default_operation: `string`
45

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

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

    
75
class DeleteConfig(RPC):
76

    
77
    "*<delete-config>* RPC"
78

    
79
    SPEC = {'tag': 'delete-config', 'subtree': []}
80

    
81
    def request(self, target):
82
        """
83
        :arg target: See :ref:`source_target`
84
        :type target: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
85

86
        :seealso: :ref:`return`
87
        """
88
        spec = DeleteConfig.SPEC.copy()
89
        spec['subtree'].append(util.store_or_url('target', target, self._assert))
90
        return self._request(spec)
91

    
92

    
93
class CopyConfig(RPC):
94

    
95
    # TESTED
96

    
97
    "*<copy-config>* RPC"
98

    
99
    SPEC = {'tag': 'copy-config', 'subtree': []}
100

    
101
    def request(self, source, target):
102
        """
103
        :arg source: See :ref:`source_target`
104
        :type source: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
105

106
        :arg target: See :ref:`source_target`
107
        :type target: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
108

109
        :seealso: :ref:`return`
110
        """
111
        spec = CopyConfig.SPEC.copy()
112
        spec['subtree'].append(util.store_or_url('target', target, self._assert))
113
        spec['subtree'].append(util.store_or_url('source', source, self._assert))
114
        return self._request(spec)
115

    
116

    
117
class Validate(RPC):
118

    
119
    # TESTED
120

    
121
    "*<validate>* RPC. Depends on the *:validate* capability."
122

    
123
    DEPENDS = [':validate']
124

    
125
    SPEC = {'tag': 'validate', 'subtree': []}
126

    
127
    def request(self, source):
128
        """
129
        :arg source: See :ref:`source_target`
130
        :type source: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
131

132
        :seealso: :ref:`return`
133
        """
134
        spec = Validate.SPEC.copy()
135
        try:
136
            src = content.validated_element(source, ('config', content.qualify('config')))
137
        except Exception as e:
138
            logger.debug(e)
139
            src = util.store_or_url('source', source, self._assert)
140
        spec['subtree'].append({
141
            'tag': 'source',
142
            'subtree': src
143
            })
144
        return self._request(spec)
145

    
146

    
147
class Commit(RPC):
148

    
149
    "*<commit>* RPC. Depends on the *:candidate* capability."
150

    
151
    DEPENDS = [':candidate']
152

    
153
    SPEC = {'tag': 'commit', 'subtree': []}
154

    
155
    def _parse_hook(self):
156
        pass
157

    
158
    def request(self, confirmed=False, timeout=None):
159
        """
160
        Requires *:confirmed-commit* capability if *confirmed* argument is
161
        :const:`True`.
162

163
        :arg confirmed: optional; request a confirmed commit
164
        :type confirmed: `bool`
165

166
        :arg timeout: specify timeout for confirmed commit
167
        :type timeout: `int`
168

169
        :seealso: :ref:`return`
170
        """
171
        spec = SPEC.copy()
172
        if confirmed:
173
            self._assert(':confirmed-commit')
174
            spec['subtree'].append({'tag': 'confirmed'})
175
            if timeout is not None:
176
                spec['subtree'].append({
177
                    'tag': 'confirm-timeout',
178
                    'text': timeout
179
                })
180
        return self._request(Commit.SPEC)
181

    
182

    
183
class DiscardChanges(RPC):
184

    
185
    "*<discard-changes>* RPC. Depends on the *:candidate* capability."
186

    
187
    DEPENDS = [':candidate']
188

    
189
    SPEC = {'tag': 'discard-changes'}
190

    
191
    def request(self):
192
        ":seealso: :ref:`return`"
193
        return self._request(DiscardChanges.SPEC)