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:
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 """COP utilities library."""
16 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
17 from resources.libraries.python.topology import Topology
24 def cop_add_whitelist_entry(node, interface, ip_format, fib_id,
26 """Add cop whitelisted entry.
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.
37 :type default_cop: int
39 if ip_format not in ('ip4', 'ip6'):
40 raise ValueError("Ip not in correct format!")
42 if ip_format == 'ip4':
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)
51 sw_if_index=int(sw_if_index),
55 default_cop=default_cop
58 with PapiSocketExecutor(node) as papi_exec:
59 papi_exec.add(cmd, **args_in).get_reply(err_msg)
62 def cop_interface_enable_or_disable(node, interface, state):
63 """Enable or disable COP on the interface.
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.
73 if state in ('enable', 'disable'):
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)
84 sw_if_index=int(sw_if_index),
85 enable_disable=enable_disable
88 with PapiSocketExecutor(node) as papi_exec:
89 papi_exec.add(cmd, **args_in).get_reply(err_msg)
93 "Possible values are 'enable' or 'disable'!"