Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / edit.py @ 0c55eea9

History | View | Annotate | Download (3.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.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
        node = new_ele("edit-config")
33
        node.append(util.datastore_or_url("target", target, self._assert))
34
        if error_option is not None:
35
            if error_option == "rollback-on-error":
36
                self._assert(":rollback-on-error")
37
            sub_ele(node, "error-option").text = error_option
38
        if test_option is not None:
39
            self._assert(':validate')
40
            sub_ele(node, "test-option").text = test_option
41
        if default_operation is not None:
42
            # TODO: check if it is a valid default-operation
43
            sub_ele(node, "default-operation").text = default_operation
44
        node.append(validated_element(config, ("config", qualify("config"))))
45
        return self._request(node)
46

    
47

    
48
class DeleteConfig(RPC):
49

    
50
    "*<delete-config>* RPC"
51

    
52
    def request(self, target):
53
        node = new_ele("delete-config")
54
        node.append(util.datastore_or_url("target", target, self._assert))
55
        return self._request(node)
56

    
57

    
58
class CopyConfig(RPC):
59

    
60
    "*<copy-config>* RPC"
61
    
62
    def request(self, source, target):
63
        node = new_ele("copy-config")
64
        node.append(util.datastore_or_url("target", target, self._assert))
65
        node.append(util.datastore_or_url("source", source, self._assert))
66
        return self._request(node)
67

    
68

    
69
class Validate(RPC):
70

    
71
    "*<validate>* RPC. Depends on the *:validate* capability."
72

    
73
    DEPENDS = [':validate']
74

    
75
    def request(self, source):
76
        node = new_ele("validate")
77
        try:
78
            src = validated_element(source, ("config", qualify("config")))
79
        except Exception as e:
80
            logger.debug(e)
81
            src = util.datastore_or_url("source", source, self._assert)
82
        (node if src.tag == "source" else sub_ele(node, "source")).append(src)
83
        return self._request(node)
84

    
85

    
86
class Commit(RPC):
87

    
88
    "*<commit>* RPC. Depends on the *:candidate* capability, and the *:confirmed-commit* "
89

    
90
    DEPENDS = [':candidate']
91
    
92
    def request(self, confirmed=False, timeout=None):
93
        node = new_ele("commit")
94
        if confirmed:
95
            self._assert(":confirmed-commit")
96
            sub_ele(node, "confirmed")
97
            if timeout is not None:
98
                # TODO check if timeout is a valid integer?
99
                sub_ele(node, "confirm-timeout").text = timeout
100
        return self._request(node)
101

    
102

    
103
class DiscardChanges(RPC):
104

    
105
    "*<discard-changes>* RPC. Depends on the *:candidate* capability."
106

    
107
    DEPENDS = [":candidate"]
108

    
109
    def request(self):
110
        return self._request(new_ele("discard-changes"))