OSDN Git Service

modify formatter's metadata handling
[ti2/ti2.git] / linkpair / formatter / baseformatter.py
1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
2 # -*- coding: utf-8 -*-
3 #
4
5 __version__ = '1.1'
6
7 import sys
8 import os
9 import re
10 from linkpair.linkpair import LinkPair
11 from linkpair.device import Device
12 from linkpair.port import Port
13
14
15 class BaseFormatter(object):
16     DEFAULT_STYLE = ''
17     VM_STYLE = ''
18     BR_STYLE = ''
19     OS_ROUTE_STYLE = ''
20     PHYNET_STYLE = ''
21     NAMESPACE_STYLE = ''
22     NOT_CONNECTED_STYLE = ''
23
24     DEFAULT_FORMAT = '[ %s ] %s -- { label: "%s   %s"; } [ %s ] %s'
25     OS_ROUTING_FORMAT = '[ %s ] %s -- { label: "%s %s"; } [ %s ] %s'
26     NAMESPACE_FORMAT = '[ %s ] %s --> { label: %s   %s; } [ %s ] %'
27     PATCH_FORMAT = '[ %s ] %s <==> { label: %s  :  %s; } [ %s ] %s'
28     VETH_FORMAT = '[ %s ] %s <--> { label: %s  \|  %s; } [ %s ] %s'
29
30     HEADER_FORMAT = ""
31     FOOTER_FORMAT = ""
32     formatter_name = "base"
33
34     def __init__(self, title=""):
35         self._linkpairs = []
36         self._title = title
37         self._groupid = 1
38
39     @classmethod
40     def format_linkpair(cls, linkpair):
41         formatted_lp = ""
42
43         device1_label = linkpair.device1
44         device2_label = linkpair.device2
45         port1_label = linkpair.port1
46         port2_label = linkpair.port2
47
48         if "remote_ip" in linkpair.port1.metadata:
49             port1_label = port1_label + " remote=" + \
50                 linkpair.port1.metadata["remote_ip"]
51         if "tag" in linkpair.port1.metadata:
52             port1_label = port1_label + " vlan=" + \
53                 linkpair.port1.metadata["tag"]
54         if "vlan_vid" in linkpair.port1.metadata:
55             port1_label = port1_label + " vid=" + \
56                 linkpair.port1.metadata["vlan_vid"]
57         if "vxlan_vnid" in linkpair.port1.metadata:
58             port1_label = port1_label + " vnid=" + \
59                 linkpair.port1.metadata["vxlan_vnid"]
60         if "remote_ip" in linkpair.port2.metadata:
61             port2_label = port2_label + " remote=" + \
62                 linkpair.port2.metadata["remote_ip"]
63         if "tag" in linkpair.port2.metadata:
64             port2_label = port2_label + " vlan=" + \
65                 linkpair.port2.metadata["tag"]
66         if "vlan_vid" in linkpair.port2.metadata:
67             port2_label = port2_label + " vid=" + \
68                 linkpair.port2.metadata["vlan_vid"]
69         if "vxlan_vnid" in linkpair.port2.metadata:
70             port2_label = port2_label + " vnid=" + \
71                 linkpair.port2.metadata["vxlan_vnid"]
72
73         formatted_lp = linkpair.format % (
74             device1_label,
75             cls.get_device_style(linkpair.device1.type),
76             port1_label,
77             port2_label,
78             device2_label,
79             cls.get_device_style(linkpair.device2.type))
80         return formatted_lp
81
82     def format_linkpairs(self):
83         formatted_lps = []
84         formatted_lps.append(self.get_header())
85         print self.get_header()
86         for lp in self._linkpairs:
87             formatted_lps.append(self.format_linkpair(lp))
88         formatted_lps.append(self.get_footer())
89         return formatted_lps
90
91     def get_header(self):
92         header = self.HEADER_FORMAT
93         return header
94
95     def get_footer(self):
96         footer = self.FOOTER_FORMAT
97         return footer
98
99     @classmethod
100     def get_device_style(cls, type=Device.DEFAULT_TYPE):
101         if type == Device.DEFAULT_TYPE:
102             return cls.DEFAULT_STYLE
103         elif type == Device.VM_TYPE:
104             return cls.VM_STYLE
105         elif type == Device.BR_TYPE:
106             return cls.BR_STYLE
107         elif type == Device.OS_ROUTE_TYPE:
108             return cls.OS_ROUTE_STYLE
109         elif type == Device.PHYNET_TYPE:
110             return cls.PHYNET_STYLE
111         elif type == Device.NAMESPACE_TYPE:
112             return cls.NAMESPACE_STYLE
113         elif type == Device.NOT_CONNECTED_TYPE:
114             return cls.NOT_CONNECTED_STYLE
115         else:
116             return cls.DEFAULT_STYLE
117
118     @classmethod
119     def get_port_style(cls, type=Port.DEFAULT_TYPE):
120         return cls.DEFAULT_STYLE
121
122     @classmethod
123     def get_all_devices(cls, lps):
124         devices = []
125         devices_str = []
126         for lp in lps:
127             if str(lp.device1) not in devices_str and str(lp.device1) != "":
128                 devices_str.append(str(lp.device1))
129                 devices.append(lp.device1)
130             if str(lp.device2) not in devices_str and str(lp.device2) != "":
131                 devices_str.append(str(lp.device2))
132                 devices.append(lp.device2)
133         devices = sorted(set(devices), key=devices.index)
134         return devices
135
136     @apply
137     def linkpairs():
138         def get(self):
139             return self._linkpairs
140
141         def set(self, linkpairs):
142             self._linkpairs = linkpairs
143
144     def set_linkpairs(self, linkpairs):
145         self._linkpairs = linkpairs
146
147     @apply
148     def format():
149         def get(self):
150             return self._format
151
152         def set(self, format):
153             self._format = format
154
155     @apply
156     def title():
157         def get(self):
158             return self._title
159
160         def set(self, title):
161             self._title = title
162
163     def __repr__(self):
164         return self.formatter_name