]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - pkg/iface.py
3 # Copyright 2013. Cumulus Networks, Inc.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
9 from collections
import OrderedDict
14 tickmark
= ' (' + u
'\u2713'.encode('utf8') + ')'
15 crossmark
= ' (' + u
'\u2717'.encode('utf8') + ')'
19 FOLLOW_DEPENDENTS
= 0x2
29 def to_str(cls
, state
):
30 if state
== cls
.UNKNOWN
:
32 elif state
== cls
.SUCCESS
:
34 elif state
== cls
.ERROR
:
36 elif state
== cls
.NOTFOUND
:
40 def from_str(cls
, state_str
):
41 if state_str
== 'unknown':
43 elif state_str
== 'success':
45 elif state_str
== 'error':
65 def to_str(cls
, state
):
66 if state
== cls
.UNKNOWN
:
68 elif state
== cls
.NEW
:
70 elif state
== cls
.PRE_UP
:
74 elif state
== cls
.POST_UP
:
76 elif state
== cls
.PRE_DOWN
:
78 elif state
== cls
.DOWN
:
80 elif state
== cls
.POST_DOWN
:
82 elif state
== cls
.QUERY_CHECKCURR
:
83 return 'query-checkcurr'
84 elif state
== cls
.QUERY_RUNNING
:
85 return 'query-running'
88 def from_str(cls
, state_str
):
89 if state_str
== 'unknown':
91 elif state_str
== 'new':
93 elif state_str
== 'pre-up':
95 elif state_str
== 'up':
97 elif state_str
== 'post-up':
99 elif state_str
== 'pre-down':
101 elif state_str
== 'down':
103 elif state_str
== 'post-down':
105 elif state_str
== 'query-checkcurr':
106 return cls
.QUERY_CHECKCURR
107 elif state_str
== 'query-running':
108 return cls
.QUERY_RUNNING
110 class ifaceJsonEncoder(json
.JSONEncoder
):
111 def default(self
, o
):
114 for k
, v
in o
.config
.items():
120 return OrderedDict({'name' : o
.name
,
121 'addr_method' : o
.addr_method
,
122 'addr_family' : o
.addr_family
,
124 'config' : retconfig
})
128 # flag to indicate that the object was created from pickled state
135 self
.addr_family
= None
136 self
.addr_method
= None
137 self
.config
= OrderedDict()
138 self
.config_status
= {}
139 self
.state
= ifaceState
.NEW
140 self
.status
= ifaceStatus
.UNKNOWN
142 self
.priv_flags
= 0x0
144 self
.lowerifaces
= None
145 self
.upperifaces
= None
150 self
.linkstate
= None
152 def inc_refcnt(self
):
155 def dec_refcnt(self
):
158 def get_refcnt(self
):
161 def set_refcnt(self
, cnt
):
164 def set_name(self
, name
):
170 def set_addr_family(self
, family
):
171 self
.addr_family
= family
173 def get_addr_family(self
):
174 return self
.addr_family
176 def set_addr_method(self
, method
):
177 self
.addr_method
= method
179 def get_addr_method(self
):
180 return self
.addr_method
182 def set_config(self
, config_dict
):
183 self
.config
= config_dict
185 def get_config(self
):
188 def is_config_present(self
):
189 addr_method
= self
.get_addr_method()
191 if (addr_method
.find('dhcp') != -1 or
192 addr_method
.find('dhcp6') != -1):
205 def reset_auto(self
):
208 def get_classes(self
):
211 def set_classes(self
, classes
):
212 """ sets interface class list to the one passed as arg """
213 self
.classes
= classes
215 def set_class(self
, classname
):
216 """ Appends a class to the list """
217 self
.classes
.append(classname
)
219 def belongs_to_class(self
, intfclass
):
220 if intfclass
in self
.classes
:
224 def set_priv_flags(self
, priv_flags
):
225 self
.priv_flags
= priv_flags
227 def get_priv_flags(self
):
228 return self
.priv_flags
233 def get_state_str(self
):
234 return ifaceState
.to_str(self
.state
)
236 def set_state(self
, state
):
239 def get_status(self
):
242 def get_status_str(self
):
243 return ifaceStatus
.to_str(self
.status
)
245 def set_status(self
, status
):
248 def set_state_n_status(self
, state
, status
):
252 def state_str_to_hex(self
, state_str
):
253 return self
.state_str_map
.get(state_str
)
255 def set_flag(self
, flag
):
258 def clear_flag(self
, flag
):
261 def set_lowerifaces(self
, dlist
):
262 self
.lowerifaces
= dlist
264 def get_lowerifaces(self
):
265 return self
.lowerifaces
267 def set_upperifaces(self
, dlist
):
268 self
.upperifaces
= dlist
270 def add_to_upperifaces(self
, upperifacename
):
272 if upperifacename
not in self
.upperifaces
:
273 self
.upperifaces
.append(upperifacename
)
275 self
.upperifaces
= [upperifacename
]
277 def get_upperifaces(self
):
278 return self
.upperifaces
280 def set_linkstate(self
, l
):
283 def get_linkstate(self
):
284 return self
.linkstate
286 def get_attr_value(self
, attr_name
):
287 config
= self
.get_config()
289 return config
.get(attr_name
)
291 def get_attr_value_first(self
, attr_name
):
292 config
= self
.get_config()
293 attr_value_list
= config
.get(attr_name
)
295 return attr_value_list
[0]
298 def get_attr_value_n(self
, attr_name
, attr_index
):
299 config
= self
.get_config()
301 attr_value_list
= config
.get(attr_name
)
304 return attr_value_list
[attr_index
]
314 def set_env(self
, env
):
317 def generate_env(self
):
319 config
= self
.get_config()
320 env
['IFACE'] = self
.get_name()
321 for attr
, attr_value
in config
.items():
322 attr_env_name
= 'IF_%s' %attr
.upper()
323 env
[attr_env_name
] = attr_value
[0]
328 def update_config(self
, attr_name
, attr_value
):
329 if not self
.config
.get(attr_name
):
330 self
.config
[attr_name
] = [attr_value
]
332 self
.config
[attr_name
].append(attr_value
)
334 def update_config_dict(self
, attrdict
):
335 self
.config
.update(attrdict
)
337 def update_config_with_status(self
, attr_name
, attr_value
, attr_status
=0):
341 if self
.config
.get(attr_name
):
342 self
.config
[attr_name
].append(attr_value
)
343 self
.config_status
[attr_name
].append(attr_status
)
345 self
.config
[attr_name
] = [attr_value
]
346 self
.config_status
[attr_name
] = [attr_status
]
348 # set global iface state
350 self
.set_status(ifaceStatus
.ERROR
)
351 elif self
.get_status() != ifaceStatus
.ERROR
:
352 # Not already error, mark success
353 self
.set_status(ifaceStatus
.SUCCESS
)
355 def get_config_attr_status(self
, attr_name
, idx
=0):
356 self
.config_status
.get(attr_name
, [])[idx
]
358 def get_config_attr_status_str(self
, attr_name
, idx
=0):
359 ret
= self
.config_status
.get(attr_name
, [])[idx
]
365 def is_different(self
, dstiface
):
366 if self
.name
!= dstiface
.name
: return True
367 if self
.addr_family
!= dstiface
.addr_family
: return True
368 if self
.addr_method
!= dstiface
.addr_method
: return True
369 if self
.auto
!= dstiface
.auto
: return True
370 if self
.classes
!= dstiface
.classes
: return True
372 if any(True for k
in self
.config
if k
not in dstiface
.config
):
375 if any(True for k
,v
in self
.config
.items()
376 if v
!= dstiface
.config
.get(k
)): return True
380 def __getstate__(self
):
381 odict
= self
.__dict
__.copy()
384 del odict
['lowerifaces']
385 del odict
['upperifaces']
387 del odict
['config_status']
389 del odict
['priv_flags']
390 del odict
['raw_lines']
391 del odict
['linkstate']
395 def __setstate__(self
, dict):
396 self
.__dict
__.update(dict)
397 self
.config_status
= {}
398 self
.state
= ifaceState
.NEW
399 self
.status
= ifaceStatus
.UNKNOWN
402 self
.lowerifaces
= None
403 self
.upperifaces
= None
404 self
.linkstate
= None
408 self
.flags |
= self
.PICKLED
410 def dump_raw(self
, logger
):
412 print (self
.raw_lines
[0])
413 for i
in range(1, len(self
.raw_lines
)):
414 print (indent
+ self
.raw_lines
[i
])
416 def dump(self
, logger
):
418 logger
.info(self
.get_name() + ' : {')
419 logger
.info(indent
+ 'family: %s' %self
.get_addr_family())
420 logger
.info(indent
+ 'method: %s' %self
.get_addr_method())
421 logger
.info(indent
+ 'flags: %x' %self
.flags
)
422 logger
.info(indent
+ 'state: %s'
423 %ifaceState
.to_str(self
.get_state()))
424 logger
.info(indent
+ 'status: %s'
425 %ifaceStatus
.to_str(self
.get_status()))
426 logger
.info(indent
+ 'refcnt: %d' %self
.get_refcnt())
427 d
= self
.get_lowerifaces()
429 logger
.info(indent
+ 'lowerdevs: %s' %str
(d
))
431 logger
.info(indent
+ 'lowerdevs: None')
433 logger
.info(indent
+ 'config: ')
434 config
= self
.get_config()
436 logger
.info(indent
+ indent
+ str(config
))
439 def dump_pretty(self
, with_status
=False):
443 outbuf
+= 'auto %s\n' %self
.get_name()
444 outbuf
+= 'iface %s' %self
.get_name()
445 if self
.get_addr_family():
446 outbuf
+= ' %s' %self
.get_addr_family()
447 if self
.get_addr_method():
448 outbuf
+= ' %s' %self
.get_addr_method()
450 config
= self
.get_config()
452 for cname
, cvaluelist
in config
.items():
454 for cv
in cvaluelist
:
456 outbuf
+= indent
+ '%s %s %s\n' %(cname
, cv
,
457 self
.get_config_attr_status_str(cname
, idx
))
459 outbuf
+= indent
+ '%s %s\n' %(cname
, cv
)
464 def dump_json(self
, with_status
=False):
465 print json
.dumps(self
, cls
=ifaceJsonEncoder
, indent
=4)