Debian Codegraph @

ncclient/operations/retrieve.py

raw
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.operations.errors import OperationError
16 from ncclient.operations.rpc import RPC, RPCReply
17
18 from ncclient.xml_ import *
19 from lxml import etree
20
21 from ncclient.operations import util
22
23
24 class WithDefaultsError(OperationError):
25
26 """Invalid 'with-defaults' mode or capability URI"""
27
28
29 class GetReply(RPCReply):
30
31 """Adds attributes for the *data* element to `RPCReply`."""
32
33 def _parsing_hook(self, root):
34 self._data = None
35 if not self._errors:
36 self._data = root.find(qualify("data"))
37
38 @property
39 def data_ele(self):
40 "*data* element as an :class:`~xml.etree.ElementTree.Element`"
41 if not self._parsed:
42 self.parse()
43 return self._data
44
45 @property
46 def data_xml(self):
47 "*data* element as an XML string"
48 if not self._parsed:
49 self.parse()
50 return to_xml(self._data)
51
52 data = data_ele
53 "Same as :attr:`data_ele`"
54
55
56 class GetSchemaReply(GetReply):
57 """Reply for GetSchema called with specific parsing hook."""
58
59 def _parsing_hook(self, root):
60 self._data = None
61 if not self._errors:
62 self._data = root.find(qualify("data", NETCONF_MONITORING_NS)).text
63
64
65 class Get(RPC):
66
67 "The *get* RPC."
68
69 REPLY_CLS = GetReply
70 "See :class:`GetReply`."
71
72 def request(self, filter=None, with_defaults=None):
73 """Retrieve running configuration and device state information.
74
75 *filter* specifies the portion of the configuration to retrieve (by default entire configuration is retrieved)
76
77 *with_defaults* defines an explicit method of retrieving default values from the configuration (see :rfc:`6243`)
78
79 :seealso: :ref:`filter_params`
80 """
81 node = new_ele("get")
82 if filter is not None:
83 node.append(util.build_filter(filter))
84 if with_defaults is not None:
85 self._assert(":with-defaults")
86 _append_with_defaults_mode(
87 node,
88 with_defaults,
89 self._session.server_capabilities,
90 )
91 return self._request(node)
92
93
94 def _append_with_defaults_mode(node, mode, capabilities):
95 _validate_with_defaults_mode(mode, capabilities)
96 with_defaults_element = sub_ele_ns(
97 node,
98 "with-defaults",
99 NETCONF_WITH_DEFAULTS_NS,
100 )
101 with_defaults_element.text = mode
102
103
104 def _validate_with_defaults_mode(mode, capabilities):
105 valid_modes = _get_valid_with_defaults_modes(capabilities)
106 if mode.strip().lower() not in valid_modes:
107 raise WithDefaultsError(
108 "Invalid 'with-defaults' mode '{provided}'; the server only "
109 "supports the following: {options}".format(
110 provided=mode,
111 options=', '.join(valid_modes)
112 )
113 )
114
115
116 def _get_valid_with_defaults_modes(capabilities):
117 """Reference: https://tools.ietf.org/html/rfc6243#section-4.3"""
118 capability = capabilities[":with-defaults"]
119
120 try:
121 valid_modes = [capability.parameters["basic-mode"]]
122 except KeyError:
123 raise WithDefaultsError(
124 "Invalid 'with-defaults' capability URI advertised by the server; "
125 "missing 'basic-mode' parameter"
126 )
127
128 try:
129 also_supported = capability.parameters["also-supported"]
130 except KeyError:
131 return valid_modes
132
133 valid_modes.extend(also_supported.split(","))
134
135 return valid_modes
136
137
138 class GetConfig(RPC):
139
140 """The *get-config* RPC."""
141
142 REPLY_CLS = GetReply
143 """See :class:`GetReply`."""
144
145 def request(self, source, filter=None, with_defaults=None):
146 """Retrieve all or part of a specified configuration.
147
148 *source* name of the configuration datastore being queried
149
150 *filter* specifies the portion of the configuration to retrieve (by default entire configuration is retrieved)
151
152 *with_defaults* defines an explicit method of retrieving default values from the configuration (see :rfc:`6243`)
153
154 :seealso: :ref:`filter_params`"""
155 node = new_ele("get-config")
156 node.append(util.datastore_or_url("source", source, self._assert))
157 if filter is not None:
158 node.append(util.build_filter(filter))
159 if with_defaults is not None:
160 self._assert(":with-defaults")
161 _append_with_defaults_mode(
162 node,
163 with_defaults,
164 self._session.server_capabilities,
165 )
166 return self._request(node)
167
168 class GetSchema(RPC):
169
170 """The *get-schema* RPC."""
171
172 REPLY_CLS = GetSchemaReply
173 """See :class:`GetReply`."""
174
175 def request(self, identifier, version=None, format=None):
176 """Retrieve a named schema, with optional revision and type.
177
178 *identifier* name of the schema to be retrieved
179
180 *version* version of schema to get
181
182 *format* format of the schema to be retrieved, yang is the default
183
184 :seealso: :ref:`filter_params`"""
185 self._huge_tree = True
186 node = etree.Element(qualify("get-schema",NETCONF_MONITORING_NS))
187 if identifier is not None:
188 elem = etree.Element(qualify("identifier",NETCONF_MONITORING_NS))
189 elem.text = identifier
190 node.append(elem)
191 if version is not None:
192 elem = etree.Element(qualify("version",NETCONF_MONITORING_NS))
193 elem.text = version
194 node.append(elem)
195 if format is not None:
196 elem = etree.Element(qualify("format",NETCONF_MONITORING_NS))
197 elem.text = format
198 node.append(elem)
199 return self._request(node)
200
201 class Dispatch(RPC):
202
203 """Generic retrieving wrapper"""
204
205 REPLY_CLS = RPCReply
206 """See :class:`RPCReply`."""
207
208 def request(self, rpc_command, source=None, filter=None):
209 """
210 *rpc_command* specifies rpc command to be dispatched either in plain text or in xml element format (depending on command)
211
212 *source* name of the configuration datastore being queried
213
214 *filter* specifies the portion of the configuration to retrieve (by default entire configuration is retrieved)
215
216 :seealso: :ref:`filter_params`
217
218 Examples of usage::
219
220 dispatch('clear-arp-table')
221
222 or dispatch element like ::
223
224 xsd_fetch = new_ele('get-xnm-information')
225 sub_ele(xsd_fetch, 'type').text="xml-schema"
226 sub_ele(xsd_fetch, 'namespace').text="junos-configuration"
227 dispatch(xsd_fetch)
228 """
229
230
231 if etree.iselement(rpc_command):
232 node = rpc_command
233 else:
234 node = new_ele(rpc_command)
235 if source is not None:
236 node.append(util.datastore_or_url("source", source, self._assert))
237 if filter is not None:
238 node.append(util.build_filter(filter))
239
240 return self._request(node)