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
+