]> git.proxmox.com Git - mirror_ifupdown2.git/blobdiff - pkg/statemanager.py
ip batch support for mstp bridges + add support for multiple globs in a
[mirror_ifupdown2.git] / pkg / statemanager.py
index 681444356bbbba4626c525d753bbf85c23100254..289bb20f382418345f26cdf5f5a3631a00bca17d 100644 (file)
@@ -1,12 +1,17 @@
 #!/usr/bin/python
-
+#
+# Copyright 2013.  Cumulus Networks, Inc.
+# Author: Roopa Prabhu, roopa@cumulusnetworks.com
+#
+# stateManager --
+#    interface state manager
+#
 import cPickle
 from collections import OrderedDict
-from exceptions import *
 import logging
-import pprint
 import os
 from iface import *
+import copy
 
 class pickling():
 
@@ -26,7 +31,6 @@ class pickling():
         except:
             raise
 
-
     @classmethod
     def load(cls, filename):
         with open(filename, 'r') as f:
@@ -35,17 +39,18 @@ class pickling():
                 except EOFError: break
                 except: raise
 
-
-
 class stateManager():
 
-    state_file = '/run/network/ifstatenew'
-
+    state_dir = '/var/tmp/network/'
+    state_filename = 'ifstatenew'
 
     def __init__(self):
         self.ifaceobjdict = OrderedDict()
         self.logger = logging.getLogger('ifupdown.' +
                     self.__class__.__name__)
+        if not os.path.exists(self.state_dir):
+            os.mkdir(self.state_dir)
+        self.state_file = self.state_dir + self.state_filename
 
     def save_ifaceobj(self, ifaceobj):
         if self.ifaceobjdict.get(ifaceobj.get_name()) is None:
@@ -55,7 +60,7 @@ class stateManager():
 
     def read_saved_state(self, filename=None):
         pickle_filename = filename
-        if pickle_filename == None:
+        if not pickle_filename:
             pickle_filename = self.state_file
 
         if not os.path.exists(pickle_filename):
@@ -64,21 +69,12 @@ class stateManager():
         # Read all ifaces from file
         for ifaceobj in pickling.load(pickle_filename):
             self.save_ifaceobj(ifaceobj)
-            ifaceobj.set_refcnt(0)
 
         return 0
 
     def get_ifaceobjdict(self):
         return self.ifaceobjdict
 
-    def save_state(self, ifaceobjs, filename=None):
-        pickle_filename = filename
-        if pickle_filename == None:
-            pickle_filename = self.state_file
-
-        pickling.save(pickle_filename, ifaceobjs)
-
-
     def compare_iface_state(ifaceobj1, ifaceobj2):
         ifaceobj1_state = ifaceobj1.get_state()
         ifaceobj2_state = ifaceobj2.get_state()
@@ -90,38 +86,6 @@ class stateManager():
         elif ifaceobj1_state == ifaceobj2_state:
             return 0
 
-    def compare_iface_with_old(self, ifaceobj):
-        old_ifaceobj = self.ifaceobjdict.get(ifaceobj.get_name())
-        if old_ifaceobj == None:
-            raise ifacenotfound(ifaceobj.get_name())
-
-        if ifaceobj.get_addr_family() != old_ifaceobj.get_addr_family():
-            return -1
-
-        if ifaceobj.get_method() != old_ifaceobj.get_method():
-            return -1
-
-        # compare config items
-        unmatched_item = set(ifaceobj.items()) ^ set(old_ifaceobj.items())
-        if len(unmatched_item) != 0:
-            return -1
-
-        return 0
-
-    def get_iface_state_old(self, ifaceobj):
-        old_ifaceobj = self.ifaceobjdict.get(ifaceobj.get_name())
-        if old_ifaceobj == None:
-            raise ifacenotfound(ifaceobj.get_name())
-
-        return old_ifaceobj.get_state()
-
-    def get_iface_status_old(self, ifaceobj):
-        old_ifaceobj = self.ifaceobjdict.get(ifaceobj.get_name())
-        if old_ifaceobj == None:
-            raise ifacenotfound(ifaceobj.get_name())
-
-        return old_ifaceobj.get_status()
-
     def cmp_old_new_state(self, ifacename, operation):
         """ compares current operation with old state """
 
@@ -147,7 +111,7 @@ class stateManager():
 
         return 1
 
-    def iface_obj_compare(self, ifaceobj_a, ifaceobj_b):
+    def ifaceobj_compare(self, ifaceobj_a, ifaceobj_b):
         if ifaceobj_a.get_name() != ifaceobj_b.get_name():
             return False
 
@@ -168,85 +132,54 @@ class stateManager():
 
         return True
 
-
-    def update_iface_state(self, ifaceobj):
-        old_ifaceobjs = self.ifaceobjdict.get(ifaceobj.get_name())
-        if old_ifaceobjs is None:
-            self.ifaceobjdict[ifaceobj.get_name()] = [ifaceobj]
+    def ifaceobj_sync(self, ifaceobj):
+        ifacename = ifaceobj.get_name()
+        self.logger.debug('%s: statemanager sync state' %ifacename)
+        old_ifaceobjs = self.ifaceobjdict.get(ifacename)
+        if not old_ifaceobjs:
+            self.ifaceobjdict[ifacename] = [ifaceobj]
         else:
-            for oi in old_ifaceobjs:
-                if self.iface_obj_compare(ifaceobj, oi) == True:
-                    oi.set_state(ifaceobj.get_state())
-                    oi.set_status(ifaceobj.get_status())
-                    return
-
-            self.ifaceobjdict[ifaceobj.get_name()].append(ifaceobj)
-
-    def flush_state(self, ifaceobjdict=None):
-        if ifaceobjdict is None:
-            ifaceobjdict = self.ifaceobjdict
+            if old_ifaceobjs[0].flags & iface.PICKLED:
+                del self.ifaceobjdict[ifacename]
+                self.ifaceobjdict[ifacename] = [ifaceobj]
+            else:
+                self.ifaceobjdict[ifacename].append(ifaceobj)
 
+    def save_state(self):
         try:
             with open(self.state_file, 'w') as f:
-                for ifaceobjs in ifaceobjdict.values():
+                for ifaceobjs in self.ifaceobjdict.values():
                     for i in ifaceobjs:
                         pickling.save_obj(f, i)
         except:
             raise
 
-
-    def is_valid_state_transition(self, ifaceobj, tobe_state):
-        if self.ifaceobjdict is None:
-            return True
-
-        if tobe_state == 'up':
-            max_tobe_state = ifaceState.POST_UP
-        elif tobe_state == 'down':
-            max_tobe_state = ifaceState.POST_DOWN
-        else:
-            return True
-
-        old_ifaceobjs = self.ifaceobjdict.get(ifaceobj.get_name())
-        if old_ifaceobjs is not None:
-            for oi in old_ifaceobjs:
-                if self.iface_obj_compare(ifaceobj, oi) == True:
-                    if (oi.get_state() == max_tobe_state and
-                        oi.get_status() == ifaceStatus.SUCCESS):
-                        # if old state is greater than or equal to
-                        # tobe_state
-                        return False
-                    else:
-                        return True
-
-            return True
-        else:
-            return True
-
     def print_state(self, ifaceobj, prefix, indent):
-        print ('%s' %indent + '%s' %prefix +
-               '%s' %firstifaceobj.get_state_str(),
-               ', %s' %firstifaceobj.get_status_str())
-
+        print (indent + '%s' %prefix +
+               '%s' %ifaceobj.get_state_str() +
+               ', %s' %ifaceobj.get_status_str())
 
     def print_state_pretty(self, ifacenames, logger):
         for ifacename in ifacenames:
             old_ifaceobjs = self.ifaceobjdict.get(ifacename)
             if old_ifaceobjs is not None:
                 firstifaceobj = old_ifaceobjs[0]
-                self.print_state(self, firstifaceobj,
-                        '%s: ' %firstifaceobj.get_name(), indent)
+                self.print_state(firstifaceobj,
+                        '%s: ' %firstifaceobj.get_name(), '')
 
     def print_state_detailed_pretty(self, ifacenames, logger):
+        indent = '\t'
         for ifacename in ifacenames:
             old_ifaceobjs = self.ifaceobjdict.get(ifacename)
             if old_ifaceobjs is not None:
                 for i in old_ifaceobjs:
                     i.dump_pretty(logger)
-                    self.print_state(self, firstifaceobj, '', indent)
+                    self.print_state(i, '', indent)
+            print '\n'
 
     def dump(self, ifacenames=None):
-        print 'iface state:'
-        if ifacenames is not None and len(ifacenames) > 0:
+        self.logger.debug('statemanager iface state:')
+        if ifacenames:
             for i in ifacenames:
                 ifaceobj = self.ifaces.get(i)
                 if ifaceobj is None:
@@ -254,5 +187,5 @@ class stateManager():
                         %i + ' not found')
                 ifaceobj.dump(self.logger)
         else:
-            for ifacename, ifaceobj in self.ifaceobjdict.items():
-                ifaceobj.dump(self.logger)
+            for ifacename, ifaceobjs in self.ifaceobjdict.items():
+                [i.dump(self.logger) for i in ifaceobjs]