Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / edit.py @ 6e571704

History | View | Annotate | Download (5.6 kB)

1 11d9e642 Shikhar Bhushan
# Copyright 2009 Shikhar Bhushan
2 11d9e642 Shikhar Bhushan
#
3 11d9e642 Shikhar Bhushan
# Licensed under the Apache License, Version 2.0 (the "License");
4 11d9e642 Shikhar Bhushan
# you may not use this file except in compliance with the License.
5 11d9e642 Shikhar Bhushan
# You may obtain a copy of the License at
6 11d9e642 Shikhar Bhushan
#
7 11d9e642 Shikhar Bhushan
#    http://www.apache.org/licenses/LICENSE-2.0
8 11d9e642 Shikhar Bhushan
#
9 11d9e642 Shikhar Bhushan
# Unless required by applicable law or agreed to in writing, software
10 11d9e642 Shikhar Bhushan
# distributed under the License is distributed on an "AS IS" BASIS,
11 11d9e642 Shikhar Bhushan
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 11d9e642 Shikhar Bhushan
# See the License for the specific language governing permissions and
13 11d9e642 Shikhar Bhushan
# limitations under the License.
14 11d9e642 Shikhar Bhushan
15 9667bcb2 Shikhar Bhushan
from ncclient.xml_ import *
16 11d9e642 Shikhar Bhushan
17 cc9af1c3 Shikhar Bhushan
from rpc import RPC
18 cc9af1c3 Shikhar Bhushan
19 d6688264 Shikhar Bhushan
import util
20 1fca349b Shikhar Bhushan
21 0b7d3b31 Shikhar Bhushan
import logging
22 19e7c7f6 Shikhar Bhushan
23 9667bcb2 Shikhar Bhushan
logger = logging.getLogger("ncclient.operations.edit")
24 0b7d3b31 Shikhar Bhushan
25 216bb34c Shikhar Bhushan
"Operations related to changing device configuration"
26 c2a5b930 Shikhar Bhushan
27 11d9e642 Shikhar Bhushan
class EditConfig(RPC):
28 19e7c7f6 Shikhar Bhushan
    "`edit-config` RPC"
29 a7cb58ce Shikhar Bhushan
30 b2d60e49 Shikhar Bhushan
    def request(self, target, config, default_operation=None, test_option=None, error_option=None):
31 19e7c7f6 Shikhar Bhushan
        """Loads all or part of the specified *config* to the *target* configuration datastore.
32 19e7c7f6 Shikhar Bhushan

33 19e7c7f6 Shikhar Bhushan
        *target* is the name of the configuration datastore being edited
34 19e7c7f6 Shikhar Bhushan

35 19e7c7f6 Shikhar Bhushan
        *config* is the configuration, which must be rooted in the `config` element. It can be specified either as a string or an :class:`~xml.etree.ElementTree.Element`.
36 19e7c7f6 Shikhar Bhushan

37 19e7c7f6 Shikhar Bhushan
        *default_operation* if specified must be one of { `"merge"`, `"replace"`, or `"none"` }
38 19e7c7f6 Shikhar Bhushan

39 19e7c7f6 Shikhar Bhushan
        *test_option* if specified must be one of { `"test_then_set"`, `"set"` }
40 19e7c7f6 Shikhar Bhushan

41 19e7c7f6 Shikhar Bhushan
        *error_option* if specified must be one of { `"stop-on-error"`, `"continue-on-error"`, `"rollback-on-error"` }
42 19e7c7f6 Shikhar Bhushan

43 19e7c7f6 Shikhar Bhushan
        The `"rollback-on-error"` *error_option* depends on the `:rollback-on-error` capability.
44 19e7c7f6 Shikhar Bhushan
        """
45 9667bcb2 Shikhar Bhushan
        node = new_ele("edit-config")
46 9667bcb2 Shikhar Bhushan
        node.append(util.datastore_or_url("target", target, self._assert))
47 2abf9522 Shikhar Bhushan
        if error_option is not None:
48 9667bcb2 Shikhar Bhushan
            if error_option == "rollback-on-error":
49 9667bcb2 Shikhar Bhushan
                self._assert(":rollback-on-error")
50 9667bcb2 Shikhar Bhushan
            sub_ele(node, "error-option").text = error_option
51 2f8bc438 Shikhar Bhushan
        if test_option is not None:
52 c2a5b930 Shikhar Bhushan
            self._assert(':validate')
53 9667bcb2 Shikhar Bhushan
            sub_ele(node, "test-option").text = test_option
54 2abf9522 Shikhar Bhushan
        if default_operation is not None:
55 19e7c7f6 Shikhar Bhushan
        # TODO: check if it is a valid default-operation
56 9667bcb2 Shikhar Bhushan
            sub_ele(node, "default-operation").text = default_operation
57 9667bcb2 Shikhar Bhushan
        node.append(validated_element(config, ("config", qualify("config"))))
58 e28708d2 Shikhar Bhushan
        return self._request(node)
59 179b00d4 Shikhar Bhushan
60 9667bcb2 Shikhar Bhushan
61 2f8bc438 Shikhar Bhushan
class DeleteConfig(RPC):
62 19e7c7f6 Shikhar Bhushan
    "`delete-config` RPC"
63 216bb34c Shikhar Bhushan
64 d771dffc Shikhar Bhushan
    def request(self, target):
65 19e7c7f6 Shikhar Bhushan
        """Delete a configuration datastore.
66 19e7c7f6 Shikhar Bhushan

67 d215b592 Shikhar Bhushan
        *target* specifies the  name or URL of configuration datastore to delete
68 d215b592 Shikhar Bhushan

69 d215b592 Shikhar Bhushan
        :seealso: :ref:`srctarget_params`"""
70 9667bcb2 Shikhar Bhushan
        node = new_ele("delete-config")
71 9667bcb2 Shikhar Bhushan
        node.append(util.datastore_or_url("target", target, self._assert))
72 e28708d2 Shikhar Bhushan
        return self._request(node)
73 d6688264 Shikhar Bhushan
74 1fca349b Shikhar Bhushan
75 2f8bc438 Shikhar Bhushan
class CopyConfig(RPC):
76 19e7c7f6 Shikhar Bhushan
    "`copy-config` RPC"
77 a7cb58ce Shikhar Bhushan
78 d771dffc Shikhar Bhushan
    def request(self, source, target):
79 19e7c7f6 Shikhar Bhushan
        """Create or replace an entire configuration datastore with the contents of another complete
80 19e7c7f6 Shikhar Bhushan
        configuration datastore.
81 19e7c7f6 Shikhar Bhushan

82 d215b592 Shikhar Bhushan
        *source* is the name of the configuration datastore to use as the source of the copy operation or `config` element containing the configuration subtree to copy
83 d215b592 Shikhar Bhushan

84 d215b592 Shikhar Bhushan
        *target* is the name of the configuration datastore to use as the destination of the copy operation
85 19e7c7f6 Shikhar Bhushan

86 d215b592 Shikhar Bhushan
        :seealso: :ref:`srctarget_params`"""
87 9667bcb2 Shikhar Bhushan
        node = new_ele("copy-config")
88 9667bcb2 Shikhar Bhushan
        node.append(util.datastore_or_url("target", target, self._assert))
89 9667bcb2 Shikhar Bhushan
        node.append(util.datastore_or_url("source", source, self._assert))
90 e28708d2 Shikhar Bhushan
        return self._request(node)
91 11d9e642 Shikhar Bhushan
92 d6688264 Shikhar Bhushan
93 2f8bc438 Shikhar Bhushan
class Validate(RPC):
94 19e7c7f6 Shikhar Bhushan
    "`validate` RPC. Depends on the `:validate` capability."
95 216bb34c Shikhar Bhushan
96 d6688264 Shikhar Bhushan
    DEPENDS = [':validate']
97 a7cb58ce Shikhar Bhushan
98 a7cb58ce Shikhar Bhushan
    def request(self, source):
99 19e7c7f6 Shikhar Bhushan
        """Validate the contents of the specified configuration.
100 19e7c7f6 Shikhar Bhushan

101 d215b592 Shikhar Bhushan
        *source* is the name of the configuration datastore being validated or `config` element containing the configuration subtree to be validated
102 d215b592 Shikhar Bhushan

103 d215b592 Shikhar Bhushan
        :seealso: :ref:`srctarget_params`"""
104 9667bcb2 Shikhar Bhushan
        node = new_ele("validate")
105 a7cb58ce Shikhar Bhushan
        try:
106 9667bcb2 Shikhar Bhushan
            src = validated_element(source, ("config", qualify("config")))
107 0b7d3b31 Shikhar Bhushan
        except Exception as e:
108 0b7d3b31 Shikhar Bhushan
            logger.debug(e)
109 9667bcb2 Shikhar Bhushan
            src = util.datastore_or_url("source", source, self._assert)
110 9667bcb2 Shikhar Bhushan
        (node if src.tag == "source" else sub_ele(node, "source")).append(src)
111 e28708d2 Shikhar Bhushan
        return self._request(node)
112 1fca349b Shikhar Bhushan
113 2f8bc438 Shikhar Bhushan
114 2f8bc438 Shikhar Bhushan
class Commit(RPC):
115 19e7c7f6 Shikhar Bhushan
    "`commit` RPC. Depends on the `:candidate` capability, and the `:confirmed-commit`."
116 216bb34c Shikhar Bhushan
117 d6688264 Shikhar Bhushan
    DEPENDS = [':candidate']
118 19e7c7f6 Shikhar Bhushan
119 216bb34c Shikhar Bhushan
    def request(self, confirmed=False, timeout=None):
120 19e7c7f6 Shikhar Bhushan
        """Commit the candidate configuration as the device's new current configuration. Depends on the `:candidate` capability.
121 19e7c7f6 Shikhar Bhushan

122 19e7c7f6 Shikhar Bhushan
        A confirmed commit (i.e. if *confirmed* is `True`) is reverted if there is no followup commit within the *timeout* interval. If no timeout is specified the confirm timeout defaults to 600 seconds (10 minutes). A confirming commit may have the *confirmed* parameter but this is not required. Depends on the `:confirmed-commit` capability.
123 19e7c7f6 Shikhar Bhushan

124 d215b592 Shikhar Bhushan
        *confirmed* whether this is a confirmed commit
125 19e7c7f6 Shikhar Bhushan

126 c15671aa Shikhar Bhushan
        *timeout* specifies the confirm timeout in seconds"""
127 9667bcb2 Shikhar Bhushan
        node = new_ele("commit")
128 d6688264 Shikhar Bhushan
        if confirmed:
129 9667bcb2 Shikhar Bhushan
            self._assert(":confirmed-commit")
130 9667bcb2 Shikhar Bhushan
            sub_ele(node, "confirmed")
131 d6688264 Shikhar Bhushan
            if timeout is not None:
132 9667bcb2 Shikhar Bhushan
                sub_ele(node, "confirm-timeout").text = timeout
133 e28708d2 Shikhar Bhushan
        return self._request(node)
134 94803aaf Shikhar Bhushan
135 1fca349b Shikhar Bhushan
136 d771dffc Shikhar Bhushan
class DiscardChanges(RPC):
137 19e7c7f6 Shikhar Bhushan
    "`discard-changes` RPC. Depends on the `:candidate` capability."
138 216bb34c Shikhar Bhushan
139 9667bcb2 Shikhar Bhushan
    DEPENDS = [":candidate"]
140 0b7d3b31 Shikhar Bhushan
141 0b7d3b31 Shikhar Bhushan
    def request(self):
142 19e7c7f6 Shikhar Bhushan
        """Revert the candidate configuration to the currently running configuration. Any uncommitted changes are discarded."""
143 9667bcb2 Shikhar Bhushan
        return self._request(new_ele("discard-changes"))