root / ncclient / operations / retrieve.py @ ad82bf43
History | View | Annotate | Download (3.6 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 |
|
91 |
"Generic retrieving wrapper"
|
92 |
|
93 |
REPLY_CLS = GetReply |
94 |
"See :class:`GetReply`."
|
95 |
|
96 |
def request(self, rpc_command, source=None, filter=None): |
97 |
"""
|
98 |
*rpc_command* specifies rpc command to be dispatched either in plain text or in xml element format (depending on command)
|
99 |
|
100 |
*source* name of the configuration datastore being queried
|
101 |
|
102 |
*filter* specifies the portion of the configuration to retrieve (by default entire configuration is retrieved)
|
103 |
|
104 |
:seealso: :ref:`filter_params`
|
105 |
|
106 |
Examples of usage::
|
107 |
|
108 |
dispatch('clear-arp-table')
|
109 |
|
110 |
or dispatch element like ::
|
111 |
|
112 |
xsd_fetch = new_ele('get-xnm-information')
|
113 |
sub_ele(xsd_fetch, 'type').text="xml-schema"
|
114 |
sub_ele(xsd_fetch, 'namespace').text="junos-configuration"
|
115 |
dispatch(xsd_fetch)
|
116 |
"""
|
117 |
|
118 |
if ET.iselement(rpc_command):
|
119 |
node = rpc_command |
120 |
else:
|
121 |
node = new_ele(rpc_command) |
122 |
if source is not None: |
123 |
node.append(util.datastore_or_url("source", source, self._assert)) |
124 |
if filter is not None: |
125 |
node.append(util.build_filter(filter))
|
126 |
return self._request(node) |
127 |
|