Add VXLAN test
[csit.git] / resources / libraries / python / IPv4Util.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 """Implements IPv4 RobotFramework keywords"""
15
16 from robot.api import logger as log
17 from robot.api.deco import keyword
18
19 from resources.libraries.python.topology import Topology
20 from resources.libraries.python.topology import NodeType
21 from resources.libraries.python.TrafficScriptExecutor\
22     import TrafficScriptExecutor
23 from resources.libraries.python.IPv4Setup import get_node
24
25
26 class IPv4Util(object):
27     """Implements keywords for IPv4 tests."""
28
29     @staticmethod
30     @keyword('From node "${node}" interface "${port}" ARP-ping '
31              'IPv4 address "${ip_address}"')
32     def arp_ping(node, interface, ip_address):
33         log.debug('From node {} interface {} ARP-ping IPv4 address {}'.
34                   format(Topology.get_node_hostname(node),
35                          interface, ip_address))
36         get_node(node).arp_ping(ip_address, interface)
37
38     @staticmethod
39     @keyword('Node "${node}" interface "${port}" has IPv4 address "${address}"'
40              ' with prefix length "${prefix_length}"')
41     def set_interface_address(node, interface, address, length):
42         """See IPv4Node.set_ip for more information.
43
44         :param node: Node where IP address should be set to.
45         :param interface: Interface name
46         :param address: IP address
47         :param length: prefix length
48         :type node: dict
49         :type interface: str
50         :type address: str
51         :type length: int
52         """
53         log.debug('Node {} interface {} has IPv4 address {} with prefix '
54                   'length {}'.format(Topology.get_node_hostname(node),
55                                      interface, address, length))
56         get_node(node).set_ip(interface, address, int(length))
57
58     @staticmethod
59     @keyword('Node "${node}" routes to IPv4 network "${network}" with prefix '
60              'length "${prefix_length}" using interface "${interface}" via '
61              '"${gateway}"')
62     def set_route(node, network, prefix_length, interface, gateway):
63         """See IPv4Node.set_route for more information.
64
65         :param node:
66         :param network:
67         :param prefix_length:
68         :param interface:
69         :param gateway:
70         :return:
71         """
72         log.debug('Node {} routes to network {} with prefix length {} '
73                   'via {} interface {}'.format(Topology.get_node_hostname(node),
74                                                network, prefix_length,
75                                                gateway, interface))
76         get_node(node).set_route(network, int(prefix_length),
77                                  gateway, interface)
78
79     @staticmethod
80     @keyword('Get IPv4 address prefix of node "${node}" interface "${port}" '
81              'from "${nodes_addr}"')
82     def get_ip_addr_prefix_length(node, port, nodes_addr):
83         """ Get IPv4 address prefix for specified interface.
84
85         :param node: Node dictionary.
86         :param port: Interface name.
87         :return: IPv4 prefix length
88         """
89         for net in nodes_addr.values():
90             for p in net['ports'].values():
91                 if p['node'] == node['host'] and p['if'] == port:
92                     return net['prefix']
93
94         raise Exception('Subnet not found for node {n} port {p}'.
95                         format(n=node['host'], p=port))
96
97     @staticmethod
98     @keyword('Get IPv4 subnet of node "${node}" interface "${port}" from '
99              '"${nodes_addr}"')
100     def get_ip_addr_subnet(node, port, nodes_addr):
101         """ Get IPv4 subnet of specified interface.
102
103         :param node: Node dictionary.
104         :param port: Interface name.
105         :return: IPv4 subnet of 'str' type
106         """
107         for net in nodes_addr.values():
108             for p in net['ports'].values():
109                 if p['node'] == node['host'] and p['if'] == port:
110                     return net['net_addr']
111
112         raise Exception('Subnet not found for node {n} port {p}'.
113                         format(n=node['host'], p=port))
114
115     @staticmethod
116     @keyword('Flush IPv4 addresses "${port}" "${node}"')
117     def flush_ip_addresses(port, node):
118         """See IPv4Node.flush_ip_addresses for more information.
119
120         :param port:
121         :param node:
122         :return:
123         """
124         get_node(node).flush_ip_addresses(port)
125
126     @staticmethod
127     def get_link_address(link, nodes_addr):
128         """Get link IPv4 address.
129
130         :param link: Link name.
131         :param nodes_addr: Available nodes IPv4 adresses.
132         :type link: str
133         :type nodes_addr: dict
134         :return: Link IPv4 address.
135         :rtype: str
136         """
137         net = nodes_addr.get(link)
138         if net is None:
139             raise ValueError('Link "{0}" not found'.format(link))
140         return net.get('net_addr')
141
142     @staticmethod
143     def get_link_prefix(link, nodes_addr):
144         """Get link IPv4 address prefix.
145
146         :param link: Link name.
147         :param nodes_addr: Available nodes IPv4 adresses.
148         :type link: str
149         :type nodes_addr: dict
150         :return: Link IPv4 address prefix.
151         :rtype: int
152         """
153         net = nodes_addr.get(link)
154         if net is None:
155             raise ValueError('Link "{0}" not found'.format(link))
156         return net.get('prefix')