Add more IP networks to IPV4_NETWORKS and IPV6_NETWORKS
[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 from resources.libraries.python.topology import Topology
22
23 # Default list of IPv4 subnets
24 IPV4_NETWORKS = ['192.168.{}.0/24'.format(i) for i in range(1, 100)]
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             self._networks.append(net)
38         if len(self._networks) == 0:
39             raise Exception('No IPv4 networks')
40
41     def next_network(self):
42         """
43         :return: next network in form (IPv4Network, subnet)
44         :raises: StopIteration if there are no more elements.
45         """
46         if len(self._networks):
47             return self._networks.pop()
48         else:
49             raise StopIteration()
50
51
52 def get_variables(nodes, networks=IPV4_NETWORKS[:]):
53     """Special robot framework method that returns dictionary nodes_ipv4_addr,
54        mapping of node and interface name to IPv4 adddress.
55
56        :param nodes: Nodes of the test topology.
57        :param networks: list of available IPv4 networks
58        :type nodes: dict
59        :type networks: list
60
61        .. note::
62            Robot framework calls it automatically.
63     """
64     topo = Topology()
65     links = topo.get_links(nodes)
66
67     if len(links) > len(networks):
68         raise Exception('Not enough available IPv4 networks for topology.')
69
70     ip4_n = IPv4NetworkGenerator(networks)
71
72     nets = {}
73
74     for link in links:
75         ip4_net = ip4_n.next_network()
76         net_hosts = ip4_net.hosts()
77         port_idx = 0
78         ports = {}
79         for node in nodes.values():
80             if_name = topo.get_interface_by_link_name(node, link)
81             if if_name is not None:
82                 port = {'addr': str(next(net_hosts)),
83                         'node': node['host'],
84                         'if': if_name}
85                 port_idx += 1
86                 port_id = 'port{0}'.format(port_idx)
87                 ports.update({port_id: port})
88         nets.update({link: {'net_addr': str(ip4_net.network_address),
89                             'prefix': ip4_net.prefixlen,
90                             'ports': ports}})
91
92     return {'DICT__nodes_ipv4_addr': nets}