Remove duplicate code
[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}" routes to IPv4 network "${network}" with prefix '
40              'length "${prefix_length}" using interface "${interface}" via '
41              '"${gateway}"')
42     def set_route(node, network, prefix_length, interface, gateway):
43         """See IPv4Node.set_route for more information.
44
45         :param node:
46         :param network:
47         :param prefix_length:
48         :param interface:
49         :param gateway:
50         :return:
51         """
52         log.debug('Node {} routes to network {} with prefix length {} '
53                   'via {} interface {}'.format(Topology.get_node_hostname(node),
54                                                network, prefix_length,
55                                                gateway, interface))
56         get_node(node).set_route(network, int(prefix_length),
57                                  gateway, interface)
58
59     @staticmethod
60     @keyword('Get IPv4 address prefix of node "${node}" interface "${port}" '
61              'from "${nodes_addr}"')
62     def get_ip_addr_prefix_length(node, port, nodes_addr):
63         """ Get IPv4 address prefix for specified interface.
64
65         :param node: Node dictionary.
66         :param port: Interface name.
67         :return: IPv4 prefix length
68         """
69         for net in nodes_addr.values():
70             for p in net['ports'].values():
71                 if p['node'] == node['host'] and p['if'] == port:
72                     return net['prefix']
73
74         raise Exception('Subnet not found for node {n} port {p}'.
75                         format(n=node['host'], p=port))
76
77     @staticmethod
78     @keyword('Get IPv4 subnet of node "${node}" interface "${port}" from '
79              '"${nodes_addr}"')
80     def get_ip_addr_subnet(node, port, nodes_addr):
81         """ Get IPv4 subnet of specified interface.
82
83         :param node: Node dictionary.
84         :param port: Interface name.
85         :return: IPv4 subnet of 'str' type
86         """
87         for net in nodes_addr.values():
88             for p in net['ports'].values():
89                 if p['node'] == node['host'] and p['if'] == port:
90                     return net['net_addr']
91
92         raise Exception('Subnet not found for node {n} port {p}'.
93                         format(n=node['host'], p=port))
94
95     @staticmethod
96     @keyword('Flush IPv4 addresses "${port}" "${node}"')
97     def flush_ip_addresses(port, node):
98         """See IPv4Node.flush_ip_addresses for more information.
99
100         :param port:
101         :param node:
102         :return:
103         """
104         get_node(node).flush_ip_addresses(port)
105
106     @staticmethod
107     def get_link_address(link, nodes_addr):
108         """Get link IPv4 address.
109
110         :param link: Link name.
111         :param nodes_addr: Available nodes IPv4 adresses.
112         :type link: str
113         :type nodes_addr: dict
114         :return: Link IPv4 address.
115         :rtype: str
116         """
117         net = nodes_addr.get(link)
118         if net is None:
119             raise ValueError('Link "{0}" not found'.format(link))
120         return net.get('net_addr')
121
122     @staticmethod
123     def get_link_prefix(link, nodes_addr):
124         """Get link IPv4 address prefix.
125
126         :param link: Link name.
127         :param nodes_addr: Available nodes IPv4 adresses.
128         :type link: str
129         :type nodes_addr: dict
130         :return: Link IPv4 address prefix.
131         :rtype: int
132         """
133         net = nodes_addr.get(link)
134         if net is None:
135             raise ValueError('Link "{0}" not found'.format(link))
136         return net.get('prefix')