OSDN Git Service

modify scripts 'vim tabstop'.
[ti2/ti2.git] / linkpair / collect / agent / linuxbridge_agent.py
1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
2 # -*- coding: utf-8 -*-
3 #
4
5 __version__ = '1.1'
6
7 import re
8 from linkpair.device import Device
9 from linkpair.port import Port
10 from linkpair.collect.agent.base_agent import BaseAgent
11
12
13 class LinuxBridgeAgent(BaseAgent):
14     '''
15     Linux bridge Collector Agent
16     '''
17
18     def run(self):
19         self.pick_bridge_configuration()
20
21     def pick_bridge_configuration(self):
22         br_name = ""
23         result = self._runner.exec_cmd("brctl show")
24         for br_line in result:
25             br_line = br_line.rstrip()
26             if self._u.d_push(re.match(r'^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$', br_line)) is not None:
27                 match = self._u.d_pop()
28                 br_name = match.group(1)
29                 port = match.group(4)
30                 if port not in self._port_to_br and br_name != "":
31                     self._port_to_br[port] = br_name
32                     ## for OpenStack quntum...
33                     if self._u.d_push(re.match(r'^qvb(.+)', port)):
34                         quantum_idprefix = self._u.d_pop().group(1)
35                         if "qvo" + quantum_idprefix in self._port_to_br:
36                             self._cu.add_linkpair(
37                                 Device(br_name, Device.BR_TYPE),
38                                 Device(self._port_to_br[
39                                        "qvo" + quantum_idprefix], Device.BR_TYPE),
40                                 Port(port),
41                                 Port("qvo" + quantum_idprefix),
42                                 self._formatter.VETH_FORMAT)
43                     else:
44                         self._cu.add_linkpair(
45                             Device(br_name),
46                             Device("INTERNAL", Device.OS_ROUTE_TYPE),
47                             Port(port),
48                             Port(""))
49     #            else:
50     #                print self._port_to_br.keys()
51     #                if self._u.d_push(re.match(r'^qvo(.+)', port)):
52     #                    continue
53     #                add_linkpair(self.DEFAULT_FORMAT, br_name, self.DEFAULT_TYPE, port, \
54     #                  "INTERNAL", self.OS_ROUTE_TYPE )
55
56             elif self._u.d_push(re.match(r'^\s+(\S+)$', br_line)) is not None:
57                 port = self._u.d_pop().group(1)
58                 if port not in self._port_to_br and br_name != "":
59                     self._port_to_br[port] = br_name
60                     ## for OpenStack quntum...
61                     if self._u.d_push(re.match(r'^qvb(.+)', port)):
62                         quantum_idprefix = self._u.d_pop().group(1)
63                         if "qvo" + quantum_idprefix in self._port_to_br:
64                             self._cu.add_linkpair(
65                                 Device(br_name, Device.BR_TYPE),
66                                 Device(self._port_to_br[
67                                        "qvo" + quantum_idprefix], Device.BR_TYPE),
68                                 Port(port),
69                                 Port("qvo" + quantum_idprefix),
70                                 self._formatter.VETH_FORMAT)
71                     else:
72                         if self._u.d_push(re.match(r'^qvo(.+)', port)):
73                             continue
74                         self._cu.add_linkpair(
75                             Device(br_name),
76                             Device("INTERNAL", Device.OS_ROUTE_TYPE),
77                             Port(port),
78                             Port(""))