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