cf93a04b2b56fb0f518deef5b0ba099bf854c1ad
[csit.git] / resources / libraries / python / Classify.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
15 from robot.api import logger
16
17 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
18
19
20 class Classify(object):
21     """Classify utilities."""
22
23     @staticmethod
24     def vpp_create_classify_table(node, ip_version, direction):
25         """Create classify table.
26
27         :param node: VPP node to create classify table.
28         :param ip_version: Version of IP protocol.
29         :param direction: Direction of traffic - src/dst.
30         :type node: dict
31         :type ip_version: str
32         :type direction: str
33         :return table_index: Classify table index.
34         :return skip_n: Number of skip vectors.
35         :return match_n: Number of match vectors.
36         :rtype table_index: int
37         :rtype skip_n: int
38         :rtype match_n: int
39         """
40         output = VatExecutor.cmd_from_template(node, "classify_add_table.vat",
41                                                ip_version=ip_version,
42                                                direction=direction)
43
44         if output[0]["retval"] == 0:
45             table_index = output[0]["new_table_index"]
46             skip_n = output[0]["skip_n_vectors"]
47             match_n = output[0]["match_n_vectors"]
48             logger.trace('Classify table with table_index {} created on node {}'
49                          .format(table_index, node['host']))
50         else:
51             raise RuntimeError('Unable to create classify table on node {}'
52                                .format(node['host']))
53
54         return table_index, skip_n, match_n
55
56     @staticmethod
57     def vpp_configure_classify_session(node, acl_method, table_index, skip_n,
58                                        match_n, ip_version, direction, address):
59         """Configuration of classify session.
60
61         :param node: VPP node to setup classify session.
62         :param acl_method: ACL method - deny/permit.
63         :param table_index: Classify table index.
64         :param skip_n: Number of skip vectors based on mask.
65         :param match_n: Number of match vectors based on mask.
66         :param ip_version: Version of IP protocol.
67         :param direction: Direction of traffic - src/dst.
68         :param address: IPv4 or IPv6 address.
69         :type node: dict
70         :type acl_method: str
71         :type table_index: int
72         :type skip_n: int
73         :type match_n: int
74         :type ip_version: str
75         :type direction: str
76         :type address: str
77         """
78         with VatTerminal(node) as vat:
79             vat.vat_terminal_exec_cmd_from_template("classify_add_session.vat",
80                                                     acl_method=acl_method,
81                                                     table_index=table_index,
82                                                     skip_n=skip_n,
83                                                     match_n=match_n,
84                                                     ip_version=ip_version,
85                                                     direction=direction,
86                                                     address=address)