[CORD-2502] fix internet emulator and add video server

Change-Id: I63e298c9d064209974208ae3b4c7678b8db83e8b
diff --git a/xos/internetemulator-onboard.yaml b/xos/internetemulator-onboard.yaml
index 013a00b..5f38260 100644
--- a/xos/internetemulator-onboard.yaml
+++ b/xos/internetemulator-onboard.yaml
@@ -1,4 +1,3 @@
-
 # Copyright 2017-present Open Networking Foundation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,7 +12,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-
 tosca_definitions_version: tosca_simple_yaml_1_0
 
 description: Onboard the internetemulator
@@ -23,15 +21,15 @@
 
 topology_template:
   node_templates:
-    internetemulator:
+    servicecontroller#internetemulator:
       type: tosca.nodes.ServiceController
       properties:
           base_url: file:///opt/xos_services/internetemulator/xos/
           # The following will concatenate with base_url automatically, if
           # base_url is non-null.
           xproto: ./
-          tosca_custom_types: internetemulator.yaml
-          tosca_resource: tosca/resources/internetemulatorservice.py, tosca/resources/internetemulatorserviceinstance.py
+          synchronizer: synchronizer/manifest
+          synchronizer_run: internetemulator-synchronizer.py
           private_key: file:///opt/xos/key_import/mcord_rsa
           public_key: file:///opt/xos/key_import/mcord_rsa.pub
 
diff --git a/xos/internetemulator.m4 b/xos/internetemulator.m4
deleted file mode 100644
index 8eecab2..0000000
--- a/xos/internetemulator.m4
+++ /dev/null
@@ -1,40 +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.
-
-
-tosca_definitions_version: tosca_simple_yaml_1_0
-
-# compile this with "m4 internetemulator.m4 > internetemulator.yaml"
-
-# include macros
-include(macros.m4)
-
-node_types:
-    tosca.nodes.InternetEmulatorService:
-        derived_from: tosca.nodes.Root
-        description: >
-            Internet Emulator
-        capabilities:
-            xos_base_service_caps
-        properties:
-            xos_base_props
-            xos_base_service_props
-
-    tosca.nodes.InternetEmulatorServiceInstance:
-        derived_from: tosca.nodes.Root
-        description: >
-            A ServiceInstance of the Internet Emulator
-        properties:
-            xos_base_tenant_props
diff --git a/xos/internetemulator.yaml b/xos/internetemulator.yaml
deleted file mode 100644
index eb9b211..0000000
--- a/xos/internetemulator.yaml
+++ /dev/null
@@ -1,91 +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.
-
-# compile this with "m4 internetemulator.m4 > internetemulator.yaml"
-
-tosca_definitions_version: tosca_simple_yaml_1_0
-
-node_types:
-    tosca.nodes.InternetEmulatorService:
-        derived_from: tosca.nodes.Root
-        description: >
-            Internet Emulator
-        capabilities:
-            scalable:
-                type: tosca.capabilities.Scalable
-            service:
-                type: tosca.capabilities.xos.Service
-        properties:
-            no-delete:
-                type: boolean
-                default: false
-                description: Do not allow Tosca to delete this object
-            no-create:
-                type: boolean
-                default: false
-                description: Do not allow Tosca to create this object
-            no-update:
-                type: boolean
-                default: false
-                description: Do not allow Tosca to update this object
-            replaces:
-                type: string
-                required: false
-                descrption: Replaces/renames this object
-            kind:
-                type: string
-                default: generic
-                description: Type of service.
-            view_url:
-                type: string
-                required: false
-                description: URL to follow when icon is clicked in the Service Directory.
-            icon_url:
-                type: string
-                required: false
-                description: ICON to display in the Service Directory.
-            enabled:
-                type: boolean
-                default: true
-            published:
-                type: boolean
-                default: true
-                description: If True then display this Service in the Service Directory.
-            public_key:
-                type: string
-                required: false
-                description: Public key to install into Instances to allows Services to SSH into them.
-            private_key_fn:
-                type: string
-                required: false
-                description: Location of private key file
-            versionNumber:
-                type: string
-                required: false
-                description: Version number of Service.
-
-    tosca.nodes.InternetEmulatorServiceInstance:
-        derived_from: tosca.nodes.Root
-        description: >
-            A ServiceInstance of the Internet Emulator
-        properties:
-            kind:
-                type: string
-                default: generic
-                description: Kind of tenant
-            service_specific_id:
-                type: string
-                required: false
-                description: Service specific ID opaque to XOS but meaningful to service
diff --git a/xos/macros.m4 b/xos/macros.m4
deleted file mode 100644
index 391aafd..0000000
--- a/xos/macros.m4
+++ /dev/null
@@ -1,100 +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.
-
-
-# Note: Tosca derived_from isn't working the way I think it should, it's not
-#    inheriting from the parent template. Until we get that figured out, use
-#    m4 macros do our inheritance
-
-define(xos_base_props,
-            no-delete:
-                type: boolean
-                default: false
-                description: Do not allow Tosca to delete this object
-            no-create:
-                type: boolean
-                default: false
-                description: Do not allow Tosca to create this object
-            no-update:
-                type: boolean
-                default: false
-                description: Do not allow Tosca to update this object
-            replaces:
-                type: string
-                required: false
-                descrption: Replaces/renames this object)
-# Service
-define(xos_base_service_caps,
-            scalable:
-                type: tosca.capabilities.Scalable
-            service:
-                type: tosca.capabilities.xos.Service)
-define(xos_base_service_props,
-            kind:
-                type: string
-                default: generic
-                description: Type of service.
-            view_url:
-                type: string
-                required: false
-                description: URL to follow when icon is clicked in the Service Directory.
-            icon_url:
-                type: string
-                required: false
-                description: ICON to display in the Service Directory.
-            enabled:
-                type: boolean
-                default: true
-            published:
-                type: boolean
-                default: true
-                description: If True then display this Service in the Service Directory.
-            public_key:
-                type: string
-                required: false
-                description: Public key to install into Instances to allows Services to SSH into them.
-            private_key_fn:
-                type: string
-                required: false
-                description: Location of private key file
-            versionNumber:
-                type: string
-                required: false
-                description: Version number of Service.)
-# Subscriber
-define(xos_base_subscriber_caps,
-            subscriber:
-                type: tosca.capabilities.xos.Subscriber)
-define(xos_base_subscriber_props,
-            kind:
-                type: string
-                default: generic
-                description: Kind of subscriber
-            service_specific_id:
-                type: string
-                required: false
-                description: Service specific ID opaque to XOS but meaningful to service)
-define(xos_base_tenant_props,
-            kind:
-                type: string
-                default: generic
-                description: Kind of tenant
-            service_specific_id:
-                type: string
-                required: false
-                description: Service specific ID opaque to XOS but meaningful to service)
-
-# end m4 macros
-
diff --git a/xos/synchronizer/model_policies/model_policy_internetemulatorserviceinstance.py b/xos/synchronizer/model_policies/model_policy_internetemulatorserviceinstance.py
index b34b121..5cc435c 100644
--- a/xos/synchronizer/model_policies/model_policy_internetemulatorserviceinstance.py
+++ b/xos/synchronizer/model_policies/model_policy_internetemulatorserviceinstance.py
@@ -19,3 +19,29 @@
 
 class InternetEmulatorServiceInstancePolicy(TenantWithContainerPolicy):
     model_name = "InternetEmulatorServiceInstance"
+    constrain_by_service_instance = True
+
+    def handle_create(self, service_instance):
+        return self.handle_update(service_instance)
+
+    def handle_update(self, service_instance):
+        if (service_instance.link_deleted_count>0) and (not service_instance.provided_links.exists()):
+            self.logger.info("The last provided link has been deleted -- self-destructing.")
+            self.handle_delete(service_instance)
+            if InternetEmulatorServiceInstance.objects.filter(id=service_instance.id).exists():
+                service_instance.delete()
+            else:
+                self.logger.info("Tenant %s is already deleted" % service_instance)
+            return
+
+        self.manage_container(service_instance)
+
+    def handle_delete(self, service_instance):
+        if service_instance.instance and (not service_instance.instance.deleted):
+            all_service_instances_this_instance = InternetEmulatorServiceInstance.objects.filter(instance_id=service_instance.instance.id)
+            other_service_instances_this_instance = [x for x in all_service_instances_this_instance if x.id != service_instance.id]
+            if (not other_service_instances_this_instance):
+                self.logger.info("InternetEmulatorServiceInstance Instance %s is now unused -- deleting" % service_instance.instance)
+                service_instance.instance.delete()
+            else:
+                self.logger.info("InternetEmulatorServiceInstance Instance %s has %d other service instances attached" % (service_instance.instance, len(other_service_instances_this_instance)))
diff --git a/xos/synchronizer/steps/internetemulatorserviceinstance_playbook.yaml b/xos/synchronizer/steps/internetemulatorserviceinstance_playbook.yaml
index 1160869..df2316e 100644
--- a/xos/synchronizer/steps/internetemulatorserviceinstance_playbook.yaml
+++ b/xos/synchronizer/steps/internetemulatorserviceinstance_playbook.yaml
@@ -23,5 +23,5 @@
   sudo: no
   gather_facts: no
 
-  tasks:
-    ping:
+  roles:
+    - start_video_server
diff --git a/xos/tosca/resources/internetemulatorservice.py b/xos/synchronizer/steps/roles/start_video_server/defaults/main.yml
similarity index 61%
copy from xos/tosca/resources/internetemulatorservice.py
copy to xos/synchronizer/steps/roles/start_video_server/defaults/main.yml
index 9ff43d7..43342fa 100644
--- a/xos/tosca/resources/internetemulatorservice.py
+++ b/xos/synchronizer/steps/roles/start_video_server/defaults/main.yml
@@ -13,13 +13,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+---
 
-from service import XOSService
-from services.internetemulator.models import InternetEmulatorService
-
-class XOSInternetEmulatorService(XOSService):
-    provides = "tosca.nodes.InternetEmulatorService"
-    xos_model = InternetEmulatorService
-    copyin_props = ["view_url", "icon_url", "enabled", "published", "public_key", "private_key_fn", "versionNumber"]
-
-
+video_streaming_port: 8080
+video_streaming_url: video
+ttl: 1
\ No newline at end of file
diff --git a/xos/tosca/resources/internetemulatorservice.py b/xos/synchronizer/steps/roles/start_video_server/tasks/main.yml
similarity index 61%
rename from xos/tosca/resources/internetemulatorservice.py
rename to xos/synchronizer/steps/roles/start_video_server/tasks/main.yml
index 9ff43d7..99ed9a9 100644
--- a/xos/tosca/resources/internetemulatorservice.py
+++ b/xos/synchronizer/steps/roles/start_video_server/tasks/main.yml
@@ -13,13 +13,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+---
 
-from service import XOSService
-from services.internetemulator.models import InternetEmulatorService
-
-class XOSInternetEmulatorService(XOSService):
-    provides = "tosca.nodes.InternetEmulatorService"
-    xos_model = InternetEmulatorService
-    copyin_props = ["view_url", "icon_url", "enabled", "published", "public_key", "private_key_fn", "versionNumber"]
-
-
+- name: start video streaming VLC server
+  command: vlc -I dummy demo-video.mp4 --sout='#rtp{sdp=rtsp://:{{ video_streaming_port }}/{{ video_streaming_url }}}' --sout-keep --loop --ttl {{ ttl }}
+  async: 1000
+  poll: 0
\ No newline at end of file
diff --git a/xos/tosca/resources/internetemulatorserviceinstance.py b/xos/tosca/resources/internetemulatorserviceinstance.py
deleted file mode 100644
index 7090be3..0000000
--- a/xos/tosca/resources/internetemulatorserviceinstance.py
+++ /dev/null
@@ -1,40 +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 xosresource import XOSResource
-from core.models import Service
-from services.internetemulator.models import InternetEmulatorServiceInstance
-
-class XOSInternetEmulatorServiceInstance(XOSResource):
-    provides = ["tosca.nodes.InternetEmulatorServiceInstance"]
-    xos_model = InternetEmulatorServiceInstance
-    name_field = "service_specific_id"
-
-    def get_xos_args(self, throw_exception=True):
-        args = super(XOSInternetEmulatorServiceInstance, self).get_xos_args()
-
-        provider_name = self.get_requirement("tosca.relationships.TenantOfService", throw_exception=True)
-        if provider_name:
-            args["owner"] = self.get_xos_object(Service, throw_exception=True, name=provider_name)
-
-        return args
-
-    def get_existing_objs(self):
-        args = self.get_xos_args(throw_exception=False)
-        return InternetEmulatorServiceInstance.objects.filter(owner=args["owner"], service_specific_id=args["service_specific_id"])
-        return []
-
-    def can_delete(self, obj):
-        return super(XOSInternetEmulatorServiceInstance, self).can_delete(obj)
-