Added V0 Registry API
[multicloud/azure.git] / azure / multicloud_azure / event_listener / server.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2018 Amdocs
5 #
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:
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
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.
15
16 from oslo_config import cfg
17 from oslo_log import log as logging
18 from i18n import _LI
19 import oslo_messaging
20 import ConfigParser
21 import json
22 import os
23 import requests
24 from multicloud_azure.pub.config.config import MR_ADDR
25 from multicloud_azure.pub.config.config import MR_PORT
26
27
28 LOG = logging.getLogger(__name__)
29
30
31 def prepare():
32
33     product_name = "oslo_server"
34     logging.register_options(cfg.CONF)
35     logging.setup(cfg.CONF, product_name)
36
37
38 '''
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
46 '''
47
48
49 def getConfig(section, key):
50
51     config = ConfigParser.ConfigParser()
52     path = os.path.split(os.path.realpath(__file__))[0] + '/listener.conf'
53     config.read(path)
54     return config.get(section, key)
55
56
57 class NotificationEndPoint():
58
59     filter_rule = oslo_messaging.NotificationFilter(
60             publisher_id='^compute.*')
61
62     def info(self, ctxt, publisher_id, event_type, payload, metadata):
63
64         VM_EVENTS = {
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'
70         }
71
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)
77
78         LOG.info(event_type)
79         self.action(payload)
80
81     def action(self, data):
82         LOG.info(_LI(json.dumps(data)))
83
84
85 class Server(object):
86
87     def __init__(self):
88         self.topic = 'notifications_test'
89         self.server = None
90         prepare()
91
92
93 class NotificationServer(Server):
94
95     def __init__(self):
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')
101             )
102         self.transport = oslo_messaging.get_notification_transport(
103             cfg.CONF,
104             url=url)
105         # The exchange must be the same as
106         # control_exchange in transport setting in client.
107         self.targets = [oslo_messaging.Target(
108             topic=self.topic,
109             exchange='nova')]
110         self.endpoints = [NotificationEndPoint()]
111
112     def start(self):
113         LOG.info(_LI("Start Notification server..."))
114         self.server = oslo_messaging.get_notification_listener(
115             self.transport,
116             self.targets,
117             self.endpoints,
118             executor='threading')
119         self.server.start()
120         self.server.wait()
121
122
123 if __name__ == '__main__':
124
125     notification_server = NotificationServer()
126     notification_server.start()