Add VPP NSIM Plugin Keywords
[csit.git] / resources / libraries / python / NsimUtil.py
1 # Copyright (c) 2019 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 """VPP Network Simulator Plugin util library."""
15
16 from robot.api import logger
17
18 from resources.libraries.python.Constants import Constants
19 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
20 from resources.libraries.python.InterfaceUtil import InterfaceUtil
21
22
23 class NsimUtil(object):
24     """VPP NSIM Plugin Keywords."""
25
26     @staticmethod
27     def configure_vpp_nsim(node, vpp_nsim_attr, interface0, interface1=None):
28         """Configure nsim on the specified VPP node.
29
30         :param node: Topology node.
31         :param vpp_nsim_attr: VPP NSIM configuration attributes
32         :param interface0: Interface name.
33         :param interface1: 2nd Interface name for cross-connect feature
34         :type node: dict
35         :type vpp_nsim_attr: dict
36         :type interface0: string or int
37         :type interface1: string or int
38         :raises RuntimeError: if no NSIM features are enabled or
39                 vpp papi command fails.
40         """
41         host = node[u"host"]
42         if vpp_nsim_attr[u"output_feature_enable"] == False \
43                 and vpp_nsim_attr[u"cross_connect_feature_enable"] == False:
44             raise RuntimeError(f"No NSIM features enabled on host {host}:\n"
45                                f"vpp_nsim_attr = {vpp_nsim_attr}")
46         cmd = u"nsim_configure"
47         args = dict(
48             delay_in_usec=vpp_nsim_attr[u"delay_in_usec"],
49             average_packet_size=vpp_nsim_attr[u"average_packet_size"],
50             bandwidth_in_bits_per_second=vpp_nsim_attr[
51                 u"bandwidth_in_bits_per_second"
52             ],
53             packets_per_drop=vpp_nsim_attr[u"packets_per_drop"],
54         )
55         err_msg = f"Failed to configure NSIM on host {host}"
56         with PapiSocketExecutor(node) as papi_exec:
57             papi_exec.add(cmd, **args).get_reply(err_msg)
58
59         if vpp_nsim_attr[u"output_feature_enable"] == True:
60             cmd = u"nsim_output_feature_enable_disable"
61             args = dict(
62                 enable_disable=vpp_nsim_attr[u"output_feature_enable"],
63                 sw_if_index=InterfaceUtil.get_interface_index(node, interface0),
64             )
65             err_msg = f"Failed to enable NSIM output feature on " \
66                 f"host {host} interface {interface0}"
67             with PapiSocketExecutor(node) as papi_exec:
68                 papi_exec.add(cmd, **args).get_reply(err_msg)
69
70         elif vpp_nsim_attr[u"cross_connect_feature_enable"] == True:
71             cmd = u"nsim_cross_connect_feature_enable_disable"
72             args = dict(
73                 enable_disable=vpp_nsim_attr[u"cross_connect_feature_enable"],
74                 sw_if_index0=InterfaceUtil.get_interface_index(node,
75                                                                interface0),
76                 sw_if_index1=InterfaceUtil.get_interface_index(node,
77                                                                interface1),
78             )
79             err_msg = f"Failed to enable NSIM output feature on " \
80                 f"host {host} interface {interface0}"
81             with PapiSocketExecutor(node) as papi_exec:
82                 papi_exec.add(cmd, **args).get_reply(err_msg)