CORD-2476 Migrate rcord profile to dynamic load

Change-Id: Iba149ccfb433d0be8918b617caca57e6e9a3f329
diff --git a/xos/synchronizer/Dockerfile.synchronizer b/xos/synchronizer/Dockerfile.synchronizer
index c89c468..fb7438d 100644
--- a/xos/synchronizer/Dockerfile.synchronizer
+++ b/xos/synchronizer/Dockerfile.synchronizer
@@ -52,4 +52,4 @@
       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 "sleep 86400"
+CMD bash -c "cd /opt/xos/synchronizers/rcord; ./run-from-api.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/models/attic/cordsubscriberroot_bottom.py b/xos/synchronizer/models/attic/cordsubscriberroot_bottom.py
new file mode 100644
index 0000000..cd51962
--- /dev/null
+++ b/xos/synchronizer/models/attic/cordsubscriberroot_bottom.py
@@ -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.
+
+
+CordSubscriberRoot.setup_simple_attributes()
diff --git a/xos/synchronizer/models/attic/cordsubscriberroot_model.py b/xos/synchronizer/models/attic/cordsubscriberroot_model.py
new file mode 100644
index 0000000..ef174c4
--- /dev/null
+++ b/xos/synchronizer/models/attic/cordsubscriberroot_model.py
@@ -0,0 +1,135 @@
+
+# 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.
+
+
+# 'simple_attributes' will be expanded into properties and setters that
+# store the attribute using self.set_attribute / self.get_attribute.
+
+simple_attributes = ( ("devices", []), )
+
+sync_attributes = ("firewall_enable",
+                   "firewall_rules",
+                   "url_filter_enable",
+                   "url_filter_rules",
+                   "cdn_enable",
+                   "uplink_speed",
+                   "downlink_speed",
+                   "enable_uverse",
+                   "status")
+
+def __init__(self, *args, **kwargs):
+    # TODO: Should probably make an RCORDService, rather than filtering by name
+    rcord_services = Service.objects.filter(name="rcord")
+    if not rcord_services:
+        # this isn't going to end well...
+        raise Exception("There is no rcord service to use as the default service for CordSubscriberRoot")
+
+    self._meta.get_field("owner").default = rcord_services[0].id
+
+    super(CordSubscriberRoot, self).__init__(*args, **kwargs)
+
+def find_device(self, mac):
+    for device in self.devices:
+        if device["mac"] == mac:
+            return device
+    return None
+
+def update_device(self, mac, **kwargs):
+    # kwargs may be "level" or "mac"
+    #    Setting one of these to None will cause None to be stored in the db
+    devices = self.devices
+    for device in devices:
+        if device["mac"] == mac:
+            for arg in kwargs.keys():
+                device[arg] = kwargs[arg]
+            self.devices = devices
+            return device
+    raise ValueError("Device with mac %s not found" % mac)
+
+def create_device(self, **kwargs):
+    if "mac" not in kwargs:
+        raise XOSMissingField("The mac field is required")
+
+    if self.find_device(kwargs['mac']):
+            raise XOSDuplicateKey("Device with mac %s already exists" % kwargs["mac"])
+
+    device = kwargs.copy()
+
+    devices = self.devices
+    devices.append(device)
+    self.devices = devices
+
+    return device
+
+def delete_device(self, mac):
+    devices = self.devices
+    for device in devices:
+        if device["mac"]==mac:
+            devices.remove(device)
+            self.devices = devices
+            return
+
+    raise ValueError("Device with mac %s not found" % mac)
+
+#--------------------------------------------------------------------------
+# Deprecated -- devices used to be called users
+
+def find_user(self, uid):
+    return self.find_device(uid)
+
+def update_user(self, uid, **kwargs):
+    return self.update_device(uid, **kwargs)
+
+def create_user(self, **kwargs):
+    return self.create_device(**kwargs)
+
+def delete_user(self, uid):
+    return self.delete_user(uid)
+
+# ------------------------------------------------------------------------
+
+@property
+def services(self):
+    return {"cdn": self.cdn_enable,
+            "url_filter": self.url_filter_enable,
+            "firewall": self.firewall_enable}
+
+@services.setter
+def services(self, value):
+    pass
+
+def invalidate_related_objects(self):
+    # Dirty all vSGs related to this subscriber, so the vSG synchronizer
+    # will run.
+
+    # TODO: This should be reimplemented when multiple-objects-per-synchronizer is implemented.
+
+    for link in self.subscribed_links.all():
+        outer_service_instance = link.provider_service_instance
+        for link in outer_service_instance.subscribed_links.all():
+            inner_service_instance = link.provider_service_instance
+            inner_service_instance.save(update_fields = ["updated"])
+
+def __xos_save_base(self, *args, **kwargs):
+    self.validate_unique_service_specific_id(none_okay=True)
+    if (not hasattr(self, 'caller') or not self.caller.is_admin):
+        if (self.has_field_changed("service_specific_id")):
+            raise XOSPermissionDenied("You do not have permission to change service_specific_id")
+
+    super(CordSubscriberRoot, self).save(*args, **kwargs)
+
+    self.invalidate_related_objects()
+
+    return True     # Indicate that we called super.save()
diff --git a/xos/synchronizer/models/attic/header.py b/xos/synchronizer/models/attic/header.py
new file mode 100644
index 0000000..a1cae74
--- /dev/null
+++ b/xos/synchronizer/models/attic/header.py
@@ -0,0 +1,34 @@
+
+# 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 django.db.models import *
+from core.models import Service, ServiceInstance, XOSBase, Slice, Instance, 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 Q
+from operator import itemgetter, attrgetter, methodcaller
+import traceback
+from xos.exceptions import *
+from xosconfig import Config
+
+class ConfigurationError(Exception):
+    pass
+
+CORD_SUBSCRIBER_KIND = "CordSubscriberRoot"
+
diff --git a/xos/synchronizer/models/rcord.xproto b/xos/synchronizer/models/rcord.xproto
new file mode 100644
index 0000000..1257ace
--- /dev/null
+++ b/xos/synchronizer/models/rcord.xproto
@@ -0,0 +1,18 @@
+option name = "rcord";
+option verbose_name = "RCORD Profile";
+
+message CordSubscriberRoot (ServiceInstance) {
+     option kind = "CordSubscriberRoot";
+
+     required bool firewall_enable = 1 [default = False, null = False, db_index = False, blank = True];
+     optional string firewall_rules = 2 [default = "accept all anywhere anywhere", null = True, db_index = False, blank = True];
+     required bool url_filter_enable = 3 [default = False, null = False, db_index = False, blank = True];
+     optional string url_filter_rules = 4 [default = "allow all", null = True, db_index = False, blank = True];
+     required string url_filter_level = 5 [default = "PG", max_length = 30, content_type = "stripped", blank = False, null = False, db_index = False];
+     required bool cdn_enable = 6 [default = False, null = False, db_index = False, blank = True];
+     required bool is_demo_user = 7 [default = False, null = False, db_index = False, blank = True];
+     required int32 uplink_speed = 8 [default = 1000000000, null = False, db_index = False, blank = False];
+     required int32 downlink_speed = 9 [default = 1000000000, null = False, db_index = False, blank = False];
+     required bool enable_uverse = 10 [default = True, null = False, db_index = False, blank = True];
+     required string status = 11 [default = "enabled", choices = "(('enabled', 'Enabled'), ('suspended', 'Suspended'), ('delinquent', 'Delinquent'), ('copyrightviolation', 'Copyright Violation'))", max_length = 30, content_type = "stripped", blank = False, null = False, db_index = False];
+}
diff --git a/xos/synchronizer/rcord-synchronizer.py b/xos/synchronizer/rcord-synchronizer.py
new file mode 100644
index 0000000..4e11e6d
--- /dev/null
+++ b/xos/synchronizer/rcord-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__)) + '/rcord_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/run-from-api.sh b/xos/synchronizer/run-from-api.sh
new file mode 100755
index 0000000..1d936f7
--- /dev/null
+++ b/xos/synchronizer/run-from-api.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 rcord-synchronizer.py