CSIT-622: Stateful Security Groups perf tests
[csit.git] / resources / libraries / python / IPv6NodesAddr.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_ipv6_addr with IPv6 addresses from available
17 networks.
18 """
19
20 from resources.libraries.python.IPv6Setup import IPv6Networks
21 from resources.libraries.python.topology import Topology
22
23 # Default list of available IPv6 networks
24 IPV6_NETWORKS = ['3ffe:{0:04x}::/64'.format(i) for i in range(1, 100)]
25
26
27 def get_variables(nodes, networks=IPV6_NETWORKS):
28     """Special robot framework method that returns dictionary nodes_ipv6_addr,
29     mapping of node and interface name to IPv6 address.
30
31     :param nodes: Nodes of the test topology.
32     :param networks: List of available IPv6 networks.
33     :type nodes: dict
34     :type networks: list
35
36     .. note::
37        Robot framework calls it automatically.
38     """
39     topo = Topology()
40     links = topo.get_links(nodes)
41
42     if len(links) > len(networks):
43         raise Exception('Not enough available IPv6 networks for topology.')
44
45     ip6_n = IPv6Networks(networks)
46
47     nets = {}
48
49     for link in links:
50         ip6_net = ip6_n.next_network()
51         net_hosts = ip6_net.hosts()
52         port_idx = 0
53         ports = {}
54         for node in nodes.values():
55             if_key = topo.get_interface_by_link_name(node, link)
56             if_name = topo.get_interface_name(node, if_key)
57             if if_name is not None:
58                 port = {'addr': str(next(net_hosts)),
59                         'node': node['host'],
60                         'if': if_name}
61                 port_idx += 1
62                 port_id = 'port{0}'.format(port_idx)
63                 ports.update({port_id: port})
64         nets.update({link: {'net_addr': str(ip6_net.network_address),
65                             'prefix': ip6_net.prefixlen,
66                             'ports': ports}})
67
68     return {'DICT__nodes_ipv6_addr': nets}