CSIT-748 vnf-agent integration
[csit.git] / resources / libraries / python / Cop.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 """COP utilities library."""
15
16 from resources.libraries.python.VatExecutor import VatTerminal
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         """Add cop whitelisted entry.
26
27         :param node: Node to add COP whitelist on.
28         :param interface: Interface of the node where the COP is added.
29         :param ip_format: IP format : ip4 or ip6 are valid formats.
30         :param fib_id: Specify the fib table ID.
31         :type node: dict
32         :type interface: str
33         :type ip_format: str
34         :type fib_id: int
35         """
36         if ip_format not in ('ip4', 'ip6'):
37             raise ValueError("Ip not in correct format!")
38         sw_if_index = Topology.get_interface_sw_index(node, interface)
39         with VatTerminal(node) as vat:
40             vat.vat_terminal_exec_cmd_from_template('cop_whitelist.vat',
41                                                     sw_if_index=sw_if_index,
42                                                     ip=ip_format,
43                                                     fib_id=fib_id)
44
45     @staticmethod
46     def cop_interface_enable_or_disable(node, interface, state):
47         """Enable or disable COP on the interface.
48
49         :param node: Node to add COP whitelist on.
50         :param interface: Interface of the node where the COP is added.
51         :param state: disable/enable COP on the interface.
52         :type node: dict
53         :type interface: str
54         :type state: str
55         """
56         state = state.lower()
57         if state in ('enable', 'disable'):
58             if state == 'enable':
59                 state = ''
60             sw_if_index = Topology.get_interface_sw_index(node, interface)
61             with VatTerminal(node) as vat:
62                 vat.vat_terminal_exec_cmd_from_template('cop_interface.vat',
63                                                         sw_if_index=sw_if_index,
64                                                         state=state)
65         else:
66             raise ValueError(
67                 "Possible values are 'enable' or 'disable'!"
68             )