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