| 1 |
# by Pasi Sikala <[email protected]> |
| 2 |
|
| 3 |
from ncclient.xml_ import * |
| 4 |
|
| 5 |
from ncclient.operations.rpc import RPC |
| 6 |
from ncclient.operations import util |
| 7 |
|
| 8 |
|
| 9 |
class ShowCLI(RPC): |
| 10 |
def request(self, command=None): |
| 11 |
"""Run CLI -show commands |
| 12 |
*command* (show) command to run |
| 13 |
|
| 14 |
""" |
| 15 |
node = new_ele('get') |
| 16 |
filter = sub_ele(node, 'filter') |
| 17 |
block = sub_ele(filter, 'oper-data-format-cli-block') |
| 18 |
sub_ele(block, 'cli-show').text = command |
| 19 |
|
| 20 |
return self._request(node) |
| 21 |
|
| 22 |
|
| 23 |
class GetConfiguration(RPC): |
| 24 |
|
| 25 |
def request(self, content='xml', filter=None, detail=False): |
| 26 |
"""Get config from Alu router |
| 27 |
*content* Content layer. cli or xml |
| 28 |
*filter* specifies the portion of the configuration to retrieve (by default entire configuration is retrieved) |
| 29 |
*detail* Show detailed config in CLI -layer""" |
| 30 |
|
| 31 |
node = new_ele('get-config') |
| 32 |
node.append(util.datastore_or_url('source', 'running', self._assert)) |
| 33 |
|
| 34 |
if filter is not None: |
| 35 |
|
| 36 |
if content == 'xml': |
| 37 |
node.append(util.build_filter(('subtree', filter))) |
| 38 |
|
| 39 |
elif content == 'cli': |
| 40 |
rep = new_ele('filter') |
| 41 |
sub_filter = sub_ele(rep, 'config-format-cli-block') |
| 42 |
|
| 43 |
if filter is not None: |
| 44 |
for item in filter: |
| 45 |
if detail: |
| 46 |
sub_ele(sub_filter, 'cli-info-detail').text = item |
| 47 |
else: |
| 48 |
sub_ele(sub_filter, 'cli-info').text = item |
| 49 |
else: |
| 50 |
if detail: |
| 51 |
sub_ele(sub_filter, 'cli-info-detail') |
| 52 |
else: |
| 53 |
sub_ele(sub_filter, 'cli-info') |
| 54 |
|
| 55 |
node.append(validated_element(rep)) |
| 56 |
|
| 57 |
return self._request(node) |
| 58 |
|
| 59 |
|
| 60 |
class LoadConfiguration(RPC): |
| 61 |
|
| 62 |
def request(self, format='xml', default_operation=None, target='running', config=None): |
| 63 |
node = new_ele('edit-config') |
| 64 |
config_node = new_ele('config') |
| 65 |
|
| 66 |
if default_operation is not None: |
| 67 |
# TODO: check if it is a valid default-operation |
| 68 |
sub_ele(node, "default-operation").text = default_operation |
| 69 |
|
| 70 |
if config is not None: |
| 71 |
if format == 'xml': |
| 72 |
node.append(util.datastore_or_url('target', target, self._assert)) |
| 73 |
config_node.append(config) |
| 74 |
|
| 75 |
if format == 'cli': |
| 76 |
node.append(util.datastore_or_url('target', 'running', self._assert)) |
| 77 |
sub_ele(config_node, 'config-format-cli-block').text = config |
| 78 |
|
| 79 |
node.append(config_node) |
| 80 |
return self._request(node) |