]>
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
111 class ifaceJsonEncoder(json
.JSONEncoder
):
112 def default(self
, o
):
115 for k
, v
in o
.config
.items():
121 return OrderedDict({'name' : o
.name
,
122 'addr_method' : o
.addr_method
,
123 'addr_family' : o
.addr_family
,
125 'config' : retconfig
})
129 # flag to indicate that the object was created from pickled state
136 self
.addr_family
= None
137 self
.addr_method
= None
138 self
.config
= OrderedDict()
139 self
.config_status
= {}
140 self
.state
= ifaceState
.NEW
141 self
.status
= ifaceStatus
.UNKNOWN
144 self
.priv_flags
= 0x0
146 self
.lowerifaces
= None
147 self
.upperifaces
= None
151 self
.config_current
= {}
153 self
.linkstate
= None
155 def inc_refcnt(self
):
158 def dec_refcnt(self
):
161 def get_refcnt(self
):
164 def set_refcnt(self
, cnt
):
167 def set_name(self
, name
):
173 def set_addr_family(self
, family
):
174 self
.addr_family
= family
176 def get_addr_family(self
):
177 return self
.addr_family
179 def set_addr_method(self
, method
):
180 self
.addr_method
= method
182 def get_addr_method(self
):
183 return self
.addr_method
185 def set_config(self
, config_dict
):
186 self
.config
= config_dict
188 def get_config(self
):
191 def is_config_present(self
):
192 addr_method
= self
.get_addr_method()
194 if (addr_method
.find('dhcp') != -1 or
195 addr_method
.find('dhcp6') != -1):
202 def set_config_current(self
, config_current
):
203 self
.config_current
= config_current
205 def get_config_current(self
):
206 return self
.config_current
214 def reset_auto(self
):
217 def get_classes(self
):
220 def set_classes(self
, classes
):
221 """ sets interface class list to the one passed as arg """
222 self
.classes
= classes
224 def set_class(self
, classname
):
225 """ Appends a class to the list """
226 self
.classes
.append(classname
)
228 def belongs_to_class(self
, intfclass
):
229 if intfclass
in self
.classes
:
234 def set_priv_flags(self
, priv_flags
):
235 self
.priv_flags
= priv_flags
237 def get_priv_flags(self
):
238 return self
.priv_flags
243 def get_state_str(self
):
244 return ifaceState
.to_str(self
.state
)
246 def set_state(self
, state
):
249 def get_status(self
):
252 def get_status_str(self
):
253 return ifaceStatus
.to_str(self
.status
)
255 def set_status(self
, status
):
258 def set_state_n_status(self
, state
, status
):
262 def state_str_to_hex(self
, state_str
):
263 return self
.state_str_map
.get(state_str
)
265 def set_flag(self
, flag
):
268 def clear_flag(self
, flag
):
271 def set_lowerifaces(self
, dlist
):
272 self
.lowerifaces
= dlist
274 def get_lowerifaces(self
):
275 return self
.lowerifaces
277 def set_upperifaces(self
, dlist
):
278 self
.upperifaces
= dlist
280 def add_to_upperifaces(self
, upperifacename
):
282 if upperifacename
not in self
.upperifaces
:
283 self
.upperifaces
.append(upperifacename
)
285 self
.upperifaces
= [upperifacename
]
287 def get_upperifaces(self
):
288 return self
.upperifaces
290 def set_linkstate(self
, l
):
293 def get_linkstate(self
):
294 return self
.linkstate
296 def get_attr_value(self
, attr_name
):
297 config
= self
.get_config()
299 return config
.get(attr_name
)
301 def get_attr_value_first(self
, attr_name
):
302 config
= self
.get_config()
303 attr_value_list
= config
.get(attr_name
)
305 return attr_value_list
[0]
308 def get_attr_value_n(self
, attr_name
, attr_index
):
309 config
= self
.get_config()
311 attr_value_list
= config
.get(attr_name
)
314 return attr_value_list
[attr_index
]
324 def set_env(self
, env
):
327 def generate_env(self
):
329 config
= self
.get_config()
330 env
['IFACE'] = self
.get_name()
331 for attr
, attr_value
in config
.items():
332 attr_env_name
= 'IF_%s' %attr
.upper()
333 env
[attr_env_name
] = attr_value
[0]
338 def update_config(self
, attr_name
, attr_value
):
339 if not self
.config
.get(attr_name
):
340 self
.config
[attr_name
] = [attr_value
]
342 self
.config
[attr_name
].append(attr_value
)
344 def update_config_dict(self
, attrdict
):
345 self
.config
.update(attrdict
)
347 def update_config_with_status(self
, attr_name
, attr_value
, attr_status
=0):
351 if self
.config
.get(attr_name
):
352 self
.config
[attr_name
].append(attr_value
)
353 self
.config_status
[attr_name
].append(attr_status
)
355 self
.config
[attr_name
] = [attr_value
]
356 self
.config_status
[attr_name
] = [attr_status
]
358 # set global iface state
360 self
.set_status(ifaceStatus
.ERROR
)
361 elif self
.get_status() != ifaceStatus
.ERROR
:
362 # Not already error, mark success
363 self
.set_status(ifaceStatus
.SUCCESS
)
365 def get_config_attr_status(self
, attr_name
, idx
=0):
366 self
.config_status
.get(attr_name
, [])[idx
]
368 def get_config_attr_status_str(self
, attr_name
, idx
=0):
369 ret
= self
.config_status
.get(attr_name
, [])[idx
]
375 def is_different(self
, dstiface
):
376 if self
.name
!= dstiface
.name
: return True
377 if self
.addr_family
!= dstiface
.addr_family
: return True
378 if self
.addr_method
!= dstiface
.addr_method
: return True
379 if self
.auto
!= dstiface
.auto
: return True
380 if self
.classes
!= dstiface
.classes
: return True
382 if any(True for k
in self
.config
if k
not in dstiface
.config
):
385 if any(True for k
,v
in self
.config
.items()
386 if v
!= dstiface
.config
.get(k
)): return True
390 def __getstate__(self
):
391 odict
= self
.__dict
__.copy()
394 del odict
['lowerifaces']
398 def __setstate__(self
, dict):
399 self
.__dict
__.update(dict)
400 self
.state
= ifaceState
.NEW
401 self
.status
= ifaceStatus
.UNKNOWN
403 self
.lowerifaces
= None
404 self
.linkstate
= None
405 self
.flags |
= self
.PICKLED
407 def dump_raw(self
, logger
):
409 print (self
.raw_lines
[0])
410 for i
in range(1, len(self
.raw_lines
)):
411 print (indent
+ self
.raw_lines
[i
])
413 def dump(self
, logger
):
415 logger
.info(self
.get_name() + ' : {')
416 logger
.info(indent
+ 'family: %s' %self
.get_addr_family())
417 logger
.info(indent
+ 'method: %s' %self
.get_addr_method())
418 logger
.info(indent
+ 'flags: %x' %self
.flags
)
419 logger
.info(indent
+ 'state: %s'
420 %ifaceState
.to_str(self
.get_state()))
421 logger
.info(indent
+ 'status: %s'
422 %ifaceStatus
.to_str(self
.get_status()))
423 logger
.info(indent
+ 'refcnt: %d' %self
.get_refcnt())
424 d
= self
.get_lowerifaces()
426 logger
.info(indent
+ 'lowerdevs: %s' %str
(d
))
428 logger
.info(indent
+ 'lowerdevs: None')
430 logger
.info(indent
+ 'config: ')
431 config
= self
.get_config()
433 logger
.info(indent
+ indent
+ str(config
))
436 def dump_pretty(self
, with_status
=False):
440 outbuf
+= 'auto %s\n' %self
.get_name()
441 outbuf
+= 'iface %s' %self
.get_name()
442 if self
.get_addr_family():
443 outbuf
+= ' %s' %self
.get_addr_family()
444 if self
.get_addr_method():
445 outbuf
+= ' %s' %self
.get_addr_method()
447 config
= self
.get_config()
449 for cname
, cvaluelist
in config
.items():
451 for cv
in cvaluelist
:
453 outbuf
+= indent
+ '%s %s %s\n' %(cname
, cv
,
454 self
.get_config_attr_status_str(cname
, idx
))
456 outbuf
+= indent
+ '%s %s\n' %(cname
, cv
)
461 def dump_json(self
, with_status
=False):
462 print json
.dumps(self
, cls
=ifaceJsonEncoder
, indent
=4)