Python3: resources and libraries
[csit.git] / resources / libraries / python / Namespaces.py
1 # Copyright (c) 2019 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 resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
17
18
19 class Namespaces:
20     """Linux namespace utilities."""
21     def __init__(self):
22         self._namespaces = []
23
24     def create_namespace(self, node, namespace_name):
25         """Create namespace and add the name to the list for later clean-up.
26
27         :param node: Where to create namespace.
28         :param namespace_name: Name for namespace.
29         :type node: dict
30         :type namespace_name: str
31         """
32         cmd = f"ip netns add {namespace_name}"
33
34         exec_cmd_no_error(node, cmd, sudo=True)
35         self._namespaces.append(namespace_name)
36
37     @staticmethod
38     def attach_interface_to_namespace(node, namespace, interface):
39         """Attach specific interface to namespace.
40
41         :param node: Node where to execute command.
42         :param namespace: Namespace to execute command on.
43         :param interface: Interface in namespace.
44         :type node: dict
45         :type namespace: str
46         :type interface: str
47         :raises RuntimeError: Interface could not be attached.
48         """
49         cmd = f"ip link set {interface} netns {namespace}"
50
51         ret_code, _, stderr = exec_cmd(node, cmd, timeout=5, sudo=True)
52         if ret_code != 0:
53             raise RuntimeError(f"Could not attach interface, reason:\n{stderr}")
54
55         cmd = f"ip netns exec {namespace} ip link set {interface} up"
56
57         ret_code, _, stderr = exec_cmd(node, cmd, timeout=5, sudo=True)
58         if ret_code != 0:
59             raise RuntimeError(
60                 f"Could not set interface state, reason:\n{stderr}"
61             )
62
63     @staticmethod
64     def create_bridge_for_int_in_namespace(
65             node, namespace, bridge_name, *interfaces):
66         """Setup bridge domain and add interfaces to it.
67
68         :param node: Node where to execute command.
69         :param namespace: Namespace to execute command on.
70         :param bridge_name: Name of the bridge to be created.
71         :param interfaces: List of interfaces to add to the namespace.
72         :type node: dict
73         :type namespace: str
74         :type bridge_name: str
75         :type interfaces: list
76         """
77         cmd = f"ip netns exec {namespace} brctl addbr {bridge_name}"
78         exec_cmd_no_error(node, cmd, sudo=True)
79
80         for interface in interfaces:
81             cmd = f"ip netns exec {namespace} brctl addif {bridge_name} " \
82                 f"{interface}"
83             exec_cmd_no_error(node, cmd, sudo=True)
84
85         cmd = f"ip netns exec {namespace} ip link set dev {bridge_name} up"
86         exec_cmd_no_error(node, cmd, sudo=True)
87
88     def clean_up_namespaces(self, node):
89         """Remove all old namespaces.
90
91         :param node: Node where to execute command.
92         :type node: dict
93         :raises RuntimeError: Namespaces could not be cleaned properly.
94         """
95         for namespace in self._namespaces:
96             print(f"Cleaning namespace {namespace}")
97             cmd = f"ip netns delete {namespace}"
98             ret_code, _, _ = exec_cmd(node, cmd, timeout=5, sudo=True)
99             if ret_code != 0:
100                 raise RuntimeError(u"Could not delete namespace")