CORD-782: monitoring service support for external analytics apps

Change-Id: I81a850642b19340d6bd7fb9528e936d7ab6e6e82
diff --git a/xos/synchronizer/steps/sync_ceilometerservice.yaml b/xos/synchronizer/steps/sync_ceilometerservice.yaml
index f05ce45..6fb680c 100644
--- a/xos/synchronizer/steps/sync_ceilometerservice.yaml
+++ b/xos/synchronizer/steps/sync_ceilometerservice.yaml
@@ -45,7 +45,10 @@
       - { regexp: '^[#]{0,}advertised\.host\.name=', line: 'advertised.host.name={{ instance_hostname }}' }
       - { regexp: '^[#]{0,}zookeeper\.connect=', line: 'zookeeper.connect={{ instance_hostname }}:2181' }
     notify:
+     - restart zookeeper service
      - restart kafka service
+     - stop ceilometer pub-sub
+     - start ceilometer pub-sub
 
   - name: Enabling/Disabling kafka publishing
     become: yes
@@ -61,9 +64,19 @@
 #     - restart ceilometer-agent-notification service
 
   handlers:
+  - name: restart zookeeper service
+    become: yes
+    service: name=zookeeper state=restarted
   - name: restart kafka service
     become: yes
     service: name=kafka state=restarted
+  - name: stop ceilometer pub-sub 
+    become: yes
+    shell: pkill -f sub_main.py
+    ignore_errors: True
+  - name: start ceilometer pub-sub 
+    become: yes
+    shell: cd /etc/init.d;sudo bash zxceilostartup.sh
   - name: restart ceilometer-agent-notification service
     become: yes
     service: name=ceilometer-agent-notification state=restarted
diff --git a/xos/synchronizer/steps/sync_monitoringchannel.py b/xos/synchronizer/steps/sync_monitoringchannel.py
index 07db238..49a1962 100644
--- a/xos/synchronizer/steps/sync_monitoringchannel.py
+++ b/xos/synchronizer/steps/sync_monitoringchannel.py
@@ -14,7 +14,7 @@
 from synchronizers.base.syncstep import SyncStep
 from synchronizers.base.ansible_helper import run_template_ssh
 from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
-from core.models import Service, Slice
+from core.models import Service, Slice, ModelLink
 from services.monitoring.models import CeilometerService, MonitoringChannel
 from xos.logger import Logger, logging
 
@@ -44,6 +44,7 @@
         self.open = False
 
     def start(self):
+        logger.info("Creating SSH Tunnel: ssh -MfN -S %s -i %s -L %s:%s:%s;%s -o ExitOnForwardFailure=True %s@%s"%(self.socket, self.key, self.local_host, self.local_port, self.remote_host, self.remote_port,self.jump_user,self.jump_host))
         exit_status = subprocess.call(['ssh', '-MfN',
             '-S', self.socket,
             '-i', self.key,
@@ -101,6 +102,7 @@
     requested_interval=0
     template_name = "sync_monitoringchannel.yaml"
     service_key_name = "/opt/xos/synchronizers/monitoring/monitoring_channel_private_key"
+    watches = [ModelLink(Slice,via='slice')]
 
     def __init__(self, *args, **kwargs):
         super(SyncMonitoringChannel, self).__init__(*args, **kwargs)
@@ -201,3 +203,17 @@
         fields = {"unique_id": o.id,
                   "delete": True}
         return fields
+
+    def handle_watched_object(self, o):
+        logger.info("handle_watched_object is invoked for object %s" % (str(o)),extra=o.tologdict())
+        if (type(o) is Slice):
+           self.handle_slice_watch_notification(o)
+        pass
+
+    def handle_slice_watch_notification(self, sliceobj):
+        logger.info("handle_slice_watch_notification: A slice %s is created or updated or deleted" % (sliceobj))
+        for obj in MonitoringChannel.get_tenant_objects().all():
+            #Save the monitoring channel object to reflect the newly updated slice
+            obj.save()
+        pass
+
diff --git a/xos/test/deploy_auto_scale_app.yaml b/xos/test/deploy_auto_scale_app.yaml
new file mode 100644
index 0000000..53258fd
--- /dev/null
+++ b/xos/test/deploy_auto_scale_app.yaml
@@ -0,0 +1,30 @@
+---
+- hosts: "{{ hosts }}"
+
+  tasks:
+    - name: install pip and related packages
+      apt: name="{{ item }}" state=present update-cache=yes
+      become: yes
+      with_items:
+        - python-pip
+        - python-dev
+        - build-essential
+        - git
+        - msgpack-python
+
+    - name: install python packages
+      pip: name="{{ item }}" state=present
+      become: yes
+      with_items:
+        - virtualenv
+        - Flask
+        - kafka-python
+        - oslo.utils
+        - Babel
+
+    - name: create clone directory
+      file: path=~/auto-scale2 state=directory
+
+    - name: clone auto-scale repo
+      git: repo=https://gerrit.opencord.org/xos-external-app-examples dest=~/auto-scale2
+
diff --git a/xos/test/kill-mon.py b/xos/test/kill-mon.py
new file mode 100644
index 0000000..0287ac8
--- /dev/null
+++ b/xos/test/kill-mon.py
@@ -0,0 +1,18 @@
+import os
+import sys
+sys.path.append("/opt/xos")
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
+import django
+from core.models import *
+#from services.exampleservice.models import *
+from services.monitoring.models import *
+django.setup()
+
+for o in CeilometerService.get_service_objects().all():
+   print "Delete", o
+   o.delete()
+
+print "wait for perma-deletion"
+
+while MonitoringChannel.deleted_objects.all().exists() or CeilometerService.deleted_objects.all().exists():
+    pass
diff --git a/xos/test/monitoring_in_loop.sh b/xos/test/monitoring_in_loop.sh
new file mode 100755
index 0000000..d35e545
--- /dev/null
+++ b/xos/test/monitoring_in_loop.sh
@@ -0,0 +1,10 @@
+while [[ 1 ]]; do
+    make cord-monitoringservice
+    if [[ $? -ne 0 ]]; then
+       echo "fail!"
+       exit
+    fi
+    docker cp kill-mon.py cordpod_xos_ui_1:/opt/xos/
+    sudo docker exec -it cordpod_xos_ui_1 python /opt/xos/kill-mon.py
+    sleep 10s
+done
diff --git a/xos/test/monitoringservice_analytics.yaml b/xos/test/monitoringservice_analytics.yaml
new file mode 100644
index 0000000..627c1be
--- /dev/null
+++ b/xos/test/monitoringservice_analytics.yaml
@@ -0,0 +1,114 @@
+tosca_definitions_version: tosca_simple_yaml_1_0
+
+description: Setup Slice for analytics applications.
+
+imports:
+   - custom_types/xos.yaml
+   - custom_types/monitoring_tosca_types.yaml
+
+topology_template:
+  node_templates:
+    service_ceilometer:
+      type: tosca.nodes.CeilometerService
+      properties:
+          no-create: true
+          no-update: true
+          no-delete: true
+
+    service_analytics:
+      type: tosca.nodes.Service
+      requirements:
+          - monitoring_tenant:
+              node: service_ceilometer
+              relationship: tosca.relationships.TenantOfService
+      properties:
+          kind: analytics
+
+# private network template, created in fixtures.yml
+    Private:
+      type: tosca.nodes.NetworkTemplate
+
+# site, fully created in deployment.yaml
+    mysite:
+      type: tosca.nodes.Site
+
+    trusty-server-multi-nic:
+      type: tosca.nodes.Image
+
+    m1.small:
+      type: tosca.nodes.Flavor
+
+# management network, fully created in management-net.yaml
+    management:
+      type: tosca.nodes.network.Network.XOS
+      properties:
+        no-create: true
+        no-delete: true
+        no-update: true
+
+# public network, fully created in public-net.yaml
+    public:
+      type: tosca.nodes.network.Network.XOS
+      properties:
+        no-create: true
+        no-delete: true
+        no-update: true
+
+    analytics_app_network:
+      type: tosca.nodes.network.Network.XOS
+      properties:
+          ip_version: 4
+          labels: analytics_app_private_network
+      requirements:
+          - network_template:
+              node: Private
+              relationship: tosca.relationships.UsesNetworkTemplate
+          - owner:
+              node: mysite_analytics
+              relationship: tosca.relationships.MemberOfSlice
+          - connection:
+              node: mysite_analytics
+              relationship: tosca.relationships.ConnectsToSlice
+
+    mysite_analytics:
+      type: tosca.nodes.Slice
+      properties:
+          network: noauto
+      requirements:
+          - service:
+              node: service_analytics
+              relationship: tosca.relationships.MemberOfService
+          - site:
+              node: mysite
+              relationship: tosca.relationships.MemberOfSite
+          - management:
+              node: management
+              relationship: tosca.relationships.ConnectsToNetwork
+          - public:
+              node: public
+              relationship: tosca.relationships.ConnectsToNetwork
+          - default_image:
+                node: trusty-server-multi-nic
+                relationship: tosca.relationships.DefaultImage
+          - m1.small:
+              node: m1.small
+              relationship: tosca.relationships.DefaultFlavor
+
+    # Virtual machines
+    auto_scale_virtual_machine:
+      type: tosca.nodes.Compute
+      capabilities:
+        # Host container properties
+        host:
+         properties:
+           num_cpus: 1
+           disk_size: 20 GB
+           mem_size: 2 GB
+      requirements:
+          - slice:
+                node: mysite_analytics
+                relationship: tosca.relationships.MemberOfSlice
+          - image:
+                node: trusty-server-multi-nic
+                relationship: tosca.relationships.UsesImage
+
diff --git a/xos/test/service_for_scale.yaml b/xos/test/service_for_scale.yaml
new file mode 100644
index 0000000..4d5a6c8
--- /dev/null
+++ b/xos/test/service_for_scale.yaml
@@ -0,0 +1,75 @@
+tosca_definitions_version: tosca_simple_yaml_1_0
+
+description: Setup example service/slice to demonstrate auto-scaling for that service/slice.
+
+imports:
+   - custom_types/xos.yaml
+
+topology_template:
+  node_templates:
+    service_xyz:
+      type: tosca.nodes.Service
+      properties:
+          kind: xyz
+
+# private network template, created in fixtures.yml
+    Private:
+      type: tosca.nodes.NetworkTemplate
+
+# site, fully created in deployment.yaml
+    mysite:
+      type: tosca.nodes.Site
+
+    trusty-server-multi-nic:
+      type: tosca.nodes.Image
+
+    m1.small:
+      type: tosca.nodes.Flavor
+
+# management network, fully created in management-net.yaml
+    management:
+      type: tosca.nodes.network.Network.XOS
+      properties:
+        no-create: true
+        no-delete: true
+        no-update: true
+
+    mysite_scalable_xyz:
+      type: tosca.nodes.Slice
+      properties:
+          network: noauto
+      requirements:
+          - service:
+              node: service_xyz
+              relationship: tosca.relationships.MemberOfService
+          - site:
+              node: mysite
+              relationship: tosca.relationships.MemberOfSite
+          - management:
+              node: management
+              relationship: tosca.relationships.ConnectsToNetwork
+          - default_image:
+                node: trusty-server-multi-nic
+                relationship: tosca.relationships.DefaultImage
+          - m1.small:
+              node: m1.small
+              relationship: tosca.relationships.DefaultFlavor
+
+    # Virtual machines
+    xyz_service_vm1:
+      type: tosca.nodes.Compute
+      capabilities:
+        # Host container properties
+        host:
+         properties:
+           num_cpus: 1
+           disk_size: 20 GB
+           mem_size: 2 GB
+      requirements:
+          - slice:
+                node: mysite_scalable_xyz
+                relationship: tosca.relationships.MemberOfSlice
+          - image:
+                node: trusty-server-multi-nic
+                relationship: tosca.relationships.UsesImage
+