CSIT-203: Expand LISP test
[csit.git] / resources / libraries / python / Map.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 """Map utilities library."""
15
16
17 from resources.libraries.python.VatExecutor import VatExecutor
18
19
20 class Map(object):
21     """Utilities for manipulating MAP feature in VPP."""
22
23     @staticmethod
24     def map_add_domain(vpp_node, ip4_pfx, ip6_pfx, ip6_src, ea_bits_len,
25                        psid_offset, psid_len):
26         """Add map domain on node.
27
28         :param vpp_node: VPP node to add map domain on.
29         :param ip4_pfx: Rule IPv4 prefix.
30         :param ip6_pfx: Rule IPv6 prefix.
31         :param ip6_src: MAP domain IPv6 BR address / Tunnel source.
32         :param ea_bits_len: Embedded Address bits length.
33         :param psid_offset: Port Set Identifier (PSID) offset.
34         :param psid_len: Port Set Identifier (PSID) length.
35         :type vpp_node: dict
36         :type ip4_pfx: str
37         :type ip6_pfx: str
38         :type ip6_src: str
39         :type ea_bits_len: int
40         :type psid_offset: int
41         :type psid_len: int
42         :return: Index of created map domain.
43         :rtype: int
44         :raises RuntimeError: If unable to add map domain.
45         """
46         output = VatExecutor.cmd_from_template(vpp_node, "map_add_domain.vat",
47                                                ip4_pfx=ip4_pfx,
48                                                ip6_pfx=ip6_pfx,
49                                                ip6_src=ip6_src,
50                                                ea_bits_len=ea_bits_len,
51                                                psid_offset=psid_offset,
52                                                psid_len=psid_len)
53         if output[0]["retval"] == 0:
54             return output[0]["index"]
55         else:
56             raise RuntimeError('Unable to add map domain on node {}'
57                                .format(vpp_node['host']))
58
59     @staticmethod
60     def map_add_rule(vpp_node, index, psid, dst, delete=False):
61         """Add or delete map rule on node.
62
63         :param vpp_node: VPP node to add map rule on.
64         :param index: Map domain index to add rule to.
65         :param psid: Port Set Identifier.
66         :param dst: MAP CE IPv6 address.
67         :param delete: If set to True, delete rule. Default False.
68         :type vpp_node: dict
69         :type index: int
70         :type psid: int
71         :type dst: str
72         :type delete: bool
73         :raises RuntimeError: If unable to add map rule.
74         """
75         output = VatExecutor.cmd_from_template(vpp_node, "map_add_del_rule.vat",
76                                                index=index,
77                                                psid=psid,
78                                                dst=dst,
79                                                delete='del' if delete else '')
80
81         if output[0]["retval"] != 0:
82             raise RuntimeError('Unable to add map rule on node {}'
83                                .format(vpp_node['host']))