8ecac6dc25371f4f4693561a4588a9b780b314e2
[vswitchperf.git] / src / ovs / dpctl.py
1 # Copyright 2016 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Wrapper for an OVS dpctl (``ovs-dpctl``) for managing datapaths.
16
17 """
18
19 import os
20 import logging
21 import string
22
23 from tools import tasks
24 from conf import settings
25
26 _OVS_DPCTL_BIN = os.path.join(settings.getValue('OVS_DIR'), 'utilities',
27                               'ovs-dpctl')
28
29 _OVS_LOCAL_DATAPATH = 'ovs-system'
30
31 class DPCtl(object):
32     """remove/show datapaths using ``ovs-dpctl``.
33     """
34     def __init__(self, timeout=10):
35         """Initialise logger.
36
37         :param timeout: Timeout to be used for each command
38
39         :returns: None
40         """
41         self.logger = logging.getLogger(__name__)
42         self.timeout = timeout
43
44     # helpers
45
46     def run_dpctl(self, args, check_error=False):
47         """Run ``ovs-dpctl`` with supplied arguments.
48
49         :param args: Arguments to pass to ``ovs-dpctl``
50         :param check_error: Throw exception on error
51
52         :return: None
53         """
54         cmd = ['sudo', _OVS_DPCTL_BIN,
55                '--timeout',
56                str(self.timeout)] + args
57         return tasks.run_task(
58             cmd, self.logger, 'Running ovs-dpctl ..', check_error)
59
60     # datapath management
61
62     def del_dp(self, dp_name=_OVS_LOCAL_DATAPATH):
63         """Delete local datapath (ovs-dpctl).
64
65         :param br_name: Name of bridge
66
67         :return: None
68         """
69         self.logger.debug('delete datapath ' + dp_name)
70         self.run_dpctl(['del-dp', dp_name])
71