feat(profiles): Cleanup IPv6 profiles
[csit.git] / resources / libraries / python / Namespaces.py
1 # Copyright (c) 2021 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 """Linux namespace utilities library."""
15
16 from copy import deepcopy
17
18 from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
19
20
21 class Namespaces:
22     """Linux namespace utilities."""
23     __namespaces = []
24
25     @staticmethod
26     def create_namespace(node, namespace, delete_before_create=True):
27         """Create namespace and add the name to the list for later clean-up.
28
29         :param node: Where to create namespace.
30         :param namespace: Name for namespace.
31         :param delete_before_create: Delete namespace prior to create
32         :type node: dict
33         :type namespace: str
34         :type delete_before_create: bool
35         """
36         if delete_before_create:
37             Namespaces.delete_namespace(node, namespace)
38
39         cmd = f"ip netns add {namespace}"
40         exec_cmd_no_error(node, cmd, sudo=True)
41         Namespaces.__namespaces.append(namespace)
42
43     @staticmethod
44     def delete_namespace(node, namespace):
45         """Delete namespace from the node and list.
46
47         :param node: Where to delete namespace.
48         :param namespace: Name for namespace.
49         :param delete_before_create: Delete namespace prior to create
50         :type node: dict
51         :type namespace: str
52         :type delete_before_create: bool
53         """
54         cmd_timeout = 5
55         cmd = f"ip netns delete {namespace}"
56         (ret_code, _, delete_errmsg) = \
57             exec_cmd(node, cmd, timeout=cmd_timeout, sudo=True)
58         if ret_code != 0:
59             cmd = f"ip netns list {namespace}"
60             (stdout, _) = \
61                 exec_cmd_no_error(node, cmd, timeout=cmd_timeout, sudo=True)
62             if stdout == namespace:
63                 raise RuntimeError(f"Could not delete namespace "
64                                    f"({namespace}): {delete_errmsg}")
65         try:
66             Namespaces.__namespaces.remove(namespace)
67         except ValueError:
68             pass
69
70     @staticmethod
71     def attach_interface_to_namespace(node, namespace, interface):
72         """Attach specific interface to namespace.
73
74         :param node: Node where to execute command.
75         :param namespace: Namespace to execute command on.
76         :param interface: Interface in namespace.
77         :type node: dict
78         :type namespace: str
79         :type interface: str
80         :raises RuntimeError: Interface could not be attached.
81         """
82         cmd = f"ip link set {interface} netns {namespace}"
83
84         ret_code, _, stderr = exec_cmd(node, cmd, timeout=5, sudo=True)
85         if ret_code != 0:
86             raise RuntimeError(f"Could not attach interface, reason:\n{stderr}")
87
88         cmd = f"ip netns exec {namespace} ip link set {interface} up"
89
90         ret_code, _, stderr = exec_cmd(node, cmd, timeout=5, sudo=True)
91         if ret_code != 0:
92             raise RuntimeError(
93                 f"Could not set interface state, reason:\n{stderr}"
94             )
95
96     @staticmethod
97     def add_default_route_to_namespace(node, namespace, default_route):
98         """Add IPv4 default route to interface in namespace.
99
100         :param node: Node where to execute command.
101         :param namespace: Namespace to execute command on.
102         :param default_route: Default route address.
103         :type node: dict
104         :type namespace: str
105         :type default_route: str
106         """
107         cmd = f"ip netns exec {namespace} ip route add default " \
108               f"via {default_route}"
109         exec_cmd_no_error(node, cmd, sudo=True)
110
111     @staticmethod
112     def create_bridge_for_int_in_namespace(
113             node, namespace, bridge_name, *interfaces):
114         """Setup bridge domain and add interfaces to it.
115
116         :param node: Node where to execute command.
117         :param namespace: Namespace to execute command on.
118         :param bridge_name: Name of the bridge to be created.
119         :param interfaces: List of interfaces to add to the namespace.
120         :type node: dict
121         :type namespace: str
122         :type bridge_name: str
123         :type interfaces: list
124         """
125         cmd = f"ip netns exec {namespace} brctl addbr {bridge_name}"
126         exec_cmd_no_error(node, cmd, sudo=True)
127
128         for interface in interfaces:
129             cmd = f"ip netns exec {namespace} brctl addif {bridge_name} " \
130                 f"{interface}"
131             exec_cmd_no_error(node, cmd, sudo=True)
132
133         cmd = f"ip netns exec {namespace} ip link set dev {bridge_name} up"
134         exec_cmd_no_error(node, cmd, sudo=True)
135
136     @staticmethod
137     def clean_up_namespaces(node, namespace=None):
138         """Delete all old namespaces.
139
140         :param node: Node where to execute command.
141         :param namespace: Namespace to delete, if None delete all namespaces
142         :type node: dict
143         :type namespace: str
144         :raises RuntimeError: Namespaces could not be cleaned properly.
145         """
146         if namespace is not None:
147             Namespaces.delete_namespace(node, namespace)
148             return
149
150         namespace_copy = deepcopy(Namespaces.__namespaces)
151         for namespace_name in namespace_copy:
152             Namespaces.delete_namespace(node, namespace_name)