From 33499c81c94c2d3baef9d3e9f061cd76ef86fa74 Mon Sep 17 00:00:00 2001 From: Stefan Kobza Date: Mon, 11 Jan 2016 18:03:25 +0100 Subject: New version of RF tests. Change-Id: I241a2b7a7706e65f71cfd4a62e2a40f053fc5d07 Signed-off-by: Stefan Kobza --- resources/libraries/python/IPv4NodeAddress.py | 104 ++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 resources/libraries/python/IPv4NodeAddress.py (limited to 'resources/libraries/python/IPv4NodeAddress.py') diff --git a/resources/libraries/python/IPv4NodeAddress.py b/resources/libraries/python/IPv4NodeAddress.py new file mode 100644 index 0000000000..0f2c1d9cd3 --- /dev/null +++ b/resources/libraries/python/IPv4NodeAddress.py @@ -0,0 +1,104 @@ +# Copyright (c) 2016 Cisco and/or its affiliates. +# 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. + +"""Robot framework variable file. + + Create dictionary variable nodes_ipv4_addr of IPv4 addresses from + available networks. +""" +from ipaddress import IPv4Network + +# Default list of IPv4 subnets +IPV4_NETWORKS = ['192.168.1.0/24', + '192.168.2.0/24', + '192.168.3.0/24'] + + +class IPv4NetworkGenerator(object): + """IPv4 network generator.""" + def __init__(self, networks): + """ + :param networks: list of strings containing IPv4 subnet + with prefix length + """ + self._networks = list() + for network in networks: + net = IPv4Network(unicode(network)) + subnet, _ = network.split('/') + self._networks.append((net, subnet)) + if len(self._networks) == 0: + raise Exception('No IPv4 networks') + + def next_network(self): + """ + :return: next network in form (IPv4Network, subnet) + """ + if len(self._networks): + return self._networks.pop() + else: + raise StopIteration() + + +def get_variables(networks=IPV4_NETWORKS[:]): + """ + Create dictionary of IPv4 addresses generated from provided subnet list. + + Example of returned dictionary: + network = { + 'NET1': { + 'subnet': '192.168.1.0', + 'prefix': 24, + 'port1': { + 'addr': '192.168.1.1', + }, + 'port2': { + 'addr': '192.168.1.0', + }, + }, + 'NET2': { + 'subnet': '192.168.2.0', + 'prefix': 24, + 'port1': { + 'addr': '192.168.2.1', + }, + 'port2': { + 'addr': '192.168.2.2', + }, + }, + } + + This function is called by RobotFramework automatically. + + :param networks: list of subnets in form a.b.c.d/length + :return: Dictionary of IPv4 addresses + """ + net_object = IPv4NetworkGenerator(networks) + + network = {} + interface_count_per_node = 2 + + for subnet_num in range(len(networks)): + net, net_str = net_object.next_network() + key = 'NET{}'.format(subnet_num + 1) + network[key] = { + 'subnet': net_str, + 'prefix': net.prefixlen, + } + hosts = net.hosts() + for port_num in range(interface_count_per_node): + port = 'port{}'.format(port_num + 1) + network[key][port] = { + 'addr': str(next(hosts)), + } + + return {'DICT__nodes_ipv4_addr': network} -- cgit 1.2.3-korg