Skip to content

Commit

Permalink
Add NETCONF support for SROS devices (ansible#40330)
Browse files Browse the repository at this point in the history
* added NETCONF support for SROS devices

* corrected alu mapping

* fix pep8 in sros.py

* BOT META file updated
  • Loading branch information
wisotzky authored and ganeshrn committed May 24, 2018
1 parent 387a23c commit 160bf82
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/BOTMETA.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,9 @@ files:
lib/ansible/plugins/lookup/onepassword_raw.py:
maintainers: samdoran
ignored: azenk
lib/ansible/plugins/netconf/sros.py:
maintainers: wisotzky $team_networking
labels: networking
lib/ansible/plugins/shell/powershell.py:
maintainers: $team_windows_core
labels:
Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/plugins/connection/netconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@

NETWORK_OS_DEVICE_PARAM_MAP = {
"nxos": "nexus",
"ios": "default"
"ios": "default",
"sros": "alu"
}


Expand Down
100 changes: 100 additions & 0 deletions lib/ansible/plugins/netconf/sros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#
# (c) 2018 Red Hat Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import json
import re

from ansible import constants as C
from ansible.module_utils._text import to_text, to_bytes
from ansible.errors import AnsibleConnectionFailure, AnsibleError
from ansible.plugins.netconf import NetconfBase
from ansible.plugins.netconf import ensure_connected

try:
from ncclient import manager
from ncclient.operations import RPCError
from ncclient.transport.errors import SSHUnknownHostError
from ncclient.xml_ import to_ele, to_xml, new_ele
except ImportError:
raise AnsibleError("ncclient is not installed")

try:
from lxml import etree
except ImportError:
raise AnsibleError("lxml is not installed")


class Netconf(NetconfBase):
def get_text(self, ele, tag):
try:
return to_text(ele.find(tag).text, errors='surrogate_then_replace').strip()
except AttributeError:
pass

def get_device_info(self):
device_info = dict()
device_info['network_os'] = 'sros'

xmlns = "urn:nokia.com:sros:ns:yang:sr:state"
f = '<state xmlns="%s"><system><platform/><bootup/><version/><lldp/></system></state>' % xmlns
reply = to_ele(self.m.get(filter=('subtree', f)).data_xml)

device_info['network_os_hostname'] = reply.findtext('.//{%s}state/{*}system/{*}lldp/{*}system-name' % xmlns)
device_info['network_os_version'] = reply.findtext('.//{%s}state/{*}system/{*}version/{*}version-number' % xmlns)
device_info['network_os_model'] = reply.findtext('.//{%s}state/{*}system/{*}platform' % xmlns)
device_info['network_os_platform'] = 'Nokia 7x50'
return device_info

def get_capabilities(self):
result = dict()
result['rpc'] = self.get_base_rpc() + ['commit', 'discard_changes', 'validate', 'lock', 'unlock']
result['network_api'] = 'netconf'
result['device_info'] = self.get_device_info()
result['server_capabilities'] = [c for c in self.m.server_capabilities]
result['client_capabilities'] = [c for c in self.m.client_capabilities]
result['session_id'] = self.m.session_id
result['device_operations'] = self.get_device_operations(result['server_capabilities'])
return json.dumps(result)

@staticmethod
def guess_network_os(obj):
try:
m = manager.connect(
host=obj._play_context.remote_addr,
port=obj._play_context.port or 830,
username=obj._play_context.remote_user,
password=obj._play_context.password,
key_filename=obj._play_context.private_key_file,
hostkey_verify=C.HOST_KEY_CHECKING,
look_for_keys=C.PARAMIKO_LOOK_FOR_KEYS,
allow_agent=obj._play_context.allow_agent,
timeout=obj._play_context.timeout
)
except SSHUnknownHostError as exc:
raise AnsibleConnectionFailure(str(exc))

guessed_os = None
for c in m.server_capabilities:
if re.search('urn:nokia.com:sros:ns:yang:sr', c):
guessed_os = 'sros'

m.close_session()
return guessed_os
1 change: 1 addition & 0 deletions test/integration/targets/netconf_get/meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
dependencies:
- { role: prepare_junos_tests, when: ansible_network_os == 'junos' }
- { role: prepare_iosxr_tests, when: ansible_network_os == 'iosxr' }
- { role: prepare_sros_tests, when: ansible_network_os == 'sros' }
1 change: 1 addition & 0 deletions test/integration/targets/netconf_get/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
- { include: junos.yaml, when: ansible_network_os == 'junos', tags: ['netconf'] }
- { include: iosxr.yaml, when: ansible_network_os == 'iosxr', tags: ['netconf'] }
- { include: sros.yaml, when: ansible_network_os == 'sros', tags: ['netconf'] }
16 changes: 16 additions & 0 deletions test/integration/targets/netconf_get/tasks/sros.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- name: collect all netconf test cases
find:
paths: "{{ role_path }}/tests/sros"
patterns: "{{ testcase }}.yaml"
register: test_cases
connection: local

- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"

- name: run test case (connection=netconf)
include: "{{ test_case_to_run }} ansible_connection=netconf"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run
56 changes: 56 additions & 0 deletions test/integration/targets/netconf_get/tests/sros/basic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
- debug: msg="START netconf_get sros/basic.yaml on connection={{ ansible_connection }}"

- name: Get complete configuration data (SROS)
netconf_get:
filter: <configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf"/>
register: result

- assert:
that:
- "'urn:nokia.com:sros:ns:yang:sr:conf' in result.stdout"
- "'urn:nokia.com:sros:ns:yang:sr:state' not in result.stdout"

- name: Get complete state data (SROS)
netconf_get:
filter: <state xmlns="urn:nokia.com:sros:ns:yang:sr:state"/>
register: result

- assert:
that:
- "'urn:nokia.com:sros:ns:yang:sr:state' in result.stdout"
- "'urn:nokia.com:sros:ns:yang:sr:conf' not in result.stdout"

- name: Get service configuration data from candidate datastore (SROS)
netconf_get:
source: candidate
filter: <configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf"><service/></configure>
display: json
register: result

- assert:
that:
- "'<service>' in result.stdout"

- name: Get system configuration data from running datastore (SROS)
netconf_get:
source: running
filter: <configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf"><system/></configure>
register: result

- assert:
that:
- "'<system>' in result.stdout"

- name: Get complete configuration and state data (SROS)
netconf_get:
register: result

- assert:
that:
- "'<service>' in result.stdout"
- "'<system>' in result.stdout"
- "'urn:nokia.com:sros:ns:yang:sr:conf' in result.stdout"
- "'urn:nokia.com:sros:ns:yang:sr:state' in result.stdout"

- debug: msg="END netconf_get sros/basic.yaml on connection={{ ansible_connection }}"
6 changes: 6 additions & 0 deletions test/integration/targets/prepare_sros_tests/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- debug: msg="START prepare_sros_tests/main.yaml"

- name: wait until everything is ready to go
pause:
seconds: 1

0 comments on commit 160bf82

Please sign in to comment.