]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - pkg/statemanager.py
3 # Copyright 2013. Cumulus Networks, Inc.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
7 # interface state manager
10 from collections
import OrderedDict
11 from exceptions
import *
20 def save(cls
, filename
, list_of_objects
):
22 with
open(filename
, 'w') as f
:
23 for obj
in list_of_objects
:
29 def save_obj(cls
, f
, obj
):
37 def load(cls
, filename
):
38 with
open(filename
, 'r') as f
:
40 try: yield cPickle
.load(f
)
41 except EOFError: break
48 state_file
= '/run/network/ifstatenew'
52 self
.ifaceobjdict
= OrderedDict()
53 self
.logger
= logging
.getLogger('ifupdown.' +
54 self
.__class
__.__name
__)
56 def save_ifaceobj(self
, ifaceobj
):
57 if self
.ifaceobjdict
.get(ifaceobj
.get_name()) is None:
58 self
.ifaceobjdict
[ifaceobj
.get_name()] = [ifaceobj
]
60 self
.ifaceobjdict
[ifaceobj
.get_name()].append(ifaceobj
)
62 def read_saved_state(self
, filename
=None):
63 pickle_filename
= filename
64 if pickle_filename
== None:
65 pickle_filename
= self
.state_file
67 if not os
.path
.exists(pickle_filename
):
70 # Read all ifaces from file
71 for ifaceobj
in pickling
.load(pickle_filename
):
72 self
.save_ifaceobj(ifaceobj
)
73 #ifaceobj.set_refcnt(0)
74 #ifaceobj.set_dependents(None)
78 def get_ifaceobjdict(self
):
79 return self
.ifaceobjdict
81 def save_state(self
, ifaceobjs
, filename
=None):
82 pickle_filename
= filename
83 if pickle_filename
== None:
84 pickle_filename
= self
.state_file
86 pickling
.save(pickle_filename
, ifaceobjs
)
89 def compare_iface_state(ifaceobj1
, ifaceobj2
):
90 ifaceobj1_state
= ifaceobj1
.get_state()
91 ifaceobj2_state
= ifaceobj2
.get_state()
93 if ifaceobj1_state
< ifaceobj2_state
:
95 elif ifaceobj1_state
> ifaceobj2_state
:
97 elif ifaceobj1_state
== ifaceobj2_state
:
100 def compare_iface_with_old(self
, ifaceobj
):
101 old_ifaceobj
= self
.ifaceobjdict
.get(ifaceobj
.get_name())
102 if old_ifaceobj
== None:
103 raise ifacenotfound(ifaceobj
.get_name())
105 if ifaceobj
.get_addr_family() != old_ifaceobj
.get_addr_family():
108 if ifaceobj
.get_method() != old_ifaceobj
.get_method():
111 # compare config items
112 unmatched_item
= set(ifaceobj
.items()) ^
set(old_ifaceobj
.items())
113 if len(unmatched_item
) != 0:
118 def get_iface_state_old(self
, ifaceobj
):
119 old_ifaceobj
= self
.ifaceobjdict
.get(ifaceobj
.get_name())
120 if old_ifaceobj
== None:
121 raise ifacenotfound(ifaceobj
.get_name())
123 return old_ifaceobj
.get_state()
125 def get_iface_status_old(self
, ifaceobj
):
126 old_ifaceobj
= self
.ifaceobjdict
.get(ifaceobj
.get_name())
127 if old_ifaceobj
== None:
128 raise ifacenotfound(ifaceobj
.get_name())
130 return old_ifaceobj
.get_status()
132 def cmp_old_new_state(self
, ifacename
, operation
):
133 """ compares current operation with old state """
135 state_arg
= ifaceState
.from_str(operation
)
136 if state_arg
== ifaceState
.UP
:
137 old_ifaceobj
= self
.ifaceobjdict
.get(ifacename
)
138 if old_ifaceobj
!= None:
139 # found old state for iface
141 if (old_ifaceobj
.get_state() == state_arg
and
142 old_ifaceobj
.get_status() == ifaceStatus
.SUCCESS
):
143 self
.statemsg
= 'iface already up'
145 elif state_arg
== ifaceState
.DOWN
:
146 old_ifaceobj
= self
.ifaceobjdict
.get(ifname
)
147 if old_ifaceobj
!= None:
148 # found old state for iface
150 if (old_ifaceobj
.get_state() == state_arg
and
151 old_ifaceobj
.get_status() == ifaceStatus
.SUCCESS
):
152 self
.statemsg
= 'iface already down'
157 def iface_obj_compare(self
, ifaceobj_a
, ifaceobj_b
):
158 if ifaceobj_a
.get_name() != ifaceobj_b
.get_name():
161 if (ifaceobj_a
.get_addr_family() is None and
162 ifaceobj_b
.get_addr_family() is not None):
165 if (ifaceobj_a
.get_addr_family() is not None and
166 ifaceobj_b
.get_addr_family() is None):
169 if (ifaceobj_a
.get_addr_family() is None and
170 ifaceobj_b
.get_addr_family() is None):
173 if ifaceobj_a
.get_addr_family() != ifaceobj_b
.get_addr_family():
179 def update_iface_state(self
, ifaceobj
):
180 old_ifaceobjs
= self
.ifaceobjdict
.get(ifaceobj
.get_name())
181 if old_ifaceobjs
is None:
182 self
.ifaceobjdict
[ifaceobj
.get_name()] = [ifaceobj
]
184 for oi
in old_ifaceobjs
:
185 if self
.iface_obj_compare(ifaceobj
, oi
) == True:
186 oi
.set_state(ifaceobj
.get_state())
187 oi
.set_status(ifaceobj
.get_status())
190 self
.ifaceobjdict
[ifaceobj
.get_name()].append(ifaceobj
)
192 def flush_state(self
, ifaceobjdict
=None):
193 if ifaceobjdict
is None:
194 ifaceobjdict
= self
.ifaceobjdict
197 with
open(self
.state_file
, 'w') as f
:
198 for ifaceobjs
in ifaceobjdict
.values():
200 pickling
.save_obj(f
, i
)
205 def is_valid_state_transition(self
, ifaceobj
, tobe_state
):
206 if self
.ifaceobjdict
is None:
209 if tobe_state
== 'up':
210 max_tobe_state
= ifaceState
.POST_UP
211 elif tobe_state
== 'down':
212 max_tobe_state
= ifaceState
.POST_DOWN
216 old_ifaceobjs
= self
.ifaceobjdict
.get(ifaceobj
.get_name())
217 if old_ifaceobjs
is not None:
218 for oi
in old_ifaceobjs
:
219 if self
.iface_obj_compare(ifaceobj
, oi
) == True:
220 if (oi
.get_state() == max_tobe_state
and
221 oi
.get_status() == ifaceStatus
.SUCCESS
):
222 # if old state is greater than or equal to
232 def print_state(self
, ifaceobj
, prefix
, indent
):
233 print (indent
+ '%s' %prefix
+
234 '%s' %ifaceobj
.get_state_str() +
235 ', %s' %ifaceobj
.get_status_str())
237 def print_state_pretty(self
, ifacenames
, logger
):
238 for ifacename
in ifacenames
:
239 old_ifaceobjs
= self
.ifaceobjdict
.get(ifacename
)
240 if old_ifaceobjs
is not None:
241 firstifaceobj
= old_ifaceobjs
[0]
242 self
.print_state(firstifaceobj
,
243 '%s: ' %firstifaceobj
.get_name(), '')
245 def print_state_detailed_pretty(self
, ifacenames
, logger
):
247 for ifacename
in ifacenames
:
248 old_ifaceobjs
= self
.ifaceobjdict
.get(ifacename
)
249 if old_ifaceobjs
is not None:
250 for i
in old_ifaceobjs
:
251 i
.dump_pretty(logger
)
252 self
.print_state(i
, '', indent
)
255 def dump(self
, ifacenames
=None):
257 if ifacenames
is not None and len(ifacenames
) > 0:
259 ifaceobj
= self
.ifaces
.get(i
)
261 raise ifaceNotFoundError('ifname %s'
263 ifaceobj
.dump(self
.logger
)
265 for ifacename
, ifaceobj
in self
.ifaceobjdict
.items():
266 ifaceobj
.dump(self
.logger
)