]> git.proxmox.com Git - mirror_ifupdown2.git/blob - pkg/iface.py
more fixes + cleanup + support for --exclude argument
[mirror_ifupdown2.git] / pkg / iface.py
1 #!/usr/bin/python
2 #
3 # Copyright 2013. Cumulus Networks, Inc.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
5 #
6 # iface --
7 # interface object
8 #
9
10 import logging
11
12 class ifaceFlags():
13
14 NONE = 0x1
15 FOLLOW_DEPENDENTS = 0x2
16
17 class ifaceStatus():
18
19 """iface status """
20 UNKNOWN = 0x1
21 SUCCESS = 0x2
22 ERROR = 0x3
23
24 @classmethod
25 def to_str(cls, state):
26 if state == cls.UNKNOWN:
27 return 'unknown'
28 elif state == cls.SUCCESS:
29 return 'success'
30 elif state == cls.ERROR:
31 return 'error'
32
33 @classmethod
34 def from_str(cls, state_str):
35 if state_str == 'unknown':
36 return cls.UNKNOWN
37 elif state_str == 'success':
38 return cls.SUCCESS
39 elif state_str == 'error':
40 return cls.ERROR
41
42 class ifaceState():
43
44 """ iface states """
45 UNKNOWN = 0x1
46 NEW = 0x2
47 PRE_UP = 0x3
48 UP = 0x4
49 POST_UP = 0x5
50 PRE_DOWN = 0x6
51 DOWN = 0x7
52 POST_DOWN = 0x8
53
54 @classmethod
55 def to_str(cls, state):
56 if state == cls.UNKNOWN:
57 return 'unknown'
58 elif state == cls.NEW:
59 return 'new'
60 elif state == cls.PRE_UP:
61 return 'pre-up'
62 elif state == cls.UP:
63 return 'up'
64 elif state == cls.POST_UP:
65 return 'post-up'
66 elif state == cls.PRE_DOWN:
67 return 'pre-down'
68 elif state == cls.POST_DOWN:
69 return 'post-down'
70
71 @classmethod
72 def from_str(cls, state_str):
73 if state_str == 'unknown':
74 return cls.UNKNOWN
75 elif state_str == 'new':
76 return cls.NEW
77 elif state_str == 'pre-up':
78 return cls.PRE_UP
79 elif state_str == 'up':
80 return cls.UP
81 elif state_str == 'post-up':
82 return cls.POST_UP
83 elif state_str == 'pre-down':
84 return cls.PRE_DOWN
85 elif state_str == 'post-down':
86 return cls.POST_DOWN
87
88
89 class iface():
90 """ config flags """
91 AUTO = 0x1
92 HOT_PLUG = 0x2
93
94
95 def __init__(self):
96 self.name = None
97 self.addr_family = None
98 self.addr_method = None
99 self.config = {}
100 self.children = []
101 self.state = ifaceState.NEW
102 self.status = ifaceStatus.UNKNOWN
103 self.flags = 0x0
104 self.refcnt = 0
105 self.dependents = None
106 self.auto = False
107 self.classes = []
108 self.env = None
109 self.config_current = {}
110 self.raw_lines = []
111 self.linkstate = None
112
113 def inc_refcnt(self):
114 #print 'inside inc_refcnt = %d' %self.refcnt
115 self.refcnt += 1
116
117 def dec_refcnt(self):
118 self.refcnt -= 1
119
120 def get_refcnt(self):
121 return self.refcnt
122
123 def set_refcnt(self, cnt):
124 self.refcnt = 0
125
126 def set_name(self, name):
127 self.name = name
128
129 def get_name(self):
130 return self.name
131
132 def set_addr_family(self, family):
133 self.addr_family = family
134
135 def get_addr_family(self):
136 return self.addr_family
137
138 def set_addr_method(self, method):
139 self.addr_method = method
140
141 def get_addr_method(self):
142 return self.addr_method
143
144 def set_config(self, config_dict):
145 self.config = config_dict
146
147 def get_config(self):
148 return self.config
149
150 def set_config_current(self, config_current):
151 self.config_current = config_current
152
153 def get_config_current(self):
154 return self.config_current
155
156 def get_auto(self):
157 return self.auto
158
159 def set_auto(self):
160 self.auto = True
161
162 def reset_auto(self):
163 self.auto = False
164
165 def get_classes(self):
166 return self.classes
167
168 def set_classes(self, classes):
169 """ sets interface class list to the one passed as arg """
170 self.classes = classes
171
172 def set_class(self, classname):
173 """ Appends a class to the list """
174 self.classes.append(classname)
175
176 def belongs_to_class(self, intfclass):
177 if intfclass in self.classes:
178 return True
179
180 return False
181
182 def add_child(self, child_iface_obj):
183 self.children.append(child_iface_obj)
184
185 def get_state(self):
186 return self.state
187
188 def get_state_str(self):
189 return ifaceState.to_str(self.state)
190
191 def set_state(self, state):
192 self.state = state
193
194 def get_status(self):
195 return self.status
196
197 def get_status_str(self):
198 return ifaceStatus.to_str(self.status)
199
200 def set_status(self, status):
201 self.status = status
202
203 def state_str_to_hex(self, state_str):
204 return self.state_str_map.get(state_str)
205
206 def set_flag(self, flag):
207 self.flags |= flag
208
209 def clear_flag(self, flag):
210 self.flags &= ~flag
211
212 def set_dependents(self, dlist):
213 self.dependents = dlist
214
215 def get_dependents(self):
216 return self.dependents
217
218 def set_linkstate(self, l):
219 self.linkstate = l
220
221 def get_linkstate(self):
222 return self.linkstate
223
224 def get_attr_value(self, attr_name):
225 config = self.get_config()
226
227 return config.get(attr_name)
228
229 def get_attr_value_first(self, attr_name):
230 config = self.get_config()
231
232 attr_value_list = config.get(attr_name)
233 if attr_value_list is not None:
234 return attr_value_list[0]
235
236 return None
237
238 def get_attr_value_n(self, attr_name, attr_index):
239 config = self.get_config()
240
241 attr_value_list = config.get(attr_name)
242 if attr_value_list is not None:
243 try:
244 return attr_value_list[attr_index]
245 except:
246 return None
247
248 return None
249
250 def get_env(self):
251 if self.env is None or len(self.env) == 0:
252 self.generate_env()
253 return self.env
254
255 def set_env(self, env):
256 self.env = env
257
258 def generate_env(self):
259 env = {}
260 config = self.get_config()
261 env['IFACE'] = self.get_name()
262 for attr, attr_value in config.items():
263 attr_env_name = 'IF_%s' %attr.upper()
264 env[attr_env_name] = attr_value[0]
265
266 if len(env) > 0:
267 self.set_env(env)
268
269 def update_config(self, attr_name, attr_value, attr_status=0):
270 if attr_value is None:
271 attr_value = ''
272
273 if attr_status != 0:
274 self.set_status(ifaceStatus.ERROR)
275 else:
276 if self.get_status() != ifaceStatus.ERROR:
277 self.set_status(ifaceStatus.SUCCESS)
278 if self.config.get(attr_name) is not None:
279 self.config[attr_name].append(attr_value)
280 else:
281 self.config[attr_name] = [attr_value]
282
283 """ XXX: If status needs to be encoded in the query string
284 if attr_status == 0:
285 self.set_status(attr
286 attr_status_str = ''
287 elif attr_status == 0:
288 attr_status_str = ' (success)'
289 elif attr_status != 0:
290 attr_status_str = ' (error)'
291 self.config[attr_name] = attr_value + attr_status_str """
292
293 def dump_raw(self, logger):
294 indent = ' '
295 print (self.raw_lines[0])
296 for i in range(1, len(self.raw_lines)):
297 print (indent + self.raw_lines[i])
298
299 def dump(self, logger):
300 indent = '\t'
301 logger.info(self.get_name() + ' : {')
302 logger.info(indent + 'family: %s' %self.get_addr_family())
303 logger.info(indent + 'method: %s' %self.get_addr_method())
304 logger.info(indent + 'state: %s'
305 %ifaceState.to_str(self.get_state()))
306 logger.info(indent + 'status: %s'
307 %ifaceStatus.to_str(self.get_status()))
308 logger.info(indent + 'refcnt: %d' %self.get_refcnt())
309 d = self.get_dependents()
310 if d is not None:
311 logger.info(indent + 'dependents: %s' %str(d))
312 else:
313 logger.info(indent + 'dependents: None')
314
315 logger.info(indent + 'config: ')
316 config = self.get_config()
317 if config is not None:
318 logger.info(indent + indent + str(config))
319 logger.info('}')
320
321 def dump_pretty(self, logger):
322 indent = '\t'
323 outbuf = 'iface %s' %self.get_name()
324 if self.get_addr_family() is not None:
325 outbuf += ' %s' %self.get_addr_family()
326
327 if self.get_addr_method() is not None:
328 outbuf += ' %s' %self.get_addr_method()
329
330 outbuf += '\n'
331
332 config = self.get_config()
333 if config is not None:
334 for cname, cvaluelist in config.items():
335 for cv in cvaluelist:
336 outbuf += indent + '%s' %cname + ' %s\n' %cv
337
338 #outbuf += ('%s' %indent + '%s' %self.get_state_str() +
339 # ' %s' %self.get_status_str())
340
341 print outbuf