CORD-1849 Split XOS code out of OLT and VTN repos

Change-Id: I307b2a9a54fdcdab14e6bf96d9a9b060ff421a77
diff --git a/xos/synchronizer/Dockerfile.synchronizer b/xos/synchronizer/Dockerfile.synchronizer
new file mode 100644
index 0000000..a9d5e65
--- /dev/null
+++ b/xos/synchronizer/Dockerfile.synchronizer
@@ -0,0 +1,56 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+# xosproject/volt-synchronizer
+
+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=volt-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
+ARG org_opencord_component_chameleon_version=unknown
+ARG org_opencord_component_chameleon_vcs_url=unknown
+ARG org_opencord_component_chameleon_vcs_ref=unknown
+ARG org_opencord_component_xos_version=unknown
+ARG org_opencord_component_xos_vcs_url=unknown
+ARG org_opencord_component_xos_vcs_ref=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 \
+      org.opencord.component.chameleon.version=$org_opencord_component_chameleon_version \
+      org.opencord.component.chameleon.vcs-url=$org_opencord_component_chameleon_vcs_url \
+      org.opencord.component.chameleon.vcs-ref=$org_opencord_component_chameleon_vcs_ref \
+      org.opencord.component.xos.version=$org_opencord_component_xos_version \
+      org.opencord.component.xos.vcs-url=$org_opencord_component_xos_vcs_url \
+      org.opencord.component.xos.vcs-ref=$org_opencord_component_xos_vcs_ref
+
+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..ef5ba4e
--- /dev/null
+++ b/xos/synchronizer/model_policies/model_policy_volttenant.py
@@ -0,0 +1,94 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+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(owner=vsgServices[0])
+            vcpe.creator = tenant.creator
+            vcpe.save()
+            link = ServiceInstanceLink(provider_service_instance = vcpe, subscriber_service_instance = tenant)
+            link.save()
+
+    def manage_subscriber(self, tenant):
+        # check for existing link to a root
+        links = tenant.provided_links.all()
+        for link in links:
+            roots = CordSubscriberRoot.objects.filter(id = link.subscriber_service_instance.id)
+            if roots:
+                return
+
+        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()
+
+        link = ServiceInstanceLink(provider_service_instance = tenant, subscriber_service_instance = sub)
+        link.save()
+
+    def cleanup_orphans(self, tenant):
+        # ensure vOLT only has one vCPE
+        cur_vcpe = tenant.vcpe
+
+        links = tenant.subscribed_links.all()
+        for link in links:
+            vsgs = VSGTenant.objects.filter(id = link.provider_service_instance.id)
+            for vsg in vsgs:
+                if (not cur_vcpe) or (vsg.id != cur_vcpe.id):
+                    vsg.delete()
diff --git a/xos/synchronizer/run.sh b/xos/synchronizer/run.sh
new file mode 100755
index 0000000..2f90845
--- /dev/null
+++ b/xos/synchronizer/run.sh
@@ -0,0 +1,17 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+python volt-synchronizer.py
diff --git a/xos/synchronizer/volt-synchronizer.py b/xos/synchronizer/volt-synchronizer.py
new file mode 100755
index 0000000..25ab599
--- /dev/null
+++ b/xos/synchronizer/volt-synchronizer.py
@@ -0,0 +1,32 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+#!/usr/bin/env python
+
+# This imports and runs ../../xos-observer.py
+
+import importlib
+import os
+import sys
+from xosconfig import Config
+
+config_file = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/volt_config.yaml')
+Config.init(config_file, 'synchronizer-config-schema.yaml')
+
+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.yaml b/xos/synchronizer/volt_config.yaml
new file mode 100644
index 0000000..fdcd31f
--- /dev/null
+++ b/xos/synchronizer/volt_config.yaml
@@ -0,0 +1,22 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+name: volt-synchronizer
+accessor:
+  username: xosadmin@opencord.org
+  password: "@/opt/xos/services/volt/credentials/xosadmin@opencord.org"
+dependency_graph: "/opt/xos/synchronizers/volt/model-deps"
+model_policies_dir: "/opt/xos/synchronizers/volt/model_policies"
\ No newline at end of file