Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / edit.py @ 9667bcb2

History | View | Annotate | Download (5.1 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.xml_ import *
16

    
17
from rpc import RPC
18

    
19
import util
20

    
21
import logging
22
logger = logging.getLogger("ncclient.operations.edit")
23

    
24
"Operations related to changing device configuration"
25

    
26
class EditConfig(RPC):
27

    
28
    "*<edit-config>* RPC"
29
    
30
    def request(self, target, config, default_operation=None, test_option=None,
31
                error_option=None):
32
        """
33
        :arg target: see :ref:`source_target`
34
        :type target: string
35

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

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

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

45
        :arg test_option: optional; one of {'test-then-set', 'set'}. Depends on *:validate* capability.
46
        :type test_option: string
47

48
        :seealso: :ref:`return`
49
        """
50
        node = new_ele("edit-config")
51
        node.append(util.datastore_or_url("target", target, self._assert))
52
        if error_option is not None:
53
            if error_option == "rollback-on-error":
54
                self._assert(":rollback-on-error")
55
            sub_ele(node, "error-option").text = error_option
56
        if test_option is not None:
57
            self._assert(':validate')
58
            sub_ele(node, "test-option").text = test_option
59
        if default_operation is not None:
60
            # TODO: check if it is a valid default-operation
61
            sub_ele(node, "default-operation").text = default_operation
62
        node.append(validated_element(config, ("config", qualify("config"))))
63
        return self._request(spec)
64

    
65

    
66
class DeleteConfig(RPC):
67

    
68
    "*<delete-config>* RPC"
69

    
70
    def request(self, target):
71
        """
72
        :arg target: See :ref:`source_target`
73
        :type target: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
74

75
        :seealso: :ref:`return`
76
        """
77
        node = new_ele("delete-config")
78
        node.append(util.datastore_or_url("target", target, self._assert))
79
        return self._request(spec)
80

    
81

    
82
class CopyConfig(RPC):
83

    
84
    "*<copy-config>* RPC"
85
    
86
    def request(self, source, target):
87
        """
88
        :arg source: See :ref:`source_target`
89
        :type source: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
90

91
        :arg target: See :ref:`source_target`
92
        :type target: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
93

94
        :seealso: :ref:`return`
95
        """
96
        node = new_ele("copy-config")
97
        node.append(util.datastore_or_url("target", target, self._assert))
98
        node.append(util.datastore_or_url("source", source, self._assert))
99
        return self._request(spec)
100

    
101

    
102
class Validate(RPC):
103

    
104
    "*<validate>* RPC. Depends on the *:validate* capability."
105

    
106
    DEPENDS = [':validate']
107

    
108
    def request(self, source):
109
        """
110
        :arg source: See :ref:`source_target`
111
        :type source: `string` or `dict` or :class:`~xml.etree.ElementTree.Element`
112

113
        :seealso: :ref:`return`
114
        """
115
        node = new_ele("validate")
116
        try:
117
            src = validated_element(source, ("config", qualify("config")))
118
        except Exception as e:
119
            logger.debug(e)
120
            src = util.datastore_or_url("source", source, self._assert)
121
        (node if src.tag == "source" else sub_ele(node, "source")).append(src)
122
        return self._request(spec)
123

    
124

    
125
class Commit(RPC):
126

    
127
    "*<commit>* RPC. Depends on the *:candidate* capability."
128

    
129
    DEPENDS = [':candidate']
130
    
131
    def request(self, confirmed=False, timeout=None):
132
        """
133
        Requires *:confirmed-commit* capability if *confirmed* argument is
134
        :const:`True`.
135

136
        :arg confirmed: optional; request a confirmed commit
137
        :type confirmed: `bool`
138

139
        :arg timeout: specify timeout for confirmed commit
140
        :type timeout: `int`
141

142
        :seealso: :ref:`return`
143
        """
144
        node = new_ele("commit")
145
        if confirmed:
146
            self._assert(":confirmed-commit")
147
            sub_ele(node, "confirmed")
148
            if timeout is not None:
149
                # TODO check if timeout is a valid integer?
150
                sub_ele(node, "confirm-timeout").text = timeout
151
        return self._request(Commit.SPEC)
152

    
153

    
154
class DiscardChanges(RPC):
155

    
156
    "*<discard-changes>* RPC. Depends on the *:candidate* capability."
157

    
158
    DEPENDS = [":candidate"]
159

    
160
    def request(self):
161
        ":seealso: :ref:`return`"
162
        return self._request(new_ele("discard-changes"))