dynamically load model_policies

Change-Id: I665759b1c56747089a1a083486616069e947cf6d
diff --git a/xos/synchronizers/base/backend.py b/xos/synchronizers/base/backend.py
index b43bdfe..075a7b9 100644
--- a/xos/synchronizers/base/backend.py
+++ b/xos/synchronizers/base/backend.py
@@ -21,14 +21,14 @@
         observer_thread.start()
 
         # start model policies thread
-        observer_name = getattr(Config(), "observer_name", "")
-        if (not observer_name) or (observer_name=="openstack"):
+        policies_dir = getattr(Config(), "observer_model_policies_dir", None)
+        if policies_dir:
             from synchronizers.model_policy import run_policy
             model_policy_thread = threading.Thread(target=run_policy)
             model_policy_thread.start()
         else:
             model_policy_thread = None
-            print "Skipping model policies thread for service observer."
+            logger.info("Skipping model policies thread due to no model_policies dir.")
 
         while True:
             try:
diff --git a/xos/synchronizers/model_policy.py b/xos/synchronizers/model_policy.py
index 2ad6966..bf9bba0 100644
--- a/xos/synchronizers/model_policy.py
+++ b/xos/synchronizers/model_policy.py
@@ -7,7 +7,6 @@
 from django.dispatch import receiver
 from django.utils import timezone
 from generate.dependency_walker import *
-from synchronizers.openstack import model_policies
 from xos.logger import Logger, logging
 
 import pdb
@@ -17,6 +16,8 @@
 modelPolicyEnabled = True
 bad_instances=[]
 
+model_policies = {}
+
 logger = Logger(level=logging.INFO)
 
 def EnableModelPolicy(x):
@@ -57,6 +58,18 @@
         pass
     return
 
+def load_model_policies(policies_dir=None):
+    global model_policies
+
+    if policies_dir is None:
+            policies_dir = Config().observer_model_policies_dir
+
+    for fn in os.listdir(policies_dir):
+            pathname = os.path.join(policies_dir,fn)
+            if os.path.isfile(pathname) and fn.startswith("model_policy_") and fn.endswith(".py") and (fn!="__init__.py"):
+                model_policies[fn[:-3]] = imp.load_source(fn[:-3],pathname)
+
+    logger.info("Loaded model polices %s from %s" % (",".join(model_policies.keys()), policies_dir))
 
 #@atomic
 def execute_model_policy(instance, deleted):
@@ -120,6 +133,8 @@
            logger.log_exc("XXX some other error")
 
 def run_policy():
+    load_model_policies()
+
     while (True):
         start = time.time()
         try: