b39515dceeddba03f7c9a2ffc98506698e985730
[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 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 configuration"""
23
24     @staticmethod
25     def vpp_add_l2fib_entry(node, mac, interface, bd_id):
26         """ Create a static L2FIB entry on a vpp node.
27
28         :param node: Node to add L2FIB entry on.
29         :param mac: Destination mac address.
30         :param interface: Interface name.
31         :param bd_id: Bridge domain id.
32         :type node: dict
33         :type mac: str
34         :type interface: str
35         :type bd_id: int
36         """
37         sw_if_index = Topology.get_interface_sw_index(node, interface)
38         VatExecutor.cmd_from_template(node, "add_l2_fib_entry.vat",
39                                       mac=mac, bd=bd_id,
40                                       interface=sw_if_index)
41
42     @staticmethod
43     def create_l2_bd(node, bd_id, flood=1, uu_flood=1, forward=1, learn=1,
44                      arp_term=0):
45         """Create a l2 bridge domain on the chosen VPP node
46
47         Execute "bridge_domain_add_del bd_id {bd_id} flood {flood} uu-flood 1
48         forward {forward} learn {learn} arp-term {arp_term}" VAT command on
49         the node.
50
51         :param node: node where we wish to crate the l2 bridge domain
52         :param bd_id: bridge domain index number
53         :param flood: enable flooding
54         :param uu_flood: enable uu_flood
55         :param forward: enable forwarding
56         :param learn: enable mac address learning to fib
57         :param arp_term: enable arp_termination
58         :type node: dict
59         :type bd_id: int
60         :type flood: bool
61         :type uu_flood: bool
62         :type forward: bool
63         :type learn: bool
64         :type arp_term:bool
65         """
66         VatExecutor.cmd_from_template(node, "l2_bd_create.vat",
67                                       bd_id=bd_id, flood=flood,
68                                       uu_flood=uu_flood, forward=forward,
69                                       learn=learn, arp_term=arp_term)
70
71     @staticmethod
72     def add_interface_to_l2_bd(node, interface, bd_id, shg=0):
73         """Add a interface to the l2 bridge domain.
74
75         Get SW IF ID and add it to the bridge domain.
76
77         :param node: node where we want to execute the command that does this
78         :param interface: interface name
79         :param bd_id: bridge domain index number to add Interface name to
80         :param shg: split horizon group
81         :type node: dict
82         :type interface: str
83         :type bd_id: int
84         :type shg: int
85         """
86         sw_if_index = Topology.get_interface_sw_index(node, interface)
87         L2Util.add_sw_if_index_to_l2_bd(node, sw_if_index, bd_id, shg)
88
89     @staticmethod
90     def add_sw_if_index_to_l2_bd(node, sw_if_index, bd_id, shg=0):
91         """Add interface with sw_if_index to l2 bridge domain.
92
93         Execute the "sw_interface_set_l2_bridge sw_if_index {sw_if_index}
94         bd_id {bd_id} shg {shg} enable" VAT command on the given node.
95
96         :param node: node where we want to execute the command that does this
97         :param sw_if_index: interface index
98         :param bd_id: bridge domain index number to add SW IF ID to
99         :param shg: split horizon group
100         :type node: dict
101         :type sw_if_index: int
102         :type bd_id: int
103         :type shg: int
104         :return:
105         """
106         VatExecutor.cmd_from_template(node, "l2_bd_add_sw_if_index.vat",
107                                       bd_id=bd_id, sw_if_index=sw_if_index,
108                                       shg=shg)
109
110     @staticmethod
111     @keyword('Create dict used in bridge domain template file for node '
112              '"${node}" with links "${link_names}" and bd_id "${bd_id}"')
113     def create_bridge_domain_vat_dict(node, link_names, bd_id):
114         """Create dictionary that can be used in l2 bridge domain template.
115
116         :param node: node data dictionary
117         :param link_names: list of names of links the bridge domain should be
118         connecting
119         :param bd_id: bridge domain index number
120         :type node: dict
121         :type link_names: list
122         :return: dictionary used to generate l2 bridge domain VAT configuration
123         from template file
124         The resulting dictionary looks like this:
125         'interface1': interface name of first interface
126         'interface2': interface name of second interface
127         'bd_id': bridge domain index
128         """
129         bd_dict = Topology().get_interfaces_by_link_names(node, link_names)
130         bd_dict['bd_id'] = bd_id
131         return bd_dict
132
133     @staticmethod
134     def vpp_add_l2_bridge_domain(node, bd_id, port_1, port_2, learn=True):
135         """Add L2 bridge domain with 2 interfaces to the VPP node.
136
137         :param node: Node to add L2BD on.
138         :param bd_id: Bridge domain ID.
139         :param port_1: First interface name added to L2BD.
140         :param port_2: Second interface name added to L2BD.
141         :param learn: Enable/disable MAC learn.
142         :type node: dict
143         :type bd_id: int
144         :type port_1: str
145         :type port_2: str
146         :type learn: bool
147         """
148         sw_if_index1 = Topology.get_interface_sw_index(node, port_1)
149         sw_if_index2 = Topology.get_interface_sw_index(node, port_2)
150         VatExecutor.cmd_from_template(node,
151                                       'l2_bridge_domain.vat',
152                                       sw_if_id1=sw_if_index1,
153                                       sw_if_id2=sw_if_index2,
154                                       bd_id=bd_id,
155                                       learn=int(learn))