1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Robot framework variable file.
16 Create dictionary variable nodes_ipv4_addr of IPv4 addresses from
19 from ipaddress import IPv4Network
21 # Default list of IPv4 subnets
22 IPV4_NETWORKS = ['20.20.20.0/24',
27 class IPv4NetworkGenerator(object):
28 """IPv4 network generator."""
29 def __init__(self, networks):
31 :param networks: list of strings containing IPv4 subnet
34 self._networks = list()
35 for network in networks:
36 net = IPv4Network(unicode(network))
37 subnet, _ = network.split('/')
38 self._networks.append((net, subnet))
39 if len(self._networks) == 0:
40 raise Exception('No IPv4 networks')
42 def next_network(self):
44 :return: next network in form (IPv4Network, subnet)
46 if len(self._networks):
47 return self._networks.pop()
52 def get_variables(networks=IPV4_NETWORKS[:]):
54 Create dictionary of IPv4 addresses generated from provided subnet list.
56 Example of returned dictionary:
59 'subnet': '192.168.1.0',
62 'addr': '192.168.1.1',
65 'addr': '192.168.1.0',
69 'subnet': '192.168.2.0',
72 'addr': '192.168.2.1',
75 'addr': '192.168.2.2',
80 This function is called by RobotFramework automatically.
82 :param networks: list of subnets in form a.b.c.d/length
83 :return: Dictionary of IPv4 addresses
85 net_object = IPv4NetworkGenerator(networks)
88 interface_count_per_node = 2
90 for subnet_num in range(len(networks)):
91 net, net_str = net_object.next_network()
92 key = 'NET{}'.format(subnet_num + 1)
95 'prefix': net.prefixlen,
98 for port_num in range(interface_count_per_node):
99 port = 'port{}'.format(port_num + 1)
100 network[key][port] = {
101 'addr': str(next(hosts)),
104 return {'DICT__nodes_ipv4_addr': network}