5ee73c08fc95f49250080eba5d25fbd577ae46a8
[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     def set_route(node, network, prefix_length, interface, gateway):
55         """See IPv4Node.set_route for more information.
56
57         :param node: Node where IP address should be set to.
58         :param network: IP network.
59         :param prefix_length: Prefix length.
60         :param interface: Interface name.
61         :param gateway: Gateway.
62         :type node: dict
63         :type network: str
64         :type prefix_length: int
65         :type interface: str
66         :type gateway: str
67         """
68         log.debug('Node {} routes to network {} with prefix length {} '
69                   'via {} interface {}'.format(Topology.get_node_hostname(node),
70                                                network, prefix_length,
71                                                gateway, interface))
72         get_node(node).set_route(network, int(prefix_length),
73                                  gateway, interface)
74
75     @staticmethod
76     @keyword('Get IPv4 address prefix of node "${node}" interface "${port}" '
77              'from "${nodes_addr}"')
78     def get_ip_addr_prefix_length(node, port, nodes_addr):
79         """ Get IPv4 address prefix for specified interface.
80
81         :param node: Node dictionary.
82         :param port: Interface name.
83         :param nodes_addr: Available nodes IPv4 addresses.
84         :type node: dict
85         :type port: str
86         :type nodes_addr: dict
87         :return: IPv4 prefix length.
88         :rtype: int
89         """
90         for net in nodes_addr.values():
91             for p in net['ports'].values():
92                 if p['node'] == node['host'] and p['if'] == port:
93                     return net['prefix']
94
95         raise Exception('Subnet not found for node {n} port {p}'.
96                         format(n=node['host'], p=port))
97
98     @staticmethod
99     @keyword('Get IPv4 subnet of node "${node}" interface "${port}" from '
100              '"${nodes_addr}"')
101     def get_ip_addr_subnet(node, port, nodes_addr):
102         """ Get IPv4 subnet of specified interface.
103
104         :param node: Node dictionary.
105         :param port: Interface name.
106         :param nodes_addr: Available nodes IPv4 addresses.
107         :type node: dict
108         :type port: int
109         :type nodes_addr: dict
110         :return: IPv4 subnet.
111         :rtype: str
112         """
113         for net in nodes_addr.values():
114             for p in net['ports'].values():
115                 if p['node'] == node['host'] and p['if'] == port:
116                     return net['net_addr']
117
118         raise Exception('Subnet not found for node {n} port {p}'.
119                         format(n=node['host'], p=port))
120
121     @staticmethod
122     @keyword('Flush IPv4 addresses "${port}" "${node}"')
123     def flush_ip_addresses(port, node):
124         """See IPv4Node.flush_ip_addresses for more information.
125
126         :param port:
127         :param node:
128         :return:
129         """
130         get_node(node).flush_ip_addresses(port)
131
132     @staticmethod
133     def get_link_address(link, nodes_addr):
134         """Get link IPv4 address.
135
136         :param link: Link name.
137         :param nodes_addr: Available nodes IPv4 addresses.
138         :type link: str
139         :type nodes_addr: dict
140         :return: Link IPv4 address.
141         :rtype: str
142         """
143         net = nodes_addr.get(link)
144         if net is None:
145             raise ValueError('Link "{0}" not found'.format(link))
146         return net.get('net_addr')
147
148     @staticmethod
149     def get_link_prefix(link, nodes_addr):
150         """Get link IPv4 address prefix.
151
152         :param link: Link name.
153         :param nodes_addr: Available nodes IPv4 addresses.
154         :type link: str
155         :type nodes_addr: dict
156         :return: Link IPv4 address prefix.
157         :rtype: int
158         """
159         net = nodes_addr.get(link)
160         if net is None:
161             raise ValueError('Link "{0}" not found'.format(link))
162         return net.get('prefix')