CORD-2476 migrate vsg service to dynamic load

Change-Id: Ifadbf322b8f59f8e87a77822e652abb7ed107af6
diff --git a/xos/synchronizer/model_policies/test_model_policy_vsgserviceinstance.py b/xos/synchronizer/model_policies/test_model_policy_vsgserviceinstance.py
index aaad139..99cdd94 100644
--- a/xos/synchronizer/model_policies/test_model_policy_vsgserviceinstance.py
+++ b/xos/synchronizer/model_policies/test_model_policy_vsgserviceinstance.py
@@ -27,9 +27,17 @@
     xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
     services_dir=os.path.join(xos_dir, "../../xos_services")
 
-# ---------------------------------------------------------------------------------------------------------------------
-# End Model Policy Testing Framework
-# ---------------------------------------------------------------------------------------------------------------------
+# While transitioning from static to dynamic load, the path to find neighboring xproto files has changed. So check
+# both possible locations...
+def get_models_fn(service_name, xproto_name):
+    name = os.path.join(service_name, "xos", xproto_name)
+    if os.path.exists(os.path.join(services_dir, name)):
+        return name
+    else:
+        name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
+        if os.path.exists(os.path.join(services_dir, name)):
+            return name
+    raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
 
 class TestModelPolicyVsgTenant(unittest.TestCase):
     def setUp(self):
@@ -45,7 +53,8 @@
         Config.init(config, 'synchronizer-config-schema.yaml')
 
         from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
-        build_mock_modelaccessor(xos_dir, services_dir, ["vsg/xos/vsg.xproto", "addressmanager/xos/addressmanager.xproto"])
+        build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("vsg", "vsg.xproto"),
+                                                         get_models_fn("addressmanager", "addressmanager.xproto")])
 
         import synchronizers.new_base.modelaccessor
         import synchronizers.new_base.model_policies.model_policy_tenantwithcontainer
diff --git a/xos/synchronizer/models/models.py b/xos/synchronizer/models/models.py
new file mode 100644
index 0000000..108de2f
--- /dev/null
+++ b/xos/synchronizer/models/models.py
@@ -0,0 +1,41 @@
+
+# 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 xos.exceptions import *
+from models_decl import *
+
+class VSGServiceInstance(VSGServiceInstance_decl):
+    class Meta:
+        proxy = True
+
+    def __init__(self, *args, **kwargs):
+        super(VSGServiceInstance, self).__init__(*args, **kwargs)
+
+    def save(self, *args, **kwargs):
+        if not self.creator:
+            if not getattr(self, "caller", None):
+                # caller must be set when creating a vCPE since it creates a slice
+                raise XOSProgrammingError("VSGServiceInstance's self.caller was not set")
+            self.creator = self.caller
+            if not self.creator:
+                raise XOSProgrammingError("VSGServiceInstance's self.creator was not set")
+
+        super(VSGServiceInstance, self).save(*args, **kwargs)
+
+class VSGService(VSGService_decl):
+    class Meta:
+        proxy = True
+
+    pass
diff --git a/xos/synchronizer/models/vsg.xproto b/xos/synchronizer/models/vsg.xproto
new file mode 100644
index 0000000..6202ef4
--- /dev/null
+++ b/xos/synchronizer/models/vsg.xproto
@@ -0,0 +1,21 @@
+option name = "vsg";
+option app_label = "vsg";
+option kind = "vCPE";
+option legacy="True";
+
+message VSGService (Service){
+    option verbose_name = "vSG Service";
+
+    optional string url_filter_kind = 1 [choices = "((None, 'None'), ('safebrowsing', 'Safe Browsing'), ('answerx', 'AnswerX'))", max_length = 30, content_type = "stripped", blank = True, null = True, db_index = False];
+    required string dns_servers = 2 [default = "8.8.8.8", max_length = 255, content_type = "stripped", blank = False, null = False, db_index = False];
+    optional string node_label = 3 [db_index = False, max_length = 30, null = True, content_type = "stripped", blank = True];
+    required string docker_image_name = 4 [default = "docker.io/xosproject/vsg", max_length = 255, content_type = "stripped", blank = False, null = False, db_index = False];
+    required bool docker_insecure_registry = 5 [default = False, null = False, db_index = False, blank = True];
+}
+
+
+message VSGServiceInstance (TenantWithContainer){
+    option verbose_name = "vSG Service Instance";
+    
+    optional string last_ansible_hash = 1 [db_index = False, max_length = 128, null = True, content_type = "stripped", blank = True];
+}
diff --git a/xos/synchronizer/steps/test_sync_vsgserviceinstance.py b/xos/synchronizer/steps/test_sync_vsgserviceinstance.py
index c436d13..31f3674 100644
--- a/xos/synchronizer/steps/test_sync_vsgserviceinstance.py
+++ b/xos/synchronizer/steps/test_sync_vsgserviceinstance.py
@@ -27,6 +27,18 @@
     xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
     services_dir=os.path.join(xos_dir, "../../xos_services")
 
+# While transitioning from static to dynamic load, the path to find neighboring xproto files has changed. So check
+# both possible locations...
+def get_models_fn(service_name, xproto_name):
+    name = os.path.join(service_name, "xos", xproto_name)
+    if os.path.exists(os.path.join(services_dir, name)):
+        return name
+    else:
+        name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
+        if os.path.exists(os.path.join(services_dir, name)):
+            return name
+    raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
+
 class TestSyncVSGServiceInstance(unittest.TestCase):
     def setUp(self):
         global SyncVSGServiceInstance, LeastLoadedNodeScheduler, MockObjectList
@@ -41,7 +53,8 @@
         Config.init(config, 'synchronizer-config-schema.yaml')
 
         from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
-        build_mock_modelaccessor(xos_dir, services_dir, ["vsg/xos/vsg.xproto", "addressmanager/xos/addressmanager.xproto"])
+        build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("vsg", "vsg.xproto"),
+                                                         get_models_fn("addressmanager", "addressmanager.xproto")])
 
         import synchronizers.new_base.modelaccessor
         import synchronizers.new_base.model_policies.model_policy_tenantwithcontainer
diff --git a/xos/synchronizer/vsg_config.yaml b/xos/synchronizer/vsg_config.yaml
index 3e91fc8..55521da 100644
--- a/xos/synchronizer/vsg_config.yaml
+++ b/xos/synchronizer/vsg_config.yaml
@@ -14,7 +14,7 @@
 # limitations under the License.
 
 
-name: vsg-synchronizer
+name: vsg
 accessor:
   username: xosadmin@opencord.org
   password: "@/opt/xos/services/vsg/credentials/xosadmin@opencord.org"
@@ -22,3 +22,4 @@
 steps_dir: "/opt/xos/synchronizers/vsg/steps"
 sys_dir: "/opt/xos/synchronizers/vsg/sys"
 model_policies_dir: "/opt/xos/synchronizers/vsg/model_policies"
+models_dir: "/opt/xos/synchronizers/vsg/models"