Add with-statment support to VatTerminal.
[csit.git] / resources / libraries / python / InterfaceUtil.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 """Interface util library"""
15
16 from time import time, sleep
17 from robot.api import logger
18 from resources.libraries.python.ssh import exec_cmd_no_error
19 from resources.libraries.python.topology import NodeType, Topology
20 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
21
22
23 class InterfaceUtil(object):
24     """General utilities for managing interfaces"""
25
26     @staticmethod
27     def set_interface_state(node, interface, state):
28         """Set interface state on a node.
29
30         Function can be used for DUTs as well as for TGs.
31
32         :param node: node where the interface is
33         :param interface: interface name
34         :param state: one of 'up' or 'down'
35         :type node: dict
36         :type interface: str
37         :type state: str
38         :return: nothing
39         """
40         if node['type'] == NodeType.DUT:
41             if state == 'up':
42                 state = 'admin-up'
43             elif state == 'down':
44                 state = 'admin-down'
45             else:
46                 raise ValueError('Unexpected interface state: {}'.format(state))
47
48             sw_if_index = Topology.get_interface_sw_index(node, interface)
49             VatExecutor.cmd_from_template(node, 'set_if_state.vat',
50                                           sw_if_index=sw_if_index, state=state)
51
52         elif node['type'] == NodeType.TG:
53             cmd = 'ip link set {} {}'.format(interface, state)
54             exec_cmd_no_error(node, cmd, sudo=True)
55         else:
56             raise Exception('Unknown NodeType: "{}"'.format(node['type']))
57
58     @staticmethod
59     def vpp_node_interfaces_ready_wait(node, timeout=10):
60         """Wait until all interfaces with admin-up are in link-up state.
61
62         :param node: Node to wait on.
63         :param timeout: Waiting timeout in seconds (optional, default 10s)
64         :type node: dict
65         :type timeout: int
66         :raises: RuntimeError if the timeout period value has elapsed.
67         """
68         if_ready = False
69         with VatTerminal(node) as vat:
70             not_ready = []
71             start = time()
72             while not if_ready:
73                 out = vat.vat_terminal_exec_cmd('sw_interface_dump')
74                 if time() - start > timeout:
75                     for interface in out:
76                         if interface.get('admin_up_down') == 1:
77                             if interface.get('link_up_down') != 1:
78                                 logger.debug('{0} link-down'.format(
79                                     interface.get('interface_name')))
80                     raise RuntimeError('timeout, not up {0}'.format(not_ready))
81                 not_ready = []
82                 for interface in out:
83                     if interface.get('admin_up_down') == 1:
84                         if interface.get('link_up_down') != 1:
85                             not_ready.append(interface.get('interface_name'))
86                 if not not_ready:
87                     if_ready = True
88                 else:
89                     logger.debug('Interfaces still in link-down state: {0}, '
90                                  'waiting...'.format(not_ready))
91                     sleep(1)
92
93     @staticmethod
94     def vpp_nodes_interfaces_ready_wait(nodes, timeout=10):
95         """Wait until all interfaces with admin-up are in link-up state for
96         listed nodes.
97
98         :param nodes: List of nodes to wait on.
99         :param timeout: Seconds to wait per node for all interfaces to come up.
100         :type nodes: list
101         :type timeout: int
102         :raises: RuntimeError if the timeout period value has elapsed.
103         """
104         for node in nodes:
105             InterfaceUtil.vpp_node_interfaces_ready_wait(node, timeout)
106
107     @staticmethod
108     def all_vpp_interfaces_ready_wait(nodes, timeout=10):
109         """Wait until all interfaces with admin-up are in link-up state for all
110         nodes in the topology.
111
112         :param nodes: Nodes in the topology.
113         :param timeout: Seconds to wait per node for all interfaces to come up.
114         :type nodes: dict
115         :type timeout: int
116         :raises: RuntimeError if the timeout period value has elapsed.
117         """
118         for node in nodes.values():
119             if node['type'] == NodeType.DUT:
120                 InterfaceUtil.vpp_node_interfaces_ready_wait(node, timeout)