summaryrefslogtreecommitdiff
path: root/ceilometer/messaging.py
blob: 4b66e3eb8e9c9c250d0f68f4226664202bed776f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- encoding: utf-8 -*-
# Copyright © 2013 eNovance <licensing@enovance.com>
#
# Author: Mehdi Abaakouk <mehdi.abaakouk@enovance.com>
#
# 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 oslo.config import cfg
import oslo.messaging

TRANSPORT = None
NOTIFIER = None

_ALIASES = {
    'ceilometer.openstack.common.rpc.impl_kombu': 'rabbit',
    'ceilometer.openstack.common.rpc.impl_qpid': 'qpid',
    'ceilometer.openstack.common.rpc.impl_zmq': 'zmq',
}


def setup(url=None):
    """Initialise the oslo.messaging layer."""
    global TRANSPORT, NOTIFIER
    if not TRANSPORT:
        oslo.messaging.set_transport_defaults('ceilometer')
        TRANSPORT = oslo.messaging.get_transport(cfg.CONF, url,
                                                 aliases=_ALIASES)
    if not NOTIFIER:
        NOTIFIER = oslo.messaging.Notifier(TRANSPORT)


def cleanup():
    """Cleanup the oslo.messaging layer."""
    global TRANSPORT, NOTIFIER
    assert TRANSPORT is not None
    assert NOTIFIER is not None
    TRANSPORT.cleanup()
    TRANSPORT = NOTIFIER = None


def get_rpc_server(topic, endpoint):
    """Return a configured oslo.messaging rpc server."""
    global TRANSPORT
    target = oslo.messaging.Target(server=cfg.CONF.host, topic=topic)
    return oslo.messaging.get_rpc_server(TRANSPORT, target, [endpoint],
                                         executor='eventlet')


def get_rpc_client(**kwargs):
    """Return a configured oslo.messaging RPCClient."""
    global TRANSPORT
    target = oslo.messaging.Target(**kwargs)
    return oslo.messaging.RPCClient(TRANSPORT, target)


def get_notification_listener(targets, endpoint):
    """Return a configured oslo.messaging notification listener."""
    global TRANSPORT
    return oslo.messaging.get_notification_listener(
        TRANSPORT, targets, [endpoint], executor='eventlet')


def get_notifier(publisher_id):
    """Return a configured oslo.messaging notifier."""
    global NOTIFIER
    return NOTIFIER.prepare(publisher_id=publisher_id)


def convert_to_old_notification_format(priority, ctxt, publisher_id,
                                       event_type, payload, metadata):
    #FIXME(sileht): temporary convert notification to old format
    #to focus on oslo.messaging migration before refactoring the code to
    #use the new oslo.messaging facilities
    notification = {'priority': priority,
                    'payload': payload,
                    'event_type': event_type,
                    'publisher_id': publisher_id}
    notification.update(metadata)
    for k in ctxt:
        notification['_context_' + k] = ctxt[k]
    return notification