Update of latest tests.
[csit.git] / resources / libraries / python / IPv4NodeAddress.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Robot framework variable file.
15
16    Create dictionary variable nodes_ipv4_addr of IPv4 addresses from
17    available networks.
18 """
19 from ipaddress import IPv4Network
20
21 # Default list of IPv4 subnets
22 IPV4_NETWORKS = ['20.20.20.0/24',
23                  '10.10.10.0/24',
24                  '1.1.1.0/30']
25
26
27 class IPv4NetworkGenerator(object):
28     """IPv4 network generator."""
29     def __init__(self, networks):
30         """
31         :param networks: list of strings containing IPv4 subnet
32         with prefix length
33         """
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')
41
42     def next_network(self):
43         """
44         :return: next network in form (IPv4Network, subnet)
45         """
46         if len(self._networks):
47             return self._networks.pop()
48         else:
49             raise StopIteration()
50
51
52 def get_variables(networks=IPV4_NETWORKS[:]):
53     """
54     Create dictionary of IPv4 addresses generated from provided subnet list.
55
56     Example of returned dictionary:
57         network = {
58         'NET1': {
59             'subnet': '192.168.1.0',
60             'prefix': 24,
61             'port1': {
62                 'addr': '192.168.1.1',
63             },
64             'port2': {
65                 'addr': '192.168.1.0',
66             },
67         },
68         'NET2': {
69             'subnet': '192.168.2.0',
70             'prefix': 24,
71             'port1': {
72                 'addr': '192.168.2.1',
73             },
74             'port2': {
75                 'addr': '192.168.2.2',
76             },
77         },
78     }
79
80     This function is called by RobotFramework automatically.
81
82     :param networks: list of subnets in form a.b.c.d/length
83     :return: Dictionary of IPv4 addresses
84     """
85     net_object = IPv4NetworkGenerator(networks)
86
87     network = {}
88     interface_count_per_node = 2
89
90     for subnet_num in range(len(networks)):
91         net, net_str = net_object.next_network()
92         key = 'NET{}'.format(subnet_num + 1)
93         network[key] = {
94             'subnet': net_str,
95             'prefix': net.prefixlen,
96         }
97         hosts = net.hosts()
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)),
102             }
103
104     return {'DICT__nodes_ipv4_addr': network}