JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / agent / env / vswitch_plugins / bridge_plugin.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 from vstf.agent.env.vswitch_plugins import model
11 from vstf.common.utils import check_call, get_eth_by_bdf, check_output
12
13
14 class BridgePlugin(model.VswitchPlugin):
15     def __init__(self):
16         pass
17
18     def clean(self):
19         """clean brs created before.
20
21         """
22         out = check_output(r"brctl show | grep -v '^\s' | awk '{print $1}'|sed '1,1d'", shell=True)
23         print out
24         for br in out.split():
25             if br != 'br0':
26                 self._del_br(br)
27
28         return True
29
30     def init(self):
31         pass
32
33     def _del_br(self, name):
34         check_call('ip link set dev %s down' % name, shell=True)
35         check_call('brctl delbr %s' % name, shell=True)
36
37     def create_br(self, br_cfg):
38         """Create a bridge(virtual switch). Return True for success, return False for failure.
39
40         :param dict    br_cfg: configuration for bridge creation like
41                 {
42                     "name": "br1",
43                     "uplinks": [
44                         {
45                             "bdf": "04:00.0",
46                         },
47                         {
48                             "bdf": "04:00.1",
49                         }
50                     ]
51                 }
52
53         """
54         name, uplinks = br_cfg['name'], br_cfg['uplinks']
55         check_call("brctl addbr %s" % name, shell=True)
56         for uplink in uplinks:
57             device = get_eth_by_bdf(uplink['bdf'])
58             check_call("ip link set dev %s up" % device, shell=True)
59             check_call("brctl addif %s %s" % (name, device), shell=True)
60         check_call("ip link set dev %s up" % name, shell=True)
61         return True
62
63     def set_tap_vid(self, tap_cfg):
64         """linux bridge doesn't support vlan id setting.
65         """
66         return True
67
68     def set_fastlink(self, br_cfg):
69         """linux bridge doesn't support openflow protocol.
70         """
71         return True