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(
25 node, interface, ip_version, fib_id, default_cop=0):
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_version: IP version. 'ip4' and 'ip6' are valid values.
31 :param fib_id: Specify the fib table ID.
32 :param default_cop: 1 => enable non-ip4, non-ip6 filtration,
38 :type default_cop: int
39 :raises ValueError: If parameter 'ip_version' has incorrect value.
41 if ip_version not in ('ip4', 'ip6'):
42 raise ValueError('IP version is not in correct format')
44 cmd = 'cop_whitelist_enable_disable'
45 err_msg = 'Failed to add COP whitelist on interface {ifc} on host' \
46 ' {host}'.format(ifc=interface, host=node['host'])
48 sw_if_index=Topology.get_interface_sw_index(node, interface),
50 ip4=True if ip_version == 'ip4' else False,
51 ip6=True if ip_version == 'ip6' else False,
52 default_cop=default_cop
55 with PapiSocketExecutor(node) as papi_exec:
56 papi_exec.add(cmd, **args).get_reply(err_msg)
59 def cop_interface_enable_or_disable(node, interface, state):
60 """Enable or disable COP on the interface.
62 :param node: Node to add COP whitelist on.
63 :param interface: Interface of the node where the COP is added.
64 :param state: Enable or disable COP on the interface.
68 :raises ValueError: If parameter 'state' has incorrect value.
71 if state in ('enable', 'disable'):
72 enable = True if state == 'enable' else False
74 raise ValueError("Possible state values are 'enable' or 'disable'")
76 cmd = 'cop_interface_enable_disable'
77 err_msg = 'Failed to enable/disable COP on interface {ifc} on host' \
78 ' {host}'.format(ifc=interface, host=node['host'])
80 sw_if_index=Topology.get_interface_sw_index(node, interface),
84 with PapiSocketExecutor(node) as papi_exec:
85 papi_exec.add(cmd, **args).get_reply(err_msg)