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.topology import NodeType
21 from resources.libraries.python.TrafficScriptExecutor\
22 import TrafficScriptExecutor
23 from resources.libraries.python.IPv4Setup import get_node
26 class IPv4Util(object):
27 """Implements keywords for IPv4 tests."""
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)
39 def set_interface_address(node, interface, address, length):
40 """See IPv4Node.set_ip for more information.
42 :param node: Node where IP address should be set to.
43 :param interface: Interface name
44 :param address: IP address
45 :param length: prefix length
51 log.debug('Node {} interface {} has IPv4 address {} with prefix '
52 'length {}'.format(Topology.get_node_hostname(node),
53 interface, address, length))
54 get_node(node).set_ip(interface, address, int(length))
57 @keyword('Node "${node}" routes to IPv4 network "${network}" with prefix '
58 'length "${prefix_length}" using interface "${interface}" via '
60 def set_route(node, network, prefix_length, interface, gateway):
61 """See IPv4Node.set_route for more information.
70 log.debug('Node {} routes to network {} with prefix length {} '
71 'via {} interface {}'.format(Topology.get_node_hostname(node),
72 network, prefix_length,
74 get_node(node).set_route(network, int(prefix_length),
78 @keyword('Get IPv4 address prefix of node "${node}" interface "${port}" '
79 'from "${nodes_addr}"')
80 def get_ip_addr_prefix_length(node, port, nodes_addr):
81 """ Get IPv4 address prefix for specified interface.
83 :param node: Node dictionary.
84 :param port: Interface name.
85 :return: IPv4 prefix length
87 for net in nodes_addr.values():
88 for p in net['ports'].values():
89 if p['node'] == node['host'] and p['if'] == port:
92 raise Exception('Subnet not found for node {n} port {p}'.
93 format(n=node['host'], p=port))
96 @keyword('Get IPv4 subnet of node "${node}" interface "${port}" from '
98 def get_ip_addr_subnet(node, port, nodes_addr):
99 """ Get IPv4 subnet of specified interface.
101 :param node: Node dictionary.
102 :param port: Interface name.
103 :return: IPv4 subnet of 'str' type
105 for net in nodes_addr.values():
106 for p in net['ports'].values():
107 if p['node'] == node['host'] and p['if'] == port:
108 return net['net_addr']
110 raise Exception('Subnet not found for node {n} port {p}'.
111 format(n=node['host'], p=port))
114 @keyword('Flush IPv4 addresses "${port}" "${node}"')
115 def flush_ip_addresses(port, node):
116 """See IPv4Node.flush_ip_addresses for more information.
122 get_node(node).flush_ip_addresses(port)
125 def get_link_address(link, nodes_addr):
126 """Get link IPv4 address.
128 :param link: Link name.
129 :param nodes_addr: Available nodes IPv4 adresses.
131 :type nodes_addr: dict
132 :return: Link IPv4 address.
135 net = nodes_addr.get(link)
137 raise ValueError('Link "{0}" not found'.format(link))
138 return net.get('net_addr')
141 def get_link_prefix(link, nodes_addr):
142 """Get link IPv4 address prefix.
144 :param link: Link name.
145 :param nodes_addr: Available nodes IPv4 adresses.
147 :type nodes_addr: dict
148 :return: Link IPv4 address prefix.
151 net = nodes_addr.get(link)
153 raise ValueError('Link "{0}" not found'.format(link))
154 return net.get('prefix')