065c97cb97347cfa7959e5b74564b8014cf0700a
[csit.git] / resources / libraries / python / L2Util.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 """L2 bridge domain utilities Library."""
15
16 from robot.api.deco import keyword
17 from resources.libraries.python.topology import Topology
18 from resources.libraries.python.VatExecutor import VatExecutor
19
20
21 class L2Util(object):
22     """Utilities for l2 bridge domain configuration"""
23
24     def __init__(self):
25         pass
26
27     @staticmethod
28     def vpp_add_l2fib_entry(node, mac, interface, bd_id):
29         """ Creates a static L2FIB entry on a vpp node.
30
31         :param node: Node to add L2FIB entry on.
32         :param mac: Destination mac address.
33         :param interface: Interface name.
34         :param bd_id: Bridge domain id.
35         :type node: dict
36         :type mac: str
37         :type interface: str
38         :type bd_id: int
39         """
40         sw_if_index = Topology.get_interface_sw_index(node, interface)
41         VatExecutor.cmd_from_template(node, "add_l2_fib_entry.vat",
42                                       mac=mac, bd=bd_id,
43                                       interface=sw_if_index)
44
45     @staticmethod
46     @keyword('Setup l2 bridge domain with id "${bd_id}" flooding "${flood}" '
47              'forwarding "${forward}" learning "${learn}" and arp termination '
48              '"${arp_term}" on vpp node "${node}"')
49     def setup_vpp_l2_bridge_domain(node, bd_id, flood, forward, learn,
50                                    arp_term):
51         """Create a l2 bridge domain on the chosen vpp node
52
53         Executes "bridge_domain_add_del bd_id {bd_id} flood {flood} uu-flood 1
54         forward {forward} learn {learn} arp-term {arp_term}" VAT command on
55         the node.
56         For the moment acts as a placeholder
57         :param node: node where we wish to crate the l2 bridge domain
58         :param bd_id: bridge domain id
59         :param flood: enable flooding
60         :param forward: enable forwarding
61         :param learn: enable mac address learning to fib
62         :param arp_term: enable arp_termination
63         :type node: str
64         :type bd_id: str
65         :type flood: bool
66         :type forward: bool
67         :type learn: bool
68         :type arp_term:bool
69         :return:
70         """
71         pass
72
73     @keyword('Add interface "${interface}" to l2 bridge domain with index '
74              '"${bd_id}" and shg "${shg}" on vpp node "${node}"')
75     def add_interface_to_l2_bd(self, node, interface, bd_id, shg):
76         """Adds interface to l2 bridge domain.
77
78         Executes the "sw_interface_set_l2_bridge {interface1} bd_id {bd_id}
79          shg {shg} enable" VAT command on the given node.
80         For the moment acts as a placeholder
81         :param node: node where we want to execute the command that does this.
82         :param interface:
83         :param bd_id:
84         :param shg:
85         :type node: dict
86         :type interface: str
87         :type bd_id: str
88         :type shg: str
89         :return:
90         """
91         pass
92
93     @staticmethod
94     @keyword('Create dict used in bridge domain template file for node '
95              '"${node}" with links "${link_names}" and bd_id "${bd_id}"')
96     def create_bridge_domain_vat_dict(node, link_names, bd_id):
97         """Creates dictionary that can be used in l2 bridge domain template.
98
99         :param node: node data dictionary
100         :param link_names: list of names of links the bridge domain should be
101         connecting
102         :param bd_id: bridge domain index number
103         :type node: dict
104         :type link_names: list
105         :return: dictionary used to generate l2 bridge domain VAT configuration
106         from template file
107         The resulting dictionary looks like this:
108         'interface1': interface name of first interface
109         'interface2': interface name of second interface
110         'bd_id': bridge domian index
111         """
112         bd_dict = Topology().get_interfaces_by_link_names(node, link_names)
113         bd_dict['bd_id'] = bd_id
114         return bd_dict
115
116     @staticmethod
117     def vpp_add_l2_bridge_domain(node, bd_id, port_1, port_2, learn=True):
118         """Add L2 bridge domain with 2 interfaces to the VPP node.
119
120         :param node: Node to add L2BD on.
121         :param bd_id: Bridge domain ID.
122         :param port_1: First interface name added to L2BD.
123         :param port_2: Second interface name addded to L2BD.
124         :param learn: Enable/disable MAC learn.
125         :type node: dict
126         :type bd_id: int
127         :type interface1: str
128         :type interface2: str
129         :type learn: bool
130         """
131         sw_if_index1 = Topology.get_interface_sw_index(node, port_1)
132         sw_if_index2 = Topology.get_interface_sw_index(node, port_2)
133         VatExecutor.cmd_from_template(node,
134                                       'l2_bridge_domain.vat',
135                                       sw_if_id1=sw_if_index1,
136                                       sw_if_id2=sw_if_index2,
137                                       bd_id=bd_id,
138                                       learn=int(learn))