d875323b9148a2ed392f60cc41bca6073ff5e57f
[csit.git] / resources / libraries / python / Cop.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 """COP utilities library."""
15
16 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
17 from resources.libraries.python.topology import Topology
18
19
20 class Cop(object):
21     """COP utilities."""
22
23     @staticmethod
24     def cop_add_whitelist_entry(node, interface, ip_format, fib_id,
25                                 default_cop=0):
26         """Add cop whitelisted entry.
27
28         :param node: Node to add COP whitelist on.
29         :param interface: Interface of the node where the COP is added.
30         :param ip_format: IP format : ip4 or ip6 are valid formats.
31         :param fib_id: Specify the fib table ID.
32         :param default_cop: 1 => enable non-ip4, non-ip6 filtration.
33         :type node: dict
34         :type interface: str
35         :type ip_format: str
36         :type fib_id: int
37         :type default_cop: int
38         """
39         if ip_format not in ('ip4', 'ip6'):
40             raise ValueError("Ip not in correct format!")
41         ip4 = ip6 = 0
42         if ip_format == 'ip4':
43             ip4 = 1
44         else:
45             ip6 = 1
46         sw_if_index = Topology.get_interface_sw_index(node, interface)
47         cmd = 'cop_whitelist_enable_disable'
48         err_msg = 'Failed to add COP whitelist on ifc {ifc}'\
49                   .format(ifc=interface)
50         args_in = dict(
51             sw_if_index=int(sw_if_index),
52             fib_id=int(fib_id),
53             ip4=ip4,
54             ip6=ip6,
55             default_cop=default_cop
56         )
57
58         with PapiSocketExecutor(node) as papi_exec:
59             papi_exec.add(cmd, **args_in).get_reply(err_msg)
60
61     @staticmethod
62     def cop_interface_enable_or_disable(node, interface, state):
63         """Enable or disable COP on the interface.
64
65         :param node: Node to add COP whitelist on.
66         :param interface: Interface of the node where the COP is added.
67         :param state: disable/enable COP on the interface.
68         :type node: dict
69         :type interface: str
70         :type state: str
71         """
72         state = state.lower()
73         if state in ('enable', 'disable'):
74             if state == 'enable':
75                 enable_disable = 1
76             else:
77                 enable_disable = 0
78             sw_if_index = Topology.get_interface_sw_index(node, interface)
79             cmd = 'cop_interface_enable_disable'
80             err_msg = 'Failed to enable or disable on {ifc}'\
81                       .format(ifc=interface)
82
83             args_in = dict(
84                 sw_if_index=int(sw_if_index),
85                 enable_disable=enable_disable
86             )
87
88             with PapiSocketExecutor(node) as papi_exec:
89                 papi_exec.add(cmd, **args_in).get_reply(err_msg)
90
91         else:
92             raise ValueError(
93                 "Possible values are 'enable' or 'disable'!"
94             )