2 # -*- coding: utf-8 -*-
4 # Copyright (c) 2018 Amdocs
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at:
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 from oslo_config import cfg
17 from oslo_log import log as logging
24 from multicloud_azure.pub.config.config import MR_ADDR
25 from multicloud_azure.pub.config.config import MR_PORT
28 LOG = logging.getLogger(__name__)
33 product_name = "oslo_server"
34 logging.register_options(cfg.CONF)
35 logging.setup(cfg.CONF, product_name)
39 below items must be added into vio nova.conf then restart nova services:
40 notification_driver=messaging
41 notification_topics= notifications_test
42 notify_on_state_change=vm_and_task_state
43 notify_on_any_change=True
44 instance_usage_audit=True
45 instance_usage_audit_period=hour
49 def getConfig(section, key):
51 config = ConfigParser.ConfigParser()
52 path = os.path.split(os.path.realpath(__file__))[0] + '/listener.conf'
54 return config.get(section, key)
57 class NotificationEndPoint():
59 filter_rule = oslo_messaging.NotificationFilter(
60 publisher_id='^compute.*')
62 def info(self, ctxt, publisher_id, event_type, payload, metadata):
65 'compute.instance.unpause.start',
66 'compute.instance.pause.start',
67 'compute.instance.power_off.start',
68 'compute.instance.reboot.start',
69 'compute.instance.create.start'
72 status = payload.get('state_description')
73 if status != '' and event_type in VM_EVENTS:
74 url = 'http://%s:%s/events/test' % (MR_ADDR, MR_PORT)
75 headers = {'Content-type': 'application/json'}
76 requests.post(url, json.dumps(payload), headers=headers)
81 def action(self, data):
82 LOG.info(_LI(json.dumps(data)))
88 self.topic = 'notifications_test'
93 class NotificationServer(Server):
96 super(NotificationServer, self).__init__()
97 # rabbit IP and password come from listener.conf
98 url = 'rabbit://test:%s@%s:5672/' % (
99 getConfig('Listener', 'rabbit_passwd'),
100 getConfig('Listener', 'rabbit_ip')
102 self.transport = oslo_messaging.get_notification_transport(
105 # The exchange must be the same as
106 # control_exchange in transport setting in client.
107 self.targets = [oslo_messaging.Target(
110 self.endpoints = [NotificationEndPoint()]
113 LOG.info(_LI("Start Notification server..."))
114 self.server = oslo_messaging.get_notification_listener(
118 executor='threading')
123 if __name__ == '__main__':
125 notification_server = NotificationServer()
126 notification_server.start()