K8S based XOS Service for EPC

Change-Id: I65bdabc3d63240bbdd21cb9e8674ff6723dd994f
diff --git a/xos/synchronizer/event_steps/vepcevent.py b/xos/synchronizer/event_steps/vepcevent.py
new file mode 100644
index 0000000..cece5fe
--- /dev/null
+++ b/xos/synchronizer/event_steps/vepcevent.py
@@ -0,0 +1,45 @@
+
+# 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.
+
+
+import json
+import os
+import sys
+from synchronizers.new_base.eventstep import EventStep
+from synchronizers.new_base.modelaccessor import *
+from xosconfig import Config
+from multistructlog import create_logger
+
+log = create_logger(Config().get('logging'))
+
+class VEpcEventStep(EventStep):
+    topics = ["VEpcEvent"]
+    technology = "kafka"
+
+    def __init__(self, *args, **kwargs):
+        super(VEpcEventStep, self).__init__(*args, **kwargs)
+
+    def process_event(self, event):
+        value = json.loads(event.value)
+        service_instance_name = value["service_instance"]
+        #tenant_message = value["tenant_message"]
+
+        objs = VEpcServiceInstance.objects.filter(name=service_instance_name)
+        if not objs:
+            raise Exception("failed to find %s" % service_instance_name)
+
+        for obj in objs:
+            #obj.tenant_message = tenant_message
+            obj.save(always_update_timestamp = True)
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_vepcserviceinstance.py b/xos/synchronizer/model_policies/model_policy_vepcserviceinstance.py
new file mode 100644
index 0000000..bcf6d53
--- /dev/null
+++ b/xos/synchronizer/model_policies/model_policy_vepcserviceinstance.py
@@ -0,0 +1,143 @@
+
+# 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.
+
+import base64
+import jinja2
+import json
+from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
+
+from xosconfig import Config
+from multistructlog import create_logger
+
+log = create_logger(Config().get('logging'))
+
+class VEpcServiceInstancePolicy(Policy):
+    model_name = "VEpcServiceInstance"
+
+    def handle_create(self, service_instance):
+        return self.handle_update(service_instance)
+
+    # def render_index(self, service_instance):
+    #     service = service_instance.owner.leaf_model
+
+    #     fields = {}
+    #     fields['tenant_message'] = service_instance.tenant_message
+    #     fields['service_message'] = service.service_message
+
+    #     if service_instance.foreground_color:
+    #         fields["foreground_color"] = service_instance.foreground_color.html_code
+
+    #     if service_instance.background_color:
+    #         fields["background_color"] = service_instance.background_color.html_code
+
+    #     images=[]
+    #     for image in service_instance.embedded_images.all():
+    #         images.append({"name": image.name,
+    #                        "url": image.url})
+    #     fields["images"] = images
+
+    #     template_fn = os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "index.html.j2")
+    #     template = jinja2.Template(open(template_fn).read())
+
+    #     return template.render(fields)
+
+    def handle_update(self, service_instance):
+        # if not service_instance.compute_instance:
+
+        #     # TODO: Break dependency
+        #     compute_service = KubernetesService.objects.first()
+        #     compute_service_instance_class = Service.objects.get(id=compute_service.id).get_service_instance_class()
+
+        #     vepcservice = service_instance.owner.leaf_model
+
+        #     # TODO: What if there is the wrong number of slices?
+        #     slice = vepcservice.slices.first()
+
+
+        #     # TODO: What if there is no default image?
+        #     # image = slice.default_image
+
+        #     images = service_instance.images.all()
+
+        #     # TODO: Gopi: Create the XOS Image object for each container
+        #     # TODO: Gopi: Might have to extend the K8S synch to support multiple containers in a K8S pod
+        #     # TODO: Gopi: Will have to extend the K8S sync to support StatefulSets
+
+        #     docker_images = []
+        #     for image in images:
+        #         # TODO: Gopi: WORK ON THIS LOOP. THE Names need to be explicit now
+        #         # TODO: slice = vepcservice.slices.get(name="mysite_vepcservice_mme")
+        #         docker_image = "%s:%s" % (image.image_name, image.image_tag)
+        #         compute_service_instance = compute_service_instance_class(slice=slice, owner=compute_service, image=docker_image, name=image.component_name) # Might need this at some point if I use configmap/secret: no_sync=True
+        #         compute_service_instance.save()
+
+        #     # # Create a configmap and attach it to the compute instance
+        #     # data = {"index.html": self.render_index(service_instance)}
+        #     # cfmap = KubernetesConfigMap(name="vepcserviceinstance-map-%s" % service_instance.id,
+        #     #                           trust_domain=slice.trust_domain,
+        #     #                           data=json.dumps(data))
+        #     # cfmap.save()
+        #     # cfmap_mnt = KubernetesConfigVolumeMount(config=cfmap,
+        #     #                                         service_instance=compute_service_instance,
+        #     #                                         mount_path="/usr/local/apache2/htdocs")
+        #     # cfmap_mnt.save()
+
+        #     # # Create a secret and attach it to the compute instance
+        #     # data = {"service_secret.txt": base64.b64encode(str(vepcservice.service_secret)),
+        #     #         "tenant_secret.txt": base64.b64encode(str(service_instance.tenant_secret))}
+        #     # secret = KubernetesSecret(name="vepcserviceinstance-secret-%s" % service_instance.id,
+        #     #                           trust_domain=slice.trust_domain,
+        #     #                           data=json.dumps(data))
+        #     # secret.save()
+        #     # secret_mnt = KubernetesSecretVolumeMount(secret=secret, service_instance=compute_service_instance, mount_path="/usr/local/apache2/secrets")
+        #     # secret_mnt.save()
+
+        #     # compute_service_instance.no_sync = False
+        #     # compute_service_instance.save(update_fields=["no_sync"])
+
+        #     # TODO: Gopi/Scott revisit this to make it more robust to declare
+        #     # the K8S service instances dynamically
+        #     service_instance.compute_instance_mme = compute_service_instance_mme
+        #     service_instance.save(update_fields=["compute_instance_mme"])
+        #     service_instance.compute_instance_hss = compute_service_instance_hss
+        #     service_instance.save(update_fields=["compute_instance_hss"])
+        #     service_instance.compute_instance_hssdb = compute_service_instance_hssdb
+        #     service_instance.save(update_fields=["compute_instance_hssdb"])
+        #     service_instance.compute_instance_spgw = compute_service_instance_spgw
+        #     service_instance.save(update_fields=["compute_instance_spgw"])
+        # else:
+        #     # TODO: Gopi: Need to handle the scenario of multiple compute instances representing the K8S Pods
+        #     compute_instance = service_instance.compute_instance
+        #     mnt = compute_instance.leaf_model.kubernetes_config_volume_mounts.first()
+        #     config = mnt.config
+        #     new_data = json.dumps({"index.html": self.render_index(service_instance)})
+        #     if (new_data != config.data):
+        #         config.data = new_data
+        #         config.save(always_update_timestamp=True)
+        #         # Force the Kubernetes syncstep
+        #         compute_instance.save(always_update_timestamp=True)
+        pass
+
+    def handle_delete(self, service_instance):
+        # TODO: Gopi: Need to handle the scenario of multiple compute instances representing the K8S Pods
+        # log.info("handle_delete")
+        # if service_instance.compute_instance:
+        #     log.info("has a compute_instance")
+        #     service_instance.compute_instance.delete()
+        #     service_instance.compute_instance = None
+        #     # TODO: I'm not sure we can save things that are being deleted...
+        #     service_instance.save(update_fields=["compute_instance"])
+        pass
diff --git a/xos/synchronizer/models/vepcservice.xproto b/xos/synchronizer/models/vepcservice.xproto
new file mode 100644
index 0000000..223ee0c
--- /dev/null
+++ b/xos/synchronizer/models/vepcservice.xproto
@@ -0,0 +1,16 @@
+option app_label = "vepcservice";
+option name = "vepcservice";
+
+message VEpcService (Service){
+    option verbose_name = "Virtual EPC Service";
+}
+
+message VEpcServiceInstance (ServiceInstance){
+     option verbose_name = "Virtual EPC Instance";
+}
+
+message VEpcResourceInstanceLink(XOSBase) {
+    required string name = 1 [help_text = "Resource link name", db_index=True, tosca_key=True];
+    required manytoone resource_instance->KubernetesResourceInstance:vepc_resource_instance_links = 2 [help_text = "Kubernetes resource file content", db_index=True, null=False, blank=False];
+    required manytoone vepc_service_instance->VEpcServiceInstance:vepc_resource_instance_links = 3 [help_text = "Virtual EPC Service Instance", db_index=True, null=False, blank=False];
+}
\ No newline at end of file
diff --git a/xos/synchronizer/steps/sync_vepcserviceinstance.py b/xos/synchronizer/steps/sync_vepcserviceinstance.py
new file mode 100644
index 0000000..91d3c54
--- /dev/null
+++ b/xos/synchronizer/steps/sync_vepcserviceinstance.py
@@ -0,0 +1,48 @@
+
+# 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.
+
+
+import os
+import sys
+from synchronizers.new_base.syncstep import SyncStep
+from synchronizers.new_base.modelaccessor import *
+from xosconfig import Config
+from multistructlog import create_logger
+
+log = create_logger(Config().get('logging'))
+
+class SyncVEpcServiceInstance(SyncStep):
+
+    provides = [VEpcServiceInstance]
+
+    observes = VEpcServiceInstance
+
+    requested_interval = 0
+
+    # template_name = "vepcserviceinstance_playbook.yaml"
+
+    # service_key_name = "/opt/xos/synchronizers/vepcservicenew/vepcservice_private_key"
+
+    def __init__(self, *args, **kwargs):
+        super(SyncVEpcServiceInstance, self).__init__(*args, **kwargs)
+
+    def sync_record(self, o):
+        # There's nothing to do at this time. Configuration of VEpcServiceInstance is handled by Kubernetes
+        # through config maps, and that all happens in the model policy.
+        #
+        # TODO(smbaker): Consider deleting this sync step
+        # Note from Gopi: This method exists as the code is copied from Simple Example Service. Please
+        # discuss with smbaker if this function is necessary.
+        pass
diff --git a/xos/synchronizer/tests/test_config.yaml b/xos/synchronizer/tests/test_config.yaml
new file mode 100644
index 0000000..acd2ba7
--- /dev/null
+++ b/xos/synchronizer/tests/test_config.yaml
@@ -0,0 +1,29 @@
+# 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: test-model-policies
+accessor:
+  username: xosadmin@opencord.org
+  password: "sample"
+  kind: "testframework"
+logging:
+  version: 1
+  handlers:
+    console:
+      class: logging.StreamHandler
+  loggers:
+    'multistructlog':
+      handlers:
+          - console
diff --git a/xos/synchronizer/tests/test_model_policy_vepcserviceinstance.py b/xos/synchronizer/tests/test_model_policy_vepcserviceinstance.py
new file mode 100644
index 0000000..c6d301b
--- /dev/null
+++ b/xos/synchronizer/tests/test_model_policy_vepcserviceinstance.py
@@ -0,0 +1,54 @@
+
+# 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.
+
+# Tests for VEpcServiceInstance model policies
+
+import base64
+import json
+import os
+import sys
+import unittest
+from mock import patch, PropertyMock, ANY, MagicMock
+from unit_test_common import setup_sync_unit_test
+
+
+class TestVEpcServiceInstancePolicy(unittest.TestCase):
+
+    def setUp(self):
+        self.unittest_setup = setup_sync_unit_test(os.path.abspath(os.path.dirname(os.path.realpath(__file__))),
+                                                   globals(),
+                                                   [("vepcservice", "vepcservice.xproto"),
+                                                    ("kubernetes-service", "kubernetes.xproto")] )
+
+        self.MockObjectList = self.unittest_setup["MockObjectList"]
+
+        sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../model_policies"))
+
+        from model_policy_vepcserviceinstance import VEpcServiceInstancePolicy
+        self.policy_class = VEpcServiceInstancePolicy
+
+        self.service = VEpcService()
+        self.k8s_service = KubernetesService(id=1111)
+        self.k8s_service.get_service_instance_class=MagicMock(return_value=KubernetesServiceInstance)
+        self.trust_domain = TrustDomain(owner=self.k8s_service, name="test-trust")
+        self.image = Image(name="test-image", tag="1.2", kind="container")
+        self.slice = Slice(trust_domain=self.trust_domain, service=self.service, default_image = self.image)
+        self.service.slices = self.MockObjectList([self.slice])
+
+    def tearDown(self):
+        sys.path = self.unittest_setup["sys_path_save"]
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/xos/synchronizer/tests/unit_test_common.py b/xos/synchronizer/tests/unit_test_common.py
new file mode 100644
index 0000000..68f6743
--- /dev/null
+++ b/xos/synchronizer/tests/unit_test_common.py
@@ -0,0 +1,84 @@
+
+# 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.
+
+import os
+import sys
+
+def setup_sync_unit_test(test_path, globals_dict, models, config_fn="test_config.yaml"):
+    """ Perform the common steps associated with setting up a synchronizer unit test.
+           1) Add synchronizers/new_base to sys.path
+           2) Import xosconfig.Config and set it up to test_config.yaml in the current dir
+           3) Build the mock modelaccessor and import it
+           4) Import all model accessor classes into global space
+
+        Arguments:
+            test_path - path to the test case that is being run
+            globals_dict - a dictionary to add global models to
+            models - a list of pairs (service_name, xproto_name,
+            config_fn - filename of config file)
+
+        Returns:
+            Dictionary containing the following:
+                sys_path_save: the original sys.path
+                model_accessor: model accessor class
+                Config: the Config object
+                xos_dir: xos directory
+                services_dir: services directory
+    """
+    def get_models_fn(services_dir, 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))
+
+    sys_path_save = sys.path
+
+    xos_dir = os.path.join(test_path, "../../..")
+    if not os.path.exists(os.path.join(test_path, "new_base")):
+        xos_dir = os.path.join(test_path, "../../../../../../orchestration/xos/xos")
+        services_dir = os.path.join(xos_dir, "../../xos_services")
+    sys.path.append(xos_dir)
+    sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+
+    # Setting up the config module
+    from xosconfig import Config
+    config = os.path.join(test_path, config_fn)
+    Config.clear()
+    Config.init(config, "synchronizer-config-schema.yaml")
+
+    xprotos = []
+    for (service_name, xproto_name) in models:
+        xprotos.append(get_models_fn(services_dir, service_name, xproto_name))
+
+    from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
+    build_mock_modelaccessor(xos_dir, services_dir, xprotos)
+    import synchronizers.new_base.modelaccessor
+    from synchronizers.new_base.modelaccessor import model_accessor
+    from mock_modelaccessor import MockObjectList
+
+    # import all class names to globals
+    for (k, v) in model_accessor.all_model_classes.items():
+        globals_dict[k] = v
+
+    return {"sys_path_save": sys_path_save,
+            "model_accessor": model_accessor,
+            "Config": Config,
+            "xos_dir": xos_dir,
+            "services_dir": services_dir,
+            "MockObjectList": MockObjectList}
diff --git a/xos/synchronizer/vepcservice-synchronizer.py b/xos/synchronizer/vepcservice-synchronizer.py
new file mode 100644
index 0000000..590c1e2
--- /dev/null
+++ b/xos/synchronizer/vepcservice-synchronizer.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+
+# 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.
+
+# Runs the standard XOS synchronizer
+
+import importlib
+import os
+import sys
+from xosconfig import Config
+
+config_file = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/vepcservice_config.yaml')
+Config.init(config_file, 'synchronizer-config-schema.yaml')
+
+synchronizer_path = os.path.join(os.path.dirname(
+    os.path.realpath(__file__)), "../../synchronizers/new_base")
+sys.path.append(synchronizer_path)
+mod = importlib.import_module("xos-synchronizer")
+mod.main()
+
diff --git a/xos/synchronizer/vepcservice_config.yaml b/xos/synchronizer/vepcservice_config.yaml
new file mode 100644
index 0000000..7d4d0aa
--- /dev/null
+++ b/xos/synchronizer/vepcservice_config.yaml
@@ -0,0 +1,33 @@
+
+# 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: vepcservice
+accessor:
+  username: xosadmin@opencord.org
+  password: "@/opt/xos/services/vepcservice/credentials/xosadmin@opencord.org"
+required_models:
+  - VEpcService
+  - VEpcServiceInstance
+  - VEpcResourceInstanceLink
+  - ServiceDependency
+  - KubernetesService
+  - kubernetesResourceInstance
+dependency_graph: "/opt/xos/synchronizers/vepcservice/model-deps"
+steps_dir: "/opt/xos/synchronizers/vepcservice/steps"
+event_steps_dir: "/opt/xos/synchronizers/vepcservice/event_steps"
+sys_dir: "/opt/xos/synchronizers/vepcservice/sys"
+model_policies_dir: "/opt/xos/synchronizers/vepcservice/model_policies"
+models_dir: "/opt/xos/synchronizers/vepcservice/models"