CORD-1243 move volt model_policies to model policy framework

Change-Id: I6797ed2c39afdfb69194da4fc00d71e99ed47246
diff --git a/xos/attic/volttenant_bottom.py b/xos/attic/volttenant_bottom.py
deleted file mode 100644
index d0935de..0000000
--- a/xos/attic/volttenant_bottom.py
+++ /dev/null
@@ -1,11 +0,0 @@
-def model_policy_volt(pk):
-    # TODO: this should be made in to a real model_policy
-    with transaction.atomic():
-        volt = VOLTTenant.objects.select_for_update().filter(pk=pk)
-        if not volt:
-            return
-        volt = volt[0]
-        volt.manage_vcpe()
-        volt.manage_subscriber()
-        volt.cleanup_orphans()
-
diff --git a/xos/attic/volttenant_model.py b/xos/attic/volttenant_model.py
index 37b8ef0..bf15c30 100644
--- a/xos/attic/volttenant_model.py
+++ b/xos/attic/volttenant_model.py
@@ -33,63 +33,6 @@
         return None
     return subs[0]
 
-def manage_vcpe(self):
-    # Each VOLT object owns exactly one VCPE object
-
-    if self.deleted:
-        return
-
-    # Check to see if the wrong s-tag is set. This can only happen if the
-    # user changed the s-tag after the VoltTenant object was created.
-    if self.vcpe and self.vcpe.instance:
-        s_tags = Tag.objects.filter(content_type=self.vcpe.instance.get_content_type_key(), object_id=self.vcpe.instance.id, name="s_tag")
-        if s_tags and (s_tags[0].value != str(self.s_tag)):
-            self.vcpe.delete()
-
-    if self.vcpe is None:
-        from services.vsg.models import VSGService, VSGTenant
-        vsgServices = VSGService.objects.all()
-        if not vsgServices:
-            raise XOSConfigurationError("No VSG Services available")
-
-        vcpe = VSGTenant(provider_service = vsgServices[0],
-                          subscriber_tenant = self)
-        vcpe.caller = self.creator
-        vcpe.save()
-
-def manage_subscriber(self):
-    if (self.subscriber_root is None):
-        # The vOLT is not connected to a Subscriber, so either find an
-        # existing subscriber with the same SSID, or autogenerate a new
-        # subscriber.
-        #
-        # TODO: This probably goes away when we rethink the ONOS-to-XOS
-        # vOLT API.
-
-        subs = CordSubscriberRoot.objects.filter(service_specific_id = self.service_specific_id)
-        if subs:
-            sub = subs[0]
-        else:
-            sub = CordSubscriberRoot(service_specific_id = self.service_specific_id,
-                                     name = "autogenerated-for-vOLT-%s" % self.id)
-            sub.save()
-        self.subscriber_root = sub
-        self.save()
-
-def cleanup_vcpe(self):
-    if self.vcpe:
-        # print "XXX cleanup vcpe", self.vcpe
-        self.vcpe.delete()
-
-def cleanup_orphans(self):
-    from services.vsg.models import VSGTenant
-    # ensure vOLT only has one vCPE
-    cur_vcpe = self.vcpe
-    for vcpe in list(self.get_subscribed_tenants(VSGTenant)):
-        if (not cur_vcpe) or (vcpe.id != cur_vcpe.id):
-            # print "XXX clean up orphaned vcpe", vcpe
-            vcpe.delete()
-
 def save(self, *args, **kwargs):
     # VOLTTenant probably doesn't need a SSID anymore; that will be handled
     # by CORDSubscriberRoot...
@@ -109,9 +52,5 @@
             raise XOSProgrammingError("VOLTTenant's self.creator was not set")
 
     super(VOLTTenant, self).save(*args, **kwargs)
-    model_policy_volt(self.pk)
 
-def delete(self, *args, **kwargs):
-    self.cleanup_vcpe()
-    super(VOLTTenant, self).delete(*args, **kwargs)
 
diff --git a/xos/synchronizer/Dockerfile.synchronizer b/xos/synchronizer/Dockerfile.synchronizer
new file mode 100644
index 0000000..b7343f7
--- /dev/null
+++ b/xos/synchronizer/Dockerfile.synchronizer
@@ -0,0 +1,26 @@
+FROM xosproject/xos-synchronizer-base:candidate
+
+ADD . /opt/xos/synchronizers/volt
+
+ENTRYPOINT []
+
+WORKDIR "/opt/xos/synchronizers/volt"
+
+# Label image
+ARG org_label_schema_schema_version=1.0
+ARG org_label_schema_name=vsg-synchronizer
+ARG org_label_schema_version=unknown
+ARG org_label_schema_vcs_url=unknown
+ARG org_label_schema_vcs_ref=unknown
+ARG org_label_schema_build_date=unknown
+ARG org_opencord_vcs_commit_date=unknown
+
+LABEL org.label-schema.schema-version=$org_label_schema_schema_version \
+      org.label-schema.name=$org_label_schema_name \
+      org.label-schema.version=$org_label_schema_version \
+      org.label-schema.vcs-url=$org_label_schema_vcs_url \
+      org.label-schema.vcs-ref=$org_label_schema_vcs_ref \
+      org.label-schema.build-date=$org_label_schema_build_date \
+      org.opencord.vcs-commit-date=$org_opencord_vcs_commit_date
+
+CMD bash -c "cd /opt/xos/synchronizers/volt; ./run.sh"
diff --git a/xos/synchronizer/model-deps b/xos/synchronizer/model-deps
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/xos/synchronizer/model-deps
@@ -0,0 +1 @@
+{}
diff --git a/xos/synchronizer/model_policies/model_policy_volttenant.py b/xos/synchronizer/model_policies/model_policy_volttenant.py
new file mode 100644
index 0000000..b474263
--- /dev/null
+++ b/xos/synchronizer/model_policies/model_policy_volttenant.py
@@ -0,0 +1,74 @@
+from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
+
+class VOLTTenantPolicy(Policy):
+    model_name = "VOLTTenant"
+
+    def handle_create(self, tenant):
+        return self.handle_update(tenant)
+
+    def handle_update(self, tenant):
+        self.manage_vsg(tenant)
+        self.manage_subscriber(tenant)
+        self.cleanup_orphans(tenant)
+
+    def handle_delete(self, tenant):
+        if tenant.vcpe:
+            tenant.vcpe.delete()
+
+    def manage_vsg(self, tenant):
+        # Each VOLT object owns exactly one VCPE object
+
+        if tenant.deleted:
+            self.logger.info("MODEL_POLICY: volttenant %s deleted, deleting vsg" % tenant)
+            return
+
+        # Check to see if the wrong s-tag is set. This can only happen if the
+        # user changed the s-tag after the VoltTenant object was created.
+        if tenant.vcpe and tenant.vcpe.instance:
+            s_tags = Tag.objects.filter(content_type=tenant.vcpe.instance.self_content_type_id,
+                                        object_id=tenant.vcpe.instance.id, name="s_tag")
+            if s_tags and (s_tags[0].value != str(tenant.s_tag)):
+                self.logger.info("MODEL_POLICY: volttenant %s s_tag changed, deleting vsg" % tenant)
+                tenant.vcpe.delete()
+
+        if tenant.vcpe is None:
+            vsgServices = VSGService.objects.all()
+            if not vsgServices:
+                raise XOSConfigurationError("No VSG Services available")
+
+            self.logger.info("MODEL_POLICY: volttenant %s creating vsg" % tenant)
+
+            vcpe = VSGTenant(provider_service=vsgServices[0],
+                             subscriber_tenant=tenant)
+            vcpe.creator = tenant.creator
+            vcpe.save()
+
+    def manage_subscriber(self, tenant):
+        if (tenant.subscriber_root is None):
+            # The vOLT is not connected to a Subscriber, so either find an
+            # existing subscriber with the same SSID, or autogenerate a new
+            # subscriber.
+            #
+            # TODO: This probably goes away when we rethink the ONOS-to-XOS
+            # vOLT API.
+
+            subs = CordSubscriberRoot.objects.filter(service_specific_id = tenant.service_specific_id)
+            if subs:
+                self.logger.info("MODEL_POLICY: volttenant %s using existing subscriber root" % tenant)
+                sub = subs[0]
+            else:
+                self.logger.info("MODEL_POLICY: volttenant %s creating new subscriber root" % tenant)
+                sub = CordSubscriberRoot(service_specific_id = tenant.service_specific_id,
+                                         name = "autogenerated-for-vOLT-%s" % tenant.id)
+                sub.save()
+            tenant.subscriber_root = sub
+            tenant.save()
+
+    def cleanup_orphans(self, tenant):
+        # ensure vOLT only has one vCPE
+        cur_vcpe = tenant.vcpe
+        subscribed_vcpes = VSGTenant.objects.filter(subscriber_tenant_id = tenant.id)
+        for vcpe in subscribed_vcpes:
+            if (not cur_vcpe) or (vcpe.id != cur_vcpe.id):
+                vcpe.delete()
diff --git a/xos/synchronizer/run.sh b/xos/synchronizer/run.sh
new file mode 100755
index 0000000..d39b08a
--- /dev/null
+++ b/xos/synchronizer/run.sh
@@ -0,0 +1,2 @@
+export XOS_DIR=/opt/xos
+python volt-synchronizer.py  -C $XOS_DIR/synchronizers/volt/volt_config
diff --git a/xos/synchronizer/volt-synchronizer.py b/xos/synchronizer/volt-synchronizer.py
new file mode 100755
index 0000000..26b44df
--- /dev/null
+++ b/xos/synchronizer/volt-synchronizer.py
@@ -0,0 +1,11 @@
+#!/usr/bin/env python
+
+# This imports and runs ../../xos-observer.py
+
+import importlib
+import os
+import sys
+observer_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),"../../synchronizers/new_base")
+sys.path.append(observer_path)
+mod = importlib.import_module("xos-synchronizer")
+mod.main()
diff --git a/xos/synchronizer/volt_config b/xos/synchronizer/volt_config
new file mode 100644
index 0000000..a756881
--- /dev/null
+++ b/xos/synchronizer/volt_config
@@ -0,0 +1,18 @@
+# Sets options for the synchronizer
+[observer]
+name=volt
+dependency_graph=/opt/xos/synchronizers/volt/model-deps
+# volt synchronizer has no steps and no sys dir
+steps_dir=
+sys_dir=
+model_policies_dir=/opt/xos/synchronizers/volt/model_policies
+log_file=console
+log_level=debug
+pretend=False
+backoff_disabled=True
+save_ansible_output=True
+proxy_ssh=True
+proxy_ssh_key=/opt/cord_profile/node_key
+proxy_ssh_user=root
+accessor_kind=api
+accessor_password=@/opt/xos/services/volt/credentials/xosadmin@opencord.org