CORD-2247: Write basic static checker for xproto
Change-Id: I63a96972e16fd8dd62d4dec840eede66cbb26368
diff --git a/lib/xos-genx/xosgenx/checkers/model_policy.xchecker b/lib/xos-genx/xosgenx/checkers/model_policy.xchecker
new file mode 100644
index 0000000..abbdd0d
--- /dev/null
+++ b/lib/xos-genx/xosgenx/checkers/model_policy.xchecker
@@ -0,0 +1,5 @@
+{% for m in proto.messages %}
+{% if not m.options.no_policy %}
+{{ xproto_check_policy(m) }}
+{% endif %}
+{% endfor %}
diff --git a/lib/xos-genx/xosgenx/checkers/service_model_base.xchecker b/lib/xos-genx/xosgenx/checkers/service_model_base.xchecker
new file mode 100644
index 0000000..85881f0
--- /dev/null
+++ b/lib/xos-genx/xosgenx/checkers/service_model_base.xchecker
@@ -0,0 +1,32 @@
+{# Using regular variables has scoping issues. #}
+{# See: https://stackoverflow.com/questions/7537439/how-to-increment-a-variable-on-a-for-loop-in-jinja-template/7537466 #}
+{% set counter = {
+ 'num_service_models': 0,
+ 'num_service_instance_models': 0,
+ 'num_orphaned_models': 0,
+ } %}
+{% macro increment(key) %}
+ {% if counter.update({key: counter[key] + 1}) %} {% endif %}
+{% endmacro %}
+{% for m in proto.messages %}
+{% set matched = False %}
+{% set base_names = m.bases | map(attribute='name') | list %}
+{% if 'Service' in base_names %}
+{{ increment('num_service_models') }}
+{% set matched = True %}
+{% endif %}
+{% if not matched and 'ServiceInstance' in base_names or 'Tenant' in base_names or 'TenantWithContainer' in base_names %}
+{{ increment('num_service_instance_models') }}
+{% set matched = True %}
+{% endif %}
+{% if not matched and 'XOSBase' not in base_names %}
+501 Model does not have a parent - {{ m.name }}
+{% endif %}
+{% endfor %}
+{% if counter.num_service_models !=1 %}
+502 {{ counter.num_service_models }} Service models instead of 1
+{% elif counter.num_service_instance_models !=1 %}
+503 {{ counter.num_service_instance_models }} ServiceInstance models instead of 1
+{% else %}
+200 OK
+{% endif %}
diff --git a/lib/xos-genx/xosgenx/checkers/service_options.xchecker b/lib/xos-genx/xosgenx/checkers/service_options.xchecker
new file mode 100644
index 0000000..383cdbd
--- /dev/null
+++ b/lib/xos-genx/xosgenx/checkers/service_options.xchecker
@@ -0,0 +1,12 @@
+{% set required_options = ['name','verbose_name','app_label'] %}
+
+{% for m in proto.messages %}
+{% for o in required_options %}
+{% if not options[o] and not m.options[o] %}
+504 Required option {{ o }} is missing from model {{ m.name }}
+{% endif %}
+
+{% set required_field_options = ['tosca_key'] %}
+
+{% endfor %}
+{% endfor %}
diff --git a/lib/xos-genx/xosgenx/checkers/syncstep.xchecker b/lib/xos-genx/xosgenx/checkers/syncstep.xchecker
new file mode 100644
index 0000000..836a2c2
--- /dev/null
+++ b/lib/xos-genx/xosgenx/checkers/syncstep.xchecker
@@ -0,0 +1,5 @@
+{% for m in proto.messages %}
+{% if not m.options.no_sync %}
+{{ xproto_check_synchronizer(m) }}
+{% endif %}
+{% endfor %}