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
21 from resources.libraries.python.ssh import exec_cmd
24 class IPv4Util(object):
25 """Implements keywords for IPv4 tests."""
28 @keyword('From node "${node}" interface "${port}" ARP-ping '
29 'IPv4 address "${ip_address}"')
30 def arp_ping(node, interface, ip_address):
31 log.debug('From node {} interface {} ARP-ping IPv4 address {}'.
32 format(Topology.get_node_hostname(node),
33 interface, ip_address))
34 get_node(node).arp_ping(ip_address, interface)
37 def set_interface_address(node, interface, address, prefix_length):
38 """See IPv4Node.set_ip for more information.
40 :param node: Node where IP address should be set to.
41 :param interface: Interface name.
42 :param address: IP address.
43 :param prefix_length: Prefix length.
47 :type prefix_length: int
49 log.debug('Node {} interface {} has IPv4 address {} with prefix '
50 'length {}'.format(Topology.get_node_hostname(node),
51 interface, address, prefix_length))
52 get_node(node).set_ip(interface, address, int(prefix_length))
55 def set_route(node, network, prefix_length, interface, gateway):
56 """See IPv4Node.set_route for more information.
58 :param node: Node where IP address should be set to.
59 :param network: IP network.
60 :param prefix_length: Prefix length.
61 :param interface: Interface name.
62 :param gateway: Gateway.
65 :type prefix_length: int
69 log.debug('Node {} routes to network {} with prefix length {} '
70 'via {} interface {}'.format(Topology.get_node_hostname(node),
71 network, prefix_length,
73 get_node(node).set_route(network, int(prefix_length),
77 @keyword('Get IPv4 address prefix of node "${node}" interface "${port}" '
78 'from "${nodes_addr}"')
79 def get_ip_addr_prefix_length(node, port, nodes_addr):
80 """ Get IPv4 address prefix for specified interface.
82 :param node: Node dictionary.
83 :param port: Interface name.
84 :param nodes_addr: Available nodes IPv4 addresses.
87 :type nodes_addr: dict
88 :return: IPv4 prefix length.
91 for net in nodes_addr.values():
92 for p in net['ports'].values():
93 if p['node'] == node['host'] and p['if'] == port:
96 raise Exception('Subnet not found for node {n} port {p}'.
97 format(n=node['host'], p=port))
100 @keyword('Get IPv4 subnet of node "${node}" interface "${port}" from '
102 def get_ip_addr_subnet(node, port, nodes_addr):
103 """ Get IPv4 subnet of specified interface.
105 :param node: Node dictionary.
106 :param port: Interface name.
107 :param nodes_addr: Available nodes IPv4 addresses.
110 :type nodes_addr: dict
111 :return: IPv4 subnet.
114 for net in nodes_addr.values():
115 for p in net['ports'].values():
116 if p['node'] == node['host'] and p['if'] == port:
117 return net['net_addr']
119 raise Exception('Subnet not found for node {n} port {p}'.
120 format(n=node['host'], p=port))
123 @keyword('Flush IPv4 addresses "${port}" "${node}"')
124 def flush_ip_addresses(port, node):
125 """See IPv4Node.flush_ip_addresses for more information.
131 get_node(node).flush_ip_addresses(port)
134 def get_link_address(link, nodes_addr):
135 """Get link IPv4 address.
137 :param link: Link name.
138 :param nodes_addr: Available nodes IPv4 addresses.
140 :type nodes_addr: dict
141 :return: Link IPv4 address.
144 net = nodes_addr.get(link)
146 raise ValueError('Link "{0}" not found'.format(link))
147 return net.get('net_addr')
150 def get_link_prefix(link, nodes_addr):
151 """Get link IPv4 address prefix.
153 :param link: Link name.
154 :param nodes_addr: Available nodes IPv4 addresses.
156 :type nodes_addr: dict
157 :return: Link IPv4 address prefix.
160 net = nodes_addr.get(link)
162 raise ValueError('Link "{0}" not found'.format(link))
163 return net.get('prefix')
166 def send_ping_from_node_to_dst(node, destination, namespace=None,
168 """Send a ping from node to destination. Optionally, you can define a
169 namespace from where to send a ping.
171 :param node: Node to start ping on.
172 :param destination: IPv4 address where to send ping.
173 :param namespace: Namespace to send ping from.
174 :param ping_count: Number of pings to send.
176 :type destination: str
178 :type ping_count: int
179 :raises RuntimeError: If no response for ping, raise error
182 if namespace is not None:
183 cmd = 'ip netns exec {0} ping -c{1} {2}'.format(
184 namespace, ping_count, destination)
186 cmd = 'ping -c{0} {1}'.format(ping_count, destination)
187 rc, stdout, stderr = exec_cmd(node, cmd, sudo=True)
189 raise RuntimeError("Ping Not Successful")