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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Implements IPv4 RobotFramework keywords"""
16 from robot.api import logger as log
17 from robot.api.deco import keyword
19 from resources.libraries.python.topology import Topology
20 from resources.libraries.python.IPv4Setup import get_node
23 class IPv4Util(object):
24 """Implements keywords for IPv4 tests."""
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)
36 def set_interface_address(node, interface, address, prefix_length):
37 """See IPv4Node.set_ip for more information.
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.
46 :type prefix_length: int
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))
54 @keyword('Node "${node}" routes to IPv4 network "${network}" with prefix '
55 'length "${prefix_length}" using interface "${interface}" via '
57 def set_route(node, network, prefix_length, interface, gateway):
58 """See IPv4Node.set_route for more information.
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.
67 :type prefix_length: int
71 log.debug('Node {} routes to network {} with prefix length {} '
72 'via {} interface {}'.format(Topology.get_node_hostname(node),
73 network, prefix_length,
75 get_node(node).set_route(network, int(prefix_length),
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.
84 :param node: Node dictionary.
85 :param port: Interface name.
86 :param nodes_addr: Available nodes IPv4 addresses.
89 :type nodes_addr: dict
90 :return: IPv4 prefix length.
93 for net in nodes_addr.values():
94 for p in net['ports'].values():
95 if p['node'] == node['host'] and p['if'] == port:
98 raise Exception('Subnet not found for node {n} port {p}'.
99 format(n=node['host'], p=port))
102 @keyword('Get IPv4 subnet of node "${node}" interface "${port}" from '
104 def get_ip_addr_subnet(node, port, nodes_addr):
105 """ Get IPv4 subnet of specified interface.
107 :param node: Node dictionary.
108 :param port: Interface name.
109 :param nodes_addr: Available nodes IPv4 addresses.
112 :type nodes_addr: dict
113 :return: IPv4 subnet.
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']
121 raise Exception('Subnet not found for node {n} port {p}'.
122 format(n=node['host'], p=port))
125 @keyword('Flush IPv4 addresses "${port}" "${node}"')
126 def flush_ip_addresses(port, node):
127 """See IPv4Node.flush_ip_addresses for more information.
133 get_node(node).flush_ip_addresses(port)
136 def get_link_address(link, nodes_addr):
137 """Get link IPv4 address.
139 :param link: Link name.
140 :param nodes_addr: Available nodes IPv4 addresses.
142 :type nodes_addr: dict
143 :return: Link IPv4 address.
146 net = nodes_addr.get(link)
148 raise ValueError('Link "{0}" not found'.format(link))
149 return net.get('net_addr')
152 def get_link_prefix(link, nodes_addr):
153 """Get link IPv4 address prefix.
155 :param link: Link name.
156 :param nodes_addr: Available nodes IPv4 addresses.
158 :type nodes_addr: dict
159 :return: Link IPv4 address prefix.
162 net = nodes_addr.get(link)
164 raise ValueError('Link "{0}" not found'.format(link))
165 return net.get('prefix')