]> git.proxmox.com Git - mirror_ifupdown2.git/blob - pkg/statemanager.py
Remove upper device check warnings + implicitly follow upperifaces when
[mirror_ifupdown2.git] / pkg / statemanager.py
1 #!/usr/bin/python
2 #
3 # Copyright 2013. Cumulus Networks, Inc.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
5 #
6 # stateManager --
7 # interface state manager
8 #
9 import cPickle
10 from collections import OrderedDict
11 import logging
12 import os
13 from iface import *
14
15 class pickling():
16 """ class with helper methods for pickling/unpickling iface objects """
17
18 @classmethod
19 def save(cls, filename, list_of_objects):
20 try:
21 with open(filename, 'w') as f:
22 for obj in list_of_objects:
23 cPickle.dump(obj, f, cPickle.HIGHEST_PROTOCOL)
24 except:
25 raise
26
27 @classmethod
28 def save_obj(cls, f, obj):
29 try:
30 cPickle.dump(obj, f, cPickle.HIGHEST_PROTOCOL)
31 except:
32 raise
33
34 @classmethod
35 def load(cls, filename):
36 with open(filename, 'r') as f:
37 while True:
38 try: yield cPickle.load(f)
39 except EOFError: break
40 except: raise
41
42 class stateManager():
43 """ state manager for managing ifupdown iface obj state """
44
45 state_dir = '/var/tmp/network/'
46 state_filename = 'ifstatenew'
47
48 def __init__(self):
49 self.ifaceobjdict = OrderedDict()
50 self.logger = logging.getLogger('ifupdown.' +
51 self.__class__.__name__)
52 if not os.path.exists(self.state_dir):
53 os.mkdir(self.state_dir)
54 self.state_file = self.state_dir + self.state_filename
55
56 def save_ifaceobj(self, ifaceobj):
57 self.ifaceobjdict.setdefault(ifaceobj.name,
58 []).append(ifaceobj)
59
60 def read_saved_state(self, filename=None):
61 pickle_filename = filename
62 if not pickle_filename:
63 pickle_filename = self.state_file
64 if not os.path.exists(pickle_filename):
65 return
66 for ifaceobj in pickling.load(pickle_filename):
67 self.save_ifaceobj(ifaceobj)
68
69 def get_ifaceobjs(self, ifacename):
70 return self.ifaceobjdict.get(ifacename)
71
72 def ifaceobj_sync(self, ifaceobj):
73 self.logger.debug('%s: statemanager sync state' %ifaceobj.name)
74 old_ifaceobjs = self.ifaceobjdict.get(ifaceobj.name)
75 if not old_ifaceobjs:
76 self.ifaceobjdict[ifaceobj.name] = [ifaceobj]
77 else:
78 # If it matches any of the object, return
79 if any(o.compare(ifaceobj) for o in old_ifaceobjs):
80 return
81 # If it does not match any of the objects, and if
82 # all objs in the list came from the pickled file,
83 # then reset the list and add this object as a fresh one,
84 # else append to the list
85 if old_ifaceobjs[0].flags & iface._PICKLED:
86 del self.ifaceobjdict[ifaceobj.name]
87 self.ifaceobjdict[ifaceobj.name] = [ifaceobj]
88 else:
89 self.ifaceobjdict[ifaceobj.name].append(ifaceobj)
90
91 def save_state(self):
92 try:
93 with open(self.state_file, 'w') as f:
94 for ifaceobjs in self.ifaceobjdict.values():
95 [pickling.save_obj(f, i) for i in ifaceobjs]
96 except:
97 raise
98
99 def dump_pretty(self, ifacenames, format='native'):
100 if not ifacenames:
101 ifacenames = self.ifaceobjdict.keys()
102 for i in ifacenames:
103 ifaceobjs = self.get_ifaceobjs(i)
104 if not ifaceobjs:
105 continue
106 for ifaceobj in ifaceobjs:
107 if format == 'json':
108 ifaceobj.dump_json()
109 else:
110 ifaceobj.dump_pretty()
111
112 def dump(self, ifacenames=None):
113 self.logger.debug('statemanager iface state:')
114 if ifacenames:
115 for i in ifacenames:
116 ifaceobj = self.ifaces.get(i)
117 if ifaceobj is None:
118 raise ifaceNotFoundError('ifname %s'
119 %i + ' not found')
120 ifaceobj.dump(self.logger)
121 else:
122 for ifacename, ifaceobjs in self.ifaceobjdict.items():
123 [i.dump(self.logger) for i in ifaceobjs]