]> git.proxmox.com Git - mirror_ifupdown2.git/blame - pkg/statemanager.py
ip batch support for mstp bridges + add support for multiple globs in a
[mirror_ifupdown2.git] / pkg / statemanager.py
CommitLineData
a6f80f0e 1#!/usr/bin/python
3e8ee54f 2#
3# Copyright 2013. Cumulus Networks, Inc.
4# Author: Roopa Prabhu, roopa@cumulusnetworks.com
5#
6# stateManager --
7# interface state manager
8#
a6f80f0e 9import cPickle
10from collections import OrderedDict
a6f80f0e 11import logging
a6f80f0e 12import os
13from iface import *
31a5f4c3 14import copy
a6f80f0e 15
16class pickling():
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)
24 except:
25 raise
26
27 @classmethod
28 def save_obj(cls, f, obj):
29 try:
30 cPickle.dump(obj, f)
31 except:
32 raise
33
a6f80f0e 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
a6f80f0e 42class stateManager():
43
a690dfae 44 state_dir = '/var/tmp/network/'
45 state_filename = 'ifstatenew'
a6f80f0e 46
47 def __init__(self):
48 self.ifaceobjdict = OrderedDict()
49 self.logger = logging.getLogger('ifupdown.' +
50 self.__class__.__name__)
a690dfae 51 if not os.path.exists(self.state_dir):
52 os.mkdir(self.state_dir)
53 self.state_file = self.state_dir + self.state_filename
a6f80f0e 54
55 def save_ifaceobj(self, ifaceobj):
56 if self.ifaceobjdict.get(ifaceobj.get_name()) is None:
57 self.ifaceobjdict[ifaceobj.get_name()] = [ifaceobj]
58 else:
59 self.ifaceobjdict[ifaceobj.get_name()].append(ifaceobj)
60
61 def read_saved_state(self, filename=None):
62 pickle_filename = filename
fe0a57d3 63 if not pickle_filename:
a6f80f0e 64 pickle_filename = self.state_file
65
66 if not os.path.exists(pickle_filename):
67 return
68
69 # Read all ifaces from file
70 for ifaceobj in pickling.load(pickle_filename):
71 self.save_ifaceobj(ifaceobj)
a6f80f0e 72
73 return 0
74
75 def get_ifaceobjdict(self):
76 return self.ifaceobjdict
77
a6f80f0e 78 def compare_iface_state(ifaceobj1, ifaceobj2):
79 ifaceobj1_state = ifaceobj1.get_state()
80 ifaceobj2_state = ifaceobj2.get_state()
81
82 if ifaceobj1_state < ifaceobj2_state:
83 return -1
84 elif ifaceobj1_state > ifaceobj2_state:
85 return 1
86 elif ifaceobj1_state == ifaceobj2_state:
87 return 0
88
a6f80f0e 89 def cmp_old_new_state(self, ifacename, operation):
90 """ compares current operation with old state """
91
92 state_arg = ifaceState.from_str(operation)
93 if state_arg == ifaceState.UP:
94 old_ifaceobj = self.ifaceobjdict.get(ifacename)
95 if old_ifaceobj != None:
96 # found old state for iface
97 # Check its state
98 if (old_ifaceobj.get_state() == state_arg and
99 old_ifaceobj.get_status() == ifaceStatus.SUCCESS):
100 self.statemsg = 'iface already up'
101 return 0
102 elif state_arg == ifaceState.DOWN:
103 old_ifaceobj = self.ifaceobjdict.get(ifname)
104 if old_ifaceobj != None:
105 # found old state for iface
106 # Check its state
107 if (old_ifaceobj.get_state() == state_arg and
108 old_ifaceobj.get_status() == ifaceStatus.SUCCESS):
109 self.statemsg = 'iface already down'
110 return 0
111
112 return 1
113
31a5f4c3 114 def ifaceobj_compare(self, ifaceobj_a, ifaceobj_b):
a6f80f0e 115 if ifaceobj_a.get_name() != ifaceobj_b.get_name():
116 return False
117
118 if (ifaceobj_a.get_addr_family() is None and
119 ifaceobj_b.get_addr_family() is not None):
120 return False
121
122 if (ifaceobj_a.get_addr_family() is not None and
123 ifaceobj_b.get_addr_family() is None):
124 return False
125
126 if (ifaceobj_a.get_addr_family() is None and
127 ifaceobj_b.get_addr_family() is None):
128 return True
129
130 if ifaceobj_a.get_addr_family() != ifaceobj_b.get_addr_family():
131 return False
132
133 return True
134
31a5f4c3 135 def ifaceobj_sync(self, ifaceobj):
136 ifacename = ifaceobj.get_name()
137 self.logger.debug('%s: statemanager sync state' %ifacename)
138 old_ifaceobjs = self.ifaceobjdict.get(ifacename)
139 if not old_ifaceobjs:
140 self.ifaceobjdict[ifacename] = [ifaceobj]
a6f80f0e 141 else:
31a5f4c3 142 if old_ifaceobjs[0].flags & iface.PICKLED:
143 del self.ifaceobjdict[ifacename]
144 self.ifaceobjdict[ifacename] = [ifaceobj]
145 else:
146 self.ifaceobjdict[ifacename].append(ifaceobj)
a6f80f0e 147
31a5f4c3 148 def save_state(self):
a6f80f0e 149 try:
150 with open(self.state_file, 'w') as f:
31a5f4c3 151 for ifaceobjs in self.ifaceobjdict.values():
a6f80f0e 152 for i in ifaceobjs:
153 pickling.save_obj(f, i)
154 except:
155 raise
156
a6f80f0e 157 def print_state(self, ifaceobj, prefix, indent):
eab25b7c 158 print (indent + '%s' %prefix +
159 '%s' %ifaceobj.get_state_str() +
160 ', %s' %ifaceobj.get_status_str())
a6f80f0e 161
162 def print_state_pretty(self, ifacenames, logger):
163 for ifacename in ifacenames:
164 old_ifaceobjs = self.ifaceobjdict.get(ifacename)
165 if old_ifaceobjs is not None:
166 firstifaceobj = old_ifaceobjs[0]
eab25b7c 167 self.print_state(firstifaceobj,
168 '%s: ' %firstifaceobj.get_name(), '')
a6f80f0e 169
170 def print_state_detailed_pretty(self, ifacenames, logger):
eab25b7c 171 indent = '\t'
a6f80f0e 172 for ifacename in ifacenames:
173 old_ifaceobjs = self.ifaceobjdict.get(ifacename)
174 if old_ifaceobjs is not None:
175 for i in old_ifaceobjs:
176 i.dump_pretty(logger)
eab25b7c 177 self.print_state(i, '', indent)
178 print '\n'
a6f80f0e 179
180 def dump(self, ifacenames=None):
31a5f4c3 181 self.logger.debug('statemanager iface state:')
fe0a57d3 182 if ifacenames:
a6f80f0e 183 for i in ifacenames:
184 ifaceobj = self.ifaces.get(i)
185 if ifaceobj is None:
186 raise ifaceNotFoundError('ifname %s'
187 %i + ' not found')
188 ifaceobj.dump(self.logger)
189 else:
31a5f4c3 190 for ifacename, ifaceobjs in self.ifaceobjdict.items():
191 [i.dump(self.logger) for i in ifaceobjs]