]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - pkg/iface.py
3 # Copyright 2013. Cumulus Networks, Inc.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
15 FOLLOW_DEPENDENTS
= 0x2
25 def to_str(cls
, state
):
26 if state
== cls
.UNKNOWN
:
28 elif state
== cls
.SUCCESS
:
30 elif state
== cls
.ERROR
:
34 def from_str(cls
, state_str
):
35 if state_str
== 'unknown':
37 elif state_str
== 'success':
39 elif state_str
== 'error':
55 def to_str(cls
, state
):
56 if state
== cls
.UNKNOWN
:
58 elif state
== cls
.NEW
:
60 elif state
== cls
.PRE_UP
:
64 elif state
== cls
.POST_UP
:
66 elif state
== cls
.PRE_DOWN
:
68 elif state
== cls
.POST_DOWN
:
72 def from_str(cls
, state_str
):
73 if state_str
== 'unknown':
75 elif state_str
== 'new':
77 elif state_str
== 'pre-up':
79 elif state_str
== 'up':
81 elif state_str
== 'post-up':
83 elif state_str
== 'pre-down':
85 elif state_str
== 'post-down':
97 self
.addr_family
= None
98 self
.addr_method
= None
101 self
.state
= ifaceState
.NEW
102 self
.status
= ifaceStatus
.UNKNOWN
105 self
.dependents
= None
109 self
.config_current
= {}
111 self
.linkstate
= None
113 def inc_refcnt(self
):
114 #print 'inside inc_refcnt = %d' %self.refcnt
117 def dec_refcnt(self
):
120 def get_refcnt(self
):
123 def set_refcnt(self
, cnt
):
126 def set_name(self
, name
):
132 def set_addr_family(self
, family
):
133 self
.addr_family
= family
135 def get_addr_family(self
):
136 return self
.addr_family
138 def set_addr_method(self
, method
):
139 self
.addr_method
= method
141 def get_addr_method(self
):
142 return self
.addr_method
144 def set_config(self
, config_dict
):
145 self
.config
= config_dict
147 def get_config(self
):
150 def set_config_current(self
, config_current
):
151 self
.config_current
= config_current
153 def get_config_current(self
):
154 return self
.config_current
162 def reset_auto(self
):
165 def get_classes(self
):
168 def set_classes(self
, classes
):
169 """ sets interface class list to the one passed as arg """
170 self
.classes
= classes
172 def set_class(self
, classname
):
173 """ Appends a class to the list """
174 self
.classes
.append(classname
)
176 def belongs_to_class(self
, intfclass
):
177 if intfclass
in self
.classes
:
182 def add_child(self
, child_iface_obj
):
183 self
.children
.append(child_iface_obj
)
188 def get_state_str(self
):
189 return ifaceState
.to_str(self
.state
)
191 def set_state(self
, state
):
194 def get_status(self
):
197 def get_status_str(self
):
198 return ifaceStatus
.to_str(self
.status
)
200 def set_status(self
, status
):
203 def state_str_to_hex(self
, state_str
):
204 return self
.state_str_map
.get(state_str
)
206 def set_flag(self
, flag
):
209 def clear_flag(self
, flag
):
212 def set_dependents(self
, dlist
):
213 self
.dependents
= dlist
215 def get_dependents(self
):
216 return self
.dependents
218 def set_linkstate(self
, l
):
221 def get_linkstate(self
):
222 return self
.linkstate
224 def get_attr_value(self
, attr_name
):
225 config
= self
.get_config()
227 return config
.get(attr_name
)
229 def get_attr_value_first(self
, attr_name
):
230 config
= self
.get_config()
232 attr_value_list
= config
.get(attr_name
)
233 if attr_value_list
is not None:
234 return attr_value_list
[0]
238 def get_attr_value_n(self
, attr_name
, attr_index
):
239 config
= self
.get_config()
241 attr_value_list
= config
.get(attr_name
)
242 if attr_value_list
is not None:
244 return attr_value_list
[attr_index
]
251 if self
.env
is None or len(self
.env
) == 0:
255 def set_env(self
, env
):
258 def generate_env(self
):
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]
269 def update_config(self
, attr_name
, attr_value
, attr_status
=0):
270 if attr_value
is None:
274 self
.set_status(ifaceStatus
.ERROR
)
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
)
281 self
.config
[attr_name
] = [attr_value
]
283 """ XXX: If status needs to be encoded in the query string
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 """
293 def dump_raw(self
, logger
):
295 print (self
.raw_lines
[0])
296 for i
in range(1, len(self
.raw_lines
)):
297 print (indent
+ self
.raw_lines
[i
])
299 def dump(self
, logger
):
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()
311 logger
.info(indent
+ 'dependents: %s' %str
(d
))
313 logger
.info(indent
+ 'dependents: None')
315 logger
.info(indent
+ 'config: ')
316 config
= self
.get_config()
317 if config
is not None:
318 logger
.info(indent
+ indent
+ str(config
))
321 def dump_pretty(self
, logger
):
323 outbuf
= 'iface %s' %self
.get_name()
324 if self
.get_addr_family() is not None:
325 outbuf
+= ' %s' %self
.get_addr_family()
327 if self
.get_addr_method() is not None:
328 outbuf
+= ' %s' %self
.get_addr_method()
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
338 #outbuf += ('%s' %indent + '%s' %self.get_state_str() +
339 # ' %s' %self.get_status_str())