Merge "opnfvresultdb: Add mapping for VPP TCs"
[vswitchperf.git] / src / ovs / dpctl.py
1 # Copyright 2016-2017 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 import logging
19
20 from tools import tasks
21 from conf import settings
22
23 _OVS_LOCAL_DATAPATH = 'ovs-system'
24
25 class DPCtl(object):
26     """remove/show datapaths using ``ovs-dpctl``.
27     """
28     def __init__(self, timeout=10):
29         """Initialise logger.
30
31         :param timeout: Timeout to be used for each command
32
33         :returns: None
34         """
35         self.logger = logging.getLogger(__name__)
36         self.timeout = timeout
37
38     # helpers
39
40     def run_dpctl(self, args, check_error=False):
41         """Run ``ovs-dpctl`` with supplied arguments.
42
43         :param args: Arguments to pass to ``ovs-dpctl``
44         :param check_error: Throw exception on error
45
46         :return: None
47         """
48         cmd = ['sudo', settings.getValue('TOOLS')['ovs-dpctl'],
49                '--timeout',
50                str(self.timeout)] + args
51         return tasks.run_task(
52             cmd, self.logger, 'Running ovs-dpctl ..', check_error)
53
54     # datapath management
55
56     def del_dp(self, dp_name=_OVS_LOCAL_DATAPATH):
57         """Delete local datapath (ovs-dpctl).
58
59         :param br_name: Name of bridge
60
61         :return: None
62         """
63         self.logger.debug('delete datapath ' + dp_name)
64         self.run_dpctl(['del-dp', dp_name])