CORD-2476 migrate vsg service to dynamic load

Change-Id: Ifadbf322b8f59f8e87a77822e652abb7ed107af6
diff --git a/xos/attic/header.py b/xos/attic/header.py
deleted file mode 100644
index 0068eb3..0000000
--- a/xos/attic/header.py
+++ /dev/null
@@ -1,37 +0,0 @@
-
-# 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 django.db import models
-from core.models import Service, XOSBase, Slice, Instance, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool, User
-from core.models.xosbase import StrippedCharField
-import os
-from django.db import models, transaction
-from django.forms.models import model_to_dict
-from django.db.models import *
-from operator import itemgetter, attrgetter, methodcaller
-from core.models import Tag
-from core.models.service import LeastLoadedNodeScheduler
-# TODO: Break hardcoded dependencies / See if the addressmanager can be removed
-from services.addressmanager.models import AddressManagerService, AddressManagerServiceInstance
-import traceback
-from xos.exceptions import *
-from django.contrib.contenttypes.models import ContentType
-
-class ConfigurationError(Exception):
-    pass
-
-
-
diff --git a/xos/attic/vsgserviceinstance_model.py b/xos/attic/vsgserviceinstance_model.py
deleted file mode 100644
index 08ede12..0000000
--- a/xos/attic/vsgserviceinstance_model.py
+++ /dev/null
@@ -1,29 +0,0 @@
-
-# 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.
-
-
-def __init__(self, *args, **kwargs):
-    super(VSGServiceInstance, self).__init__(*args, **kwargs)
-
-def __xos_save_base(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")
-
-    return False
diff --git a/xos/header.py b/xos/header.py
deleted file mode 120000
index 721b5c0..0000000
--- a/xos/header.py
+++ /dev/null
@@ -1 +0,0 @@
-attic/header.py
\ No newline at end of file
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/vsg.xproto b/xos/synchronizer/models/vsg.xproto
similarity index 97%
rename from xos/vsg.xproto
rename to xos/synchronizer/models/vsg.xproto
index 55985b7..6202ef4 100644
--- a/xos/vsg.xproto
+++ b/xos/synchronizer/models/vsg.xproto
@@ -1,6 +1,7 @@
 option name = "vsg";
 option app_label = "vsg";
 option kind = "vCPE";
+option legacy="True";
 
 message VSGService (Service){
     option verbose_name = "vSG Service";
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"
diff --git a/xos/vsg-onboard.yaml b/xos/vsg-onboard.yaml
index b21dc74..92d2fdf 100644
--- a/xos/vsg-onboard.yaml
+++ b/xos/vsg-onboard.yaml
@@ -29,8 +29,5 @@
           base_url: file:///opt/xos_services/vsg/xos/
           # The following will concatenate with base_url automatically, if
           # base_url is non-null.
-          xproto: ./
-          tosca_resource: tosca/resources/vsgservice.py
           private_key: file:///opt/xos/key_import/vsg_rsa
           public_key: file:///opt/xos/key_import/vsg_rsa.pub
-