From fb887f79671ec37656199ebf4946488bd3a76416 Mon Sep 17 00:00:00 2001 From: gifrerenom <lluis.gifre@cttc.es> Date: Sat, 17 Jun 2023 08:02:53 +0000 Subject: [PATCH] Device component: - Added minor tests for gNMI and NetConf --- src/device/tests/test_gnmi.py | 115 +++++++++++++++++++++++++++++++ src/device/tests/test_netconf.py | 31 +++++++++ 2 files changed, 146 insertions(+) create mode 100644 src/device/tests/test_gnmi.py create mode 100644 src/device/tests/test_netconf.py diff --git a/src/device/tests/test_gnmi.py b/src/device/tests/test_gnmi.py new file mode 100644 index 000000000..50c915582 --- /dev/null +++ b/src/device/tests/test_gnmi.py @@ -0,0 +1,115 @@ +# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging, os, sys, time +from typing import Dict, Tuple +os.environ['DEVICE_EMULATED_ONLY'] = 'YES' +from device.service.drivers.gnmi_openconfig.GnmiOpenConfigDriver import GnmiOpenConfigDriver # pylint: disable=wrong-import-position +#from device.service.driver_api._Driver import ( +# RESOURCE_ENDPOINTS, RESOURCE_INTERFACES, RESOURCE_NETWORK_INSTANCES, RESOURCE_ROUTING_POLICIES, RESOURCE_SERVICES +#) + +logging.basicConfig(level=logging.DEBUG) +LOGGER = logging.getLogger(__name__) +LOGGER.setLevel(logging.DEBUG) + +# +---+---------------------------+--------------+---------------------------------+-------+---------+--------------------+--------------+ +# | # | Name | Container ID | Image | Kind | State | IPv4 Address | IPv6 Address | +# +---+---------------------------+--------------+---------------------------------+-------+---------+--------------------+--------------+ +# | 1 | clab-tfs-scenario-client1 | a8d48ec3265a | ghcr.io/hellt/network-multitool | linux | running | 172.100.100.201/24 | N/A | +# | 2 | clab-tfs-scenario-client2 | fc88436d2b32 | ghcr.io/hellt/network-multitool | linux | running | 172.100.100.202/24 | N/A | +# | 3 | clab-tfs-scenario-srl1 | b995b9bdadda | ghcr.io/nokia/srlinux | srl | running | 172.100.100.101/24 | N/A | +# | 4 | clab-tfs-scenario-srl2 | aacfc38cc376 | ghcr.io/nokia/srlinux | srl | running | 172.100.100.102/24 | N/A | +# +---+---------------------------+--------------+---------------------------------+-------+---------+--------------------+--------------+ + +def interface(if_name, sif_index, ipv4_address, ipv4_prefix, enabled) -> Tuple[str, Dict]: + str_path = '/interface[{:s}]'.format(if_name) + str_data = {'name': if_name, 'enabled': enabled, 'sub_if_index': sif_index, 'sub_if_enabled': enabled, + 'sub_if_ipv4_enabled': enabled, 'sub_if_ipv4_address': ipv4_address, 'sub_if_ipv4_prefix': ipv4_prefix} + return str_path, str_data + +def network_instance(ni_name, ni_type) -> Tuple[str, Dict]: + str_path = '/network_instance[{:s}]'.format(ni_name) + str_data = {'name': ni_name, 'type': ni_type} + return str_path, str_data + +def network_instance_static_route(ni_name, prefix, next_hop, next_hop_index=0) -> Tuple[str, Dict]: + str_path = '/network_instance[{:s}]/static_route[{:s}]'.format(ni_name, prefix) + str_data = {'name': ni_name, 'prefix': prefix, 'next_hop': next_hop, 'next_hop_index': next_hop_index} + return str_path, str_data + +def network_instance_interface(ni_name, if_name, sif_index) -> Tuple[str, Dict]: + str_path = '/network_instance[{:s}]/interface[{:s}.{:d}]'.format(ni_name, if_name, sif_index) + str_data = {'name': ni_name, 'if_name': if_name, 'sif_index': sif_index} + return str_path, str_data + +def main(): + driver_settings = { + 'protocol': 'gnmi', + 'username': 'admin', + 'password': 'NokiaSrl1!', + 'use_tls' : True, + } + driver = GnmiOpenConfigDriver('172.100.100.102', 57400, **driver_settings) + driver.Connect() + + #resources_to_get = [] + #resources_to_get = [RESOURCE_ENDPOINTS] + #resources_to_get = [RESOURCE_INTERFACES] + #resources_to_get = [RESOURCE_NETWORK_INSTANCES] + #resources_to_get = [RESOURCE_ROUTING_POLICIES] + #resources_to_get = [RESOURCE_SERVICES] + #LOGGER.info('resources_to_get = {:s}'.format(str(resources_to_get))) + #results_getconfig = driver.GetConfig(resources_to_get) + #LOGGER.info('results_getconfig = {:s}'.format(str(results_getconfig))) + + #resources_to_set = [ + # network_instance('test-svc', 'L3VRF'), + # + # interface('ethernet-1/1', 0, '172.16.0.1', 24, True), + # network_instance_interface('test-svc', 'ethernet-1/1', 0), + # + # interface('ethernet-1/2', 0, '172.0.0.1', 24, True), + # network_instance_interface('test-svc', 'ethernet-1/2', 0), + # + # network_instance_static_route('test-svc', '172.0.0.0/24', '172.16.0.2'), + # network_instance_static_route('test-svc', '172.2.0.0/24', '172.16.0.3'), + #] + #LOGGER.info('resources_to_set = {:s}'.format(str(resources_to_set))) + #results_setconfig = driver.SetConfig(resources_to_set) + #LOGGER.info('results_setconfig = {:s}'.format(str(results_setconfig))) + + resources_to_delete = [ + #network_instance_static_route('d35fc1d9', '172.0.0.0/24', '172.16.0.2'), + #network_instance_static_route('d35fc1d9', '172.2.0.0/24', '172.16.0.3'), + + #network_instance_interface('d35fc1d9', 'ethernet-1/1', 0), + #network_instance_interface('d35fc1d9', 'ethernet-1/2', 0), + + interface('ethernet-1/1', 0, '172.16.1.1', 24, True), + interface('ethernet-1/2', 0, '172.0.0.2', 24, True), + + network_instance('20f66fb5', 'L3VRF'), + ] + LOGGER.info('resources_to_delete = {:s}'.format(str(resources_to_delete))) + results_deleteconfig = driver.DeleteConfig(resources_to_delete) + LOGGER.info('results_deleteconfig = {:s}'.format(str(results_deleteconfig))) + + time.sleep(1) + + driver.Disconnect() + return 0 + +if __name__ == '__main__': + sys.exit(main()) diff --git a/src/device/tests/test_netconf.py b/src/device/tests/test_netconf.py new file mode 100644 index 000000000..70551eed7 --- /dev/null +++ b/src/device/tests/test_netconf.py @@ -0,0 +1,31 @@ +# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ncclient.manager import Manager, connect_ssh + +str_filter = '''<filter> + <components xmlns="http://openconfig.net/yang/platform"> + <component/> + </components> +</filter>''' + +_manager : Manager = connect_ssh( + host='10.5.32.3', port=830, username='admin', password='admin', + device_params={'name': 'huaweiyang'}, manager_params={'timeout': 120}, + key_filename=None, hostkey_verify=False, allow_agent=False, + look_for_keys=False) +c = _manager.get(filter=str_filter, with_defaults=None).data_xml +with open('data.xml', 'w') as f: + f.write(c) +_manager.close_session() -- GitLab