root / ncclient / operations / retrieve.py @ 53e1a046
History | View | Annotate | Download (3.2 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 rpc import RPC, RPCReply |
16 |
|
17 |
from ncclient.xml_ import * |
18 |
|
19 |
import util |
20 |
|
21 |
class GetReply(RPCReply): |
22 |
|
23 |
"""Adds attributes for the *data* element to `RPCReply`."""
|
24 |
|
25 |
def _parsing_hook(self, root): |
26 |
self._data = None |
27 |
if not self._errors: |
28 |
self._data = root.find(qualify("data")) |
29 |
|
30 |
@property
|
31 |
def data_ele(self): |
32 |
"*data* element as an :class:`~xml.etree.ElementTree.Element`"
|
33 |
if not self._parsed: |
34 |
self.parse()
|
35 |
return self._data |
36 |
|
37 |
@property
|
38 |
def data_xml(self): |
39 |
"*data* element as an XML string"
|
40 |
if not self._parsed: |
41 |
self.parse()
|
42 |
return to_xml(self._data) |
43 |
|
44 |
data = data_ele |
45 |
"Same as :attr:`data_ele`"
|
46 |
|
47 |
|
48 |
class Get(RPC): |
49 |
|
50 |
"The *get* RPC."
|
51 |
|
52 |
REPLY_CLS = GetReply |
53 |
"See :class:`GetReply`."
|
54 |
|
55 |
def request(self, filter=None): |
56 |
"""Retrieve running configuration and device state information.
|
57 |
|
58 |
*filter* specifies the portion of the configuration to retrieve (by default entire configuration is retrieved)
|
59 |
|
60 |
:seealso: :ref:`filter_params`
|
61 |
"""
|
62 |
node = new_ele("get")
|
63 |
if filter is not None: |
64 |
node.append(util.build_filter(filter))
|
65 |
return self._request(node) |
66 |
|
67 |
|
68 |
class GetConfig(RPC): |
69 |
|
70 |
"The *get-config* RPC."
|
71 |
|
72 |
REPLY_CLS = GetReply |
73 |
"See :class:`GetReply`."
|
74 |
|
75 |
def request(self, source, filter=None): |
76 |
"""Retrieve all or part of a specified configuration.
|
77 |
|
78 |
*source* name of the configuration datastore being queried
|
79 |
|
80 |
*filter* specifies the portion of the configuration to retrieve (by default entire configuration is retrieved)
|
81 |
|
82 |
:seealso: :ref:`filter_params`"""
|
83 |
node = new_ele("get-config")
|
84 |
node.append(util.datastore_or_url("source", source, self._assert)) |
85 |
if filter is not None: |
86 |
node.append(util.build_filter(filter))
|
87 |
return self._request(node) |
88 |
|
89 |
class Dispatch(RPC): |
90 |
"""Generic retrieving wrapper. Eg. dispatch('clear-arp-table') or dispatch element
|
91 |
like :
|
92 |
xsd_fetch = new_ele('get-xnm-information')
|
93 |
sub_ele(xsd_fetch, 'type').text="xml-schema"
|
94 |
sub_ele(xsd_fetch, 'namespace').text="junos-configuration
|
95 |
dispatch(xsd_fetch)
|
96 |
to fetch entire xsd sxhema file from Juniper
|
97 |
"""
|
98 |
|
99 |
REPLY_CLS = GetReply |
100 |
|
101 |
def request(self, rpc_command, source=None, filter=None): |
102 |
if ET.iselement(rpc_command):
|
103 |
node = rpc_command |
104 |
else:
|
105 |
node = new_ele(rpc_command) |
106 |
if source is not None: |
107 |
node.append(util.datastore_or_url("source", source, self._assert)) |
108 |
if filter is not None: |
109 |
node.append(util.build_filter(filter))
|
110 |
return self._request(node) |