CORD-2247: Write basic static checker for xproto

Change-Id: I63a96972e16fd8dd62d4dec840eede66cbb26368
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/__init__.py b/lib/xos-genx/xosgenx/jinja2_extensions/__init__.py
index a81dcb6..859594c 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions/__init__.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/__init__.py
@@ -19,3 +19,4 @@
 from .fol2 import *
 from .gui import *
 from .tosca import *
+from .checklib import *
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/base.py b/lib/xos-genx/xosgenx/jinja2_extensions/base.py
index 5943e66..f8224b2 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions/base.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/base.py
@@ -271,4 +271,4 @@
 
         return list
     else:
-        return False
\ No newline at end of file
+        return False
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/checklib.py b/lib/xos-genx/xosgenx/jinja2_extensions/checklib.py
new file mode 100644
index 0000000..a34fd7f
--- /dev/null
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/checklib.py
@@ -0,0 +1,61 @@
+import ast
+
+def xproto_check_synchronizer(m):
+    try:
+        sync_step_path = 'synchronizer/steps/sync_%s.py'%m['name'].lower()
+        sync_step = open(sync_step_path).read()
+    except IOError:
+        return '510 Model needs a sync step %s'%sync_step_path
+
+    try:
+        sync_step_ast = ast.parse(sync_step)
+    except SyntaxError:
+        return '511 Could not parse sync step %s'%sync_step_path
+
+    classes = filter(lambda x:isinstance(x, ast.ClassDef), sync_step_ast.body)
+    found_sync_step_class = False
+
+    for c in classes:
+        base_names = [v.id for v in c.bases]
+        if 'SyncStep' in base_names or 'SyncInstanceUsingAnsible' in base_names:
+            attributes = filter(lambda x:isinstance(x, ast.Assign), c.body)
+            for a in attributes:
+                target_names = [t.id for t in a.targets]
+                values = a.value.elts if isinstance(a.value, ast.List) else [a.value]
+                value_names = [v.id for v in values]
+
+                if 'observes' in target_names and m['name'] in value_names:
+                    found_sync_step_class = True
+                    break
+
+    if not found_sync_step_class:
+        return '512 Synchronizer needs a sync step class with an observes field containing %s'%m['name']
+    else:
+        return '200 OK'
+
+
+def xproto_check_policy(m):
+    try:
+        model_policy_path = 'synchronizer/model_policies/model_policy_%s.py'%m['name'].lower()
+        model_policy = open(model_policy_path).read()
+    except IOError:
+        return '510 Model needs a model policy %s'%model_policy_path
+
+    try:
+        model_policy_ast = ast.parse(model_policy)
+    except SyntaxError:
+        return '511 Could not parse sync step %s'%model_policy_path
+
+    classes = filter(lambda x:isinstance(x, ast.ClassDef), model_policy_ast.body)
+    found_model_policy_class = False
+    for c in classes:
+        base_names = [v.id for v in c.bases]
+        if 'Policy' in base_names or 'TenantWithContainerPolicy' in base_names:
+            found_model_policy_class = True
+            break
+
+    if not found_model_policy_class:
+        return '513 Synchronizer needs a model policy class'
+    else:
+        return '200 OK'
+
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/gui.py b/lib/xos-genx/xosgenx/jinja2_extensions/gui.py
index 7c65388..50bcf0e 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions/gui.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/gui.py
@@ -124,4 +124,4 @@
             outlist.append('- {model: %s, type: %s, on_field: %s}\n' % (l['peer']['name'], l['link_type'], on_field))
         seen.append(l['peer'])
 
-    return outlist
\ No newline at end of file
+    return outlist
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/tosca.py b/lib/xos-genx/xosgenx/jinja2_extensions/tosca.py
index 51cd2bb..9e3b4a8 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions/tosca.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/tosca.py
@@ -62,4 +62,4 @@
 
         keys.append(of)
 
-    return keys
\ No newline at end of file
+    return keys