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